Yee*_*Liu 6 python arrays numpy image tensorflow
我有一个一维的numpy数组。在TensorFlow中执行计算后,我得到一个tf.Tensor
as输出。我正在尝试将其重塑为二维数组并将其显示为图像。
如果它是一个numpy ndarray,我会知道如何将其绘制为图像。但是现在是张量!
尽管我尝试tensor.eval()
将其转换为numpy数组,但出现错误消息“无默认会话”。
谁能教我如何将张量显示为图像?
... ...
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
# training
for i in range(1):
sess.run(train_step, feed_dict={x: x_data.T, y_: y_data.T})
# testing
probability = tf.argmax(y,1);
sess.run(probability, feed_dict={x: x_test.T})
#show result
img_res = tf.reshape(probability,[len_y,len_x])
fig, ax = plt.subplots(ncols = 1)
# It is the the following line that I do not know how to make it work...
ax.imshow(np.asarray(img_res.eval())) #how to plot a tensor ?#
plt.show()
... ...
Run Code Online (Sandbox Code Playgroud)
您看到的直接错误是因为Tensor.eval()
仅在存在“默认Session
”时才有效。这要求 (i) 您在with tf.Session():
块中执行,(ii) 您在with sess.as_default():
块中执行,或者 (iii) 您正在使用tf.InteractiveSession
.
有两种简单的解决方法可以使您的案例发挥作用:
# Pass the session to eval().
ax.imshow(img_res.eval(session=sess))
# Use sess.run().
ax.imshow(sess.run(img_res))
Run Code Online (Sandbox Code Playgroud)
请注意,作为可视化图像的一个要点,您可能会考虑使用该tf.image_summary()
操作和TensorBoard来可视化由更大的训练管道生成的张量。
归档时间: |
|
查看次数: |
4065 次 |
最近记录: |