Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use tf.while_loop() for variable-length inputs in tensorflow?

I am trying to use tf.while_loop() to process variable-length inputs. However, I can only use it for fixed length. The code no longer works after I change shape=(4) to shape=(None). tf.dynamic_rnn seems to handle variable-length inputs. I am not sure how tf.dynamic_rnn achieves this with tf.while_loop().

import tensorflow as tf
import numpy as np
from tensorflow.python.ops import tensor_array_ops
from tensorflow.python.ops import array_ops

with tf.Graph().as_default(), tf.Session() as sess:
  initial_m = tf.Variable(0.0, name='m')

  inputs = tf.placeholder(dtype='float32', shape=(4))
  #The code no longer works after I change shape=(4) to shape=(None)
  #inputs = tf.placeholder(dtype='float32', shape=(None)) 

  time_steps = tf.shape(inputs)[0]

  initial_outputs = tf.TensorArray(dtype=tf.float32, size=time_steps)
  initial_t = tf.constant(0, dtype='int32')

  def should_continue(t, *args):
    return t < time_steps

  def iteration(t, m, outputs_):
    cur = tf.gather(inputs, t)
    m  = m * 0.5 + cur * 0.5
    outputs_ = outputs_.write(t, m)
    return t + 1, m, outputs_

  t, m, outputs = tf.while_loop(
    should_continue, iteration,
    [initial_t, initial_m, initial_outputs])

  outputs = outputs.pack()
  init = tf.global_variables_initializer()
  sess.run([init])
  print sess.run([outputs], feed_dict={inputs: np.asarray([1,1,1,1])})

output (before change):

[array([ 0.5   ,  0.75  ,  0.875 ,  0.9375], dtype=float32)]

output (after change):

Traceback (most recent call last):
  File "simple.py", line 26, in <module>
    [initial_t, initial_m, initial_outputs])
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2636, in while_loop
    result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2469, in BuildLoop
    pred, body, original_loop_vars, loop_vars, shape_invariants)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2450, in _BuildLoop
    _EnforceShapeInvariant(m_var, n_var)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 586, in _EnforceShapeInvariant
    % (merge_var.name, m_shape, n_shape))
ValueError: The shape for while/Merge_1:0 is not an invariant for the loop. It enters the loop with shape (), but has shape <unknown> after one iteration. Provide shape invariants using either the `shape_invariants` argument of tf.while_loop or set_shape() on the loop variables.
like image 520
read Read Avatar asked Jan 12 '17 03:01

read Read


1 Answers

It works if you remove shapes from all the variables:

import tensorflow as tf
import numpy as np

config = tf.ConfigProto(graph_options=tf.GraphOptions(
  optimizer_options=tf.OptimizerOptions(opt_level=tf.OptimizerOptions.L0)))
tf.reset_default_graph()
sess = tf.Session("", config=config)
#initial_m = tf.Variable(0.0, name='m')

#The code no longer works after I change shape=(4) to shape=(None)
inputs = tf.placeholder(dtype='float32', shape=(None)) 
time_steps = tf.shape(inputs)[0]
initial_outputs = tf.TensorArray(dtype=tf.float32, size=time_steps)
initial_t = tf.placeholder(dtype='int32')
initial_m = tf.placeholder(dtype=tf.float32)

def should_continue(t, *args):
    return t < time_steps

def iteration(t, m, outputs_):
    cur = tf.gather(inputs, t)
    m  = m * 0.5 + cur * 0.5
    outputs_ = outputs_.write(t, m)
    return t + 1, m, outputs_

t, m, outputs = tf.while_loop(should_continue, iteration,
                              [initial_t, initial_m, initial_outputs])

outputs = outputs.stack()
init = tf.global_variables_initializer()
sess.run([init])
print(sess.run([outputs],
               feed_dict={inputs: np.asarray([1, 1, 1, 1]), initial_t: 0,
                          initial_m: 0.}))
like image 177
Yaroslav Bulatov Avatar answered Sep 19 '22 10:09

Yaroslav Bulatov