TransWikia.com

Tensorflow neural network TypeError: Fetch argument has invalid type

Data Science Asked by Hasse Iona on December 9, 2020

I am making a simple neural network using tensorflow, with data I collected myself, however, it’s not cooperating 😛 I have run into an error that I can’t fix or find the fix for and I would love your help.

The errormessage:

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

The error refers to the following line in my code:

_, cost = tf_session.run([optimizer, cost], feed_dict = {champion_data: batch_input, item_data: batch_output})

I have already figured out that the error doesn’t occur when I comment out the following lines in my code:

prediction = neural_network_model(champion_data)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, item_data))
optimizer = tf.train.AdamOptimizer().minimize(cost)
_, cost = tf_session.run([optimizer, cost], feed_dict = {champion_data: batch_input, item_data: batch_output})

So somewhere one of these lines is getting something that doesn’t look exactly like it expects it to look. I have already tried the obvious (removing the np.array() from batch_input and batch_output or replacing it by list()) but that doesn’t solve the problem. My current hypothesis is that the output of neural_network_model(champion_data) is somehow of the wrong shape or type, however I’m not sure how to test that or how to solve it if that turns out to be the case.

The full code can be found here.

Edit: I have verified that the champion data that are input into the neural_network_model, the prediction and the cost are all tensors. I’ve been trying to solve the problem using the hypothesis that the problem somehow lies in the feed_dict = {} part of the code, but not getting anywhere so far

One Answer

The problem lay in using the name 'cost' on two occasions, the problem was solved by changing this:

_, cost = tf_session.run([optimizer, cost], feed_dict = {champion_data: batch_input, item_data: batch_output})

to this:

_, c = tf_session.run([optimizer, cost], feed_dict = {champion_data: batch_input, item_data: batch_output})

This way the name of the variable 'c' doesn't clash anymore with the [optimizer, cost] part of the code.

Correct answer by Hasse Iona on December 9, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP