tf. Variable has a dynamic shape from the input placeholder
I’m trying to build a network and my requirement is that the hidden cell shape should be based on the input shape so that the user can provide input of any length. What I want to do is:
import tensorflow as tf
n_hidden_1=100
input_ = tf.placeholder(name='input_data',shape=[None,None],dtype=tf.float32)
variable_data = tf. Variable(tf.random_normal([tf.shape(input_)[1], n_hidden_1]))
bias_ = tf. Variable(tf.random_normal([n_hidden_1]))
output = tf.matmul(input_,variable_data)+bias_
with tf. Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(output,feed_dict={input_:[[1,2,3,4,5]]}))
I want
variable_data = tf. Variable(tf.random_normal([tf.shape(input_)[1], n_hidden_1]))
A shape from an input placeholder, but it gives an error. That’s why I tried this method:
variable_data = tf. Variable(tf.random_normal([tf.shape(input_)[1], n_hidden_1]),validate_shape=False)
But I got another error :
InvalidArgumentError: You must feed a value for placeholder tensor 'input_data_7' with dtype float and shape [?,?]
[[Node: input_data_7 = Placeholder[dtype=DT_FLOAT, shape=[?,?], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
How to get the shape integer from the placeholder of a variable shape.
Solution
tf. Variables
can’t really have dynamic shapes because it doesn’t make sense for their purpose. In tensorflow, variables mean optimized model parameters, usually via SGD. During optimization, you usually assume that your cost function and its defined space will not change at each step. Failure to do so – especially in a random, unpredetermined way – leads to poorly defined optimization problems.
However, what you can do is have a variable whose (static) shape definition is postponed at runtime when the first sample appears. You’re almost done: you just need to provide the example to global_variables_initializer
and it initializes the variables to the correct shapes and values:
import tensorflow as tf
n_hidden_1=100
input_ = tf.placeholder(name='input_data',shape=[None,None],dtype=tf.float32)
variable_data = tf. Variable(tf.random_normal([tf.shape(input_)[1], n_hidden_1]), validate_shape=False)
bias_ = tf. Variable(tf.random_normal([n_hidden_1]))
output = tf.matmul(input_,variable_data)+bias_
with tf. Session() as sess:
sess.run(tf.global_variables_initializer(), <b>feed_dict={input_:[[1,2,3,4,5]]}</b>)
print(sess.run(output, feed_dict={input_:[[1,2,3,4,5]]}))