Tag Archives: Tensorflow Error TypeError

Tensorflow Error TypeError: Fetch argument XXXX has invalid typeXXXX,must be a string or Tensor

When training a small model today, this error occurred:


TypeError: Fetch argument 2.19779 has invalid type <class 'numpy.float32'>, 
must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)

In the output, you can see that there is a line of correct output, that is, the Loss calculation is performed once, and then this error is thrown during the second calculation. In the error message, 2.19779 happens to be the result of the first Loss calculation.

Found the same problem in stackoverflow (see TypeError: Fetch argument has invalid type float32, must be a string or Tensor
).
In fact, the reason for the error is very simple. My previous execution statement was like this:


_, loss = sess.run([optimizer, loss], feed_dict={X:input_batch, Y:target_batch})

The problem is that loss is redefined ! ! , Which is in this form:


loss = sess.run(loss)

In this way, the first run is no problem. If the result of the first calculation of loss is 10, then the loss is redefined, that is, the previous loss is the node for calculating the loss value. After the first run, it becomes A common variable is also assigned a value of 10, so the second operation becomes:


loss = sess.run(10)

The above type error occurred.
Therefore, the question of naming must not be sloppy, remember to remember.