TypeError: Fetch argument 0.484375 has invalid type , must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)


報錯:

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

出錯代碼:

1 _, summaries, acc, loss = sess.run([train_step, train_summary_op, acc, cost], feed_dict={cnn.input_x1: x1, cnn.input_x2: x2, cnn.input_y: y, cnn.dropout_keep_prob: 0.5})
2 time_str = datetime.datetime.now().isoformat()
3 print("{}: loss {:g}, acc {:g}".format(time_str, loss, acc))

 

原因:

  代碼迭代了n次,第1次迭代可以打印出acc的值,第2次迭代就開始報錯誤。主要是因為在同一個session里面用新的acc取代了原來的acc, 第1次迭代時,兩個變量都是原始數據,數據類型均為Tensor。但run之后,原來的acc就被新的acc取代了,此時兩個變量的數據類型都變成了numpy.float32,所有第2次迭代的時候就會因為數據類型不是Tensor而報錯。

解決方法:


避免重名引起數據類型輸送錯誤,最好使用不同的命名。

1 _, summaries, accuracy, loss = sess.run([train_step, train_summary_op, acc, cost], feed_dict={cnn.input_x1: x1, cnn.input_x2: x2, cnn.input_y: y, cnn.dropout_keep_prob: 0.5})
2 time_str = datetime.datetime.now().isoformat()
3 print("{}: loss {:g}, acc {:g}".format(time_str, loss, accuracy))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM