Feeding a tensorflow placeholder from an array - tensorflow

I'm trying to train CatPole-v0 using Q learning. When trying to update the replay buffer with experience I am getting the following error:
ValueError: Cannot feed value of shape (128,) for Tensor 'Placeholder_1:0', which has shape '(?, 2)'
The related code snippet is:
def update_replay_buffer(replay_buffer, state, action, reward, next_state, done, action_dim):
# append to buffer
experience = (state, action, reward, next_state, done)
replay_buffer.append(experience)
# Ensure replay_buffer doesn't grow larger than REPLAY_SIZE
if len(replay_buffer) > REPLAY_SIZE:
replay_buffer.pop(0)
return None
The placeholder to be fed is
action_in = tf.placeholder("float", [None, action_dim])
Can someone clarify how action_dim should be used to resolve this error?

Let's start by action_in:
action_in = tf.placeholder("float", [None, action_dim])
This means that action_in can have shape like (None, action_dim), nothing other than that. And from the error:
ValueError: Cannot feed value of shape (128,) for Tensor 'Placeholder_1:0', which has shape '(?, 2)'
From error it seems your action_dim is 2.
its easy to see that you're placing an object of shape (128,) in place of tensor which expects shape like (?, 2) i.e (None, 2).
So you need to check your feed_dict that's where you're messing. Your placeholder action_in's dimensions should match with object you're putting in feed_dict.
Can someone clarify how action_dim should be used to resolve this
error?
Seems your environment's action has two components from value of action_dim, but you are providing only one component, this is inferred from your error((128,)). You need to fix this. Hope this helps.

Related

Issue with feeding value into placeholder tensor for sess.run()

I want to get the value of an intermediate tensor in a convolutional neural network for a specific input. I know how to do this in keras and even though I have trained a model using keras, I'm going to move towards constructing and training the model using only tensorflow. Therefore, I want to move away from something like K.function(input_layer, output_layer) which is fairly simple, and instead use tensorflow. I believe I should use placeholder values, like the following approach:
with tf.compat.v1.Session(graph=tf.Graph()) as sess:
loaded_model = tf.keras.models.load_model(filepath)
graph = tf.compat.v1.get_default_graph()
images = tf.compat.v1.placeholder(tf.float32, shape=(None, 28, 28, 1)) # To specify input at MNIST images
output_tensor = graph.get_tensor_by_name(tensor_name) # tensor_name is 'dense_1/MatMul:0'
output = sess.run([output_tensor], feed_dict={images: x_test[0:1]}) # x_test[0:1] is of shape (1, 28, 28, 1)
print(output)
However, I get the following error message for the sess.run() line: Invalid argument: You must feed a value for placeholder tensor 'conv2d_2_input' with dtype float and shape [?,28,28,1]. I am unsure why I get this message because the image used for feed_dict is of type float and is what I believe to be the correct shape. Any help would be suggested.
You must use the input tensor from the Keras model, not make your own new placeholder, which would be disconnected from the rest of the model:
with tf.Graph().as_default(), tf.compat.v1.Session() as sess:
# Load model
loaded_model = tf.keras.models.load_model(filepath)
# Take model input tensor
images = loaded_model.input
# Take output of the second layer (index 1)
output_tensor = loaded_model.layers[1].output
# Evaluate
output = sess.run(output_tensor, feed_dict={images: x_test[0:1]})
print(output)

Keras / Tensorflow : "You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?, 600, 451, 3]"

I have this CNN I'm working on.
Input shape is dynamic, but I fixed it to [?, 600, 451, 3] (batch_size, height, width, channels) so that I can debug it.
I have a random batch generator I created:
test = random_batch_generator(z_train
, num_processes=12
, num_batch=steps_train
, preloaded_batch=100
, batch_size=batch_size
, chunk_size=batch_size
, dataaugmfunc=heavy_dataaugm
, seq=seq
, initial_dim=initial_dim
, min_overlap=MINOVERLAP
)
When I do:
next(test)[0].shape
or
next(test)[0].dtype
it outputs me the correct shape ([?, 600, 451, 3]) and dtype (float32), which is in theory required for my input. I also checked the content of the batches, it seems good.
Still, I got, when I train my model with the following:
model.fit_generator(
random_batch_generator(z_train (...)),
validation_data= (x_val_mem,y_val_mem),
steps_per_epoch=steps_train,
validation_steps=steps_val,
epochs=epochs
,callbacks=model_callbacks(modelname)
,class_weight = [0.005,0.995]
)
this error message:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?,600,451,3]
[[Node: input_1 = Placeholderdtype=DT_FLOAT, shape=[?,600,451,3], _device="/job:localhost/replica:0/task:0/device:GPU:0"]]
What am I doing wrong? Thanks a thousand for any help or intuition on this.
Are you using a TensorBoard callback? If so, you could try adding this before creating the model
import keras.backend as K
K.clear_session()
See this answer
most of the time this problem occures because of unused inputes(on your fit generator) are added to your network. try to avoid or put a comment on unused inputes in your network and try it again.if number of inputs for the model and number of inputs to batch generator or fit() function are not balanced this problem will happen.
before all you have to reset your session
*
import keras.backend as K
K.clear_session()
Not sure this is the cause, but something is not compatible with the validation data.
If you have the validation data as arrays, you pass it as validation_data=(array_x, array_y), and there aren't validation_steps.
Now, if it's a generator, then you need to pass it as validation_data = someGenerator, then you pass validation_steps=number_of_batches_expected_from_generator.
This happened to me (TF 1.14) when I set 'histogram_freq = 1' instead of 0.

Tensorflow cannot initialize tf.Variable for dynamic batch size

I tried creating a tf.Variable with a dynamic shape. The following outlines the problem.
Doing this works.
init_bias = tf.random_uniform(shape=[self.config.hidden_layer_size, tf.shape(self.question_inputs)[0]])
However, when i try to do this:
init_bias = tf.Variable(init_bias)
It throws the error ValueError: initial_value must have a shape specified: Tensor("random_uniform:0", shape=(?, ?), dtype=float32)
Just come context (question input is a placeholder which dynamic batch ):
self.question_inputs = tf.placeholder(tf.int32, shape=[None, self.config.qmax])
It seems like putting a dynamic value into random uniform gives shape=(?,?) which gives an error with tf.Variable.
Thanks and appreciate any help!
This should work:
init_bias = tf.Variable(init_bias,validate_shape=False)
If validate_shape is False, tensorflow allows the variable to be initialized with a value of unknown shape.
However, what you're doing seems a little strange to me. In tensorflow, Variables are generally used to store weights of a neural net, whose shape remains fixed irrespective of the batch size. Variable batch size is handled by passing a variable length tensor into the graph (and multiplying/adding it with a fixed shape bias Variable).

Cannot interpret feed_dict key as Tensor

I have a list of placeholders called "enqueue_ops" and a list of methods called "feed_fns", each of which returns a feed_dict.
The queue runner of my graph is defined as:
queue_runner = feeding_queue_runner.FeedingQueueRunner(
queue=queue, enqueue_ops=enqueue_ops,
feed_fns=feed_fns)
However I got an error of
TypeError: Cannot interpret feed_dict key as Tensor: The name 'face_detection/x1' refers to an Operation, not a Tensor. Tensor names must be of the form "<op_name>:<output_index>".
But why are they looking at my feed_dict keys, while my feed_dict values are tensors that they don't want to look at?
Thanks!!!
In tensorflow if you want to restore a graph and use it, before saving the graph you should give your desired variables, placeholders, operations etc a unique name.
For an example see below.
W = tf.Variable(0.1, name='W')
X = tf.placeholder(tf.float32, (None, 2), name='X')
mult = tf.multiply(W,X,name='mult')
Then, once the graph is saved, you could restore and use it as follows. Remember to bundle your tensors with quotation marks. And if you are finding a value of a tensor, add :0 at the end of the tensor name as tensorflow requires it to be in "op_name:output_index" format.
with tf.Session() as sess:
new_saver = tf.train.import_meta_graph('your_model.meta')
new_saver.restore(sess, tf.train.latest_checkpoint('./'))
print(sess.run('mult:0', feed_dict={'X:0': [[1,4],[2,9]]}))

Why can't tensorflow determine the shape of this expression?

I have the following expression which is giving me problems. I have defined the batch_size as batch_size = tf.shape(input_tensor)[0] which dynamically determines the size of the batch based on the size of the input tensor to the model. I have used it elsewhere in the code without issue. What I am confused about is that when I run the following line of code it says the shape is (?, ?) I would expect it to be (?, 128) because it knows the second dimension.
print(tf.zeros((batch_size, 128)).get_shape())
I want to know the shape since I am trying to do the following and I am getting an error.
rnn_input = tf.reduce_sum(w * decoder_input, 1)
last_out = decoder_outputs[t - 1] if t else tf.zeros((batch_size, 128))
rnn_input = tf.concat(1, (rnn_input, last_out))
This code needs to set last_out to zero on the first time step.
Here is the error ValueError: Linear expects shape[1] of arguments: [[None, None], [None, 1024]]
I am doing something similar when I determine my initial state vector for the RNNs.
state = tf.zeros((batch_size, decoder_multi_rnn.state_size), tf.float32)
I also get (?, ?) when I try to print the size of state but it does not really throw any exceptions when I try to use it.
You are mixing static shapes and dynamic shapes. Static shape is what you get during tensor.get_shape(tensor) which is best-effort attempt to obtain shape, while dynamic shape comes from sess.run(tf.shape(tensor)) and it is always defined.
To be more precise, tf.shape(tensor) creates an op in the graph that will produce shape tensor on run call. If you do aop=tf.shape(tensor)[0], there's some magic through _SliceHelper that adds extra ops that will extract first element of the shape tensor on run call.
This means that myval=tf.zeros((aop, 128)) has to run aop to obtain the dimensions and this means that first dimension of myval is undefined until you issue the run call. IE, your run call could look like sess.run(myval, feed_dict={aop:2}, where feed_dict overrides aop with 2. Hence static shape inference reports ? for that dimension.
(EDIT: I rewrite an answer as what I wrote before was not up to the point)
The quick fix to your issue is to use set_shape() to update the static (inferred) shape of the Tensor:
input_tensor = tf.placeholder(tf.float32, [None, 32])
batch_size = tf.shape(input_tensor)[0]
res = tf.zeros((batch_size, 128))
print res.get_shape() # prints (?, ?) WHEREAS one could expect (?, 128)
res.set_shape([None, 128])
print res.get_shape() # prints (?, 128)
As for why TensorFlow looses the information about the second dimension being 128, I don't really know.
Maybe #Yaroslav will be able to answer.
EDIT:
The incorrect behavior was corrected following this issue.