tensorflow中feed_dict相關問題
阿新 • • 發佈:2018-12-12
問題說明:
對於image_batch進行feed_dict,結果一直報錯:
TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.For reference, the tensor object was Tensor("shuffle_batch_1:1", shape=(3, 5), dtype=int32) which was passed to the feed with key Tensor("y-input_1:0", shape=(?, 5), dtype=float32).
問題分析:
feed_dict不能喂入tf張量,可以喂入陣列,字串,列表等
而喂入的image_batch經過了tf.reshape,變成了tf張量。
解決方法:
將tf.Tensor變成陣列即可。
陣列與張量的互相轉換:
import tensorflow as tf
sess=tf.Session()
sess.run(tf.global_variables_initializer())
#轉化為numpy陣列
img_numpy=img.eval(session=sess)
print("out2=",type(img_numpy))
#轉化為tensor
img_tensor= tf.convert_to_tensor(img_numpy)
print("out2=",type(img_tensor))