2

The following code results in a very unhelpful error:

import tensorflow as tf

x = tf.Variable(tf.constant(0.), name="x")
with tf.Session() as s:
    val = s.run(x.assign(1))
    print(val)  # 1
    val = s.run(x, {x: 2})
    print(val)  # 2
    val = s.run(x.assign(1), {x: 0.})   # InvalidArgumentError

tensorflow.python.framework.errors_impl.InvalidArgumentError: Input 0 of node Assign_1 was passed float from _arg_x_0_0:0 incompatible with expected float_ref.


How did I get this error?
Why do I get this error?

1
  • Hi, Did you fix the issue? Commented Jul 5, 2018 at 8:23

1 Answer 1

2

Here's what I could infer.

How did I get this error?
This error is seen when attempting to perform the following two operations in a single session run:

  1. A Tensorflow variable is assigned a value
  2. That same variable is also passed a value as part of the feed_dict

This is why the first 2 runs succeed (they both don't simultaneously attempt to perform both these operations).

Why do I get this error?
I am not sure, but I don't think this was an intentional design choice by Google. Here's my explanation:

Firstly, the TF(TensorFlow) source code (basically) resolves x.assign(1) to tf.assign(x, 1) which gives us a hint for better understand the error message when it says Input 0.
The error message refers to x when it says Input 0 of the assign op. It goes on to say that the first argument of the assign op was passed float from _arg_x_0_0:0.

TLDR
Thus for a run where a TF variable is provided as a feed, that variable will no longer be treated as a variable (but instead as the value it was assigned), and thus any attempts at further assigning a value to it would be erroneous since only TF variables can be assigned a value in the graph.

Fix

If your graph has variable assignment operation, don't pass a value to that same variable in your feed_dict. ¯_(ツ)_/¯. Assuming you're using the feed_dict to provide an initial value, you could instead assign it a value in a prior session run. Or, leverage tf.control_dependencies when building your graph to assign it an initial value from a placeholder as shown below:

import tensorflow as tf

x = tf.Variable(tf.constant(0.), name="x")

initial_x = tf.placeholder(tf.float32)
assign_from_placeholder = x.assign(initial_x)
with tf.control_dependencies([assign_from_placeholder]):
    x_assign = x.assign(1)

with tf.Session() as s:
    val = s.run(x_assign, {initial_x: 0.})  # Success!
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, how to fix the issue?
The solution is edited to explicitly specify the fix.
Do you know what does the error mean if we don't have assignment operation?
Sorry, not sure. Even though you didn't create an assignment operator explicitly, It's likely created by something like the update step of the optimizer. You're probably additionally passing a value to that variable through feed_dict.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.