Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算完张量后,如何将其显示为图像?_Python_Arrays_Image_Numpy_Tensorflow - Fatal编程技术网

Python 计算完张量后,如何将其显示为图像?

Python 计算完张量后,如何将其显示为图像?,python,arrays,image,numpy,tensorflow,Python,Arrays,Image,Numpy,Tensorflow,我有一维numpy数组。在TensorFlow中执行计算后,我得到一个tf.Tensor作为输出。我试图将其重塑为二维数组,并将其显示为图像 如果它是一个numpy Ndaray,我会知道如何将其绘制为图像。但现在它是张量了 尽管我试图将其转换为numpy数组,但还是出现了一个错误,说“没有默认会话” 有人能教我如何把张量表示成图像吗 ... ... init = tf.initialize_all_variables() sess = tf.Session() sess.run(ini

我有一维numpy数组。在TensorFlow中执行计算后,我得到一个
tf.Tensor
作为输出。我试图将其重塑为二维数组,并将其显示为图像

如果它是一个numpy Ndaray,我会知道如何将其绘制为图像。但现在它是张量了

尽管我试图将其转换为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()
... ...

您看到的直接错误是因为
Tensor.eval()
仅在出现错误时有效。这需要(i)使用tf.Session():块在
中执行,(ii)使用sess.as_default():
块在
中执行,或者(iii)使用

有两种简单的变通方法可以让您的案例发挥作用:

# Pass the session to eval().
ax.imshow(img_res.eval(session=sess))

# Use sess.run().
ax.imshow(sess.run(img_res))

注意,作为一个可视化图像的大点,你可以考虑使用OP来可视化更大的训练流水线产生的张量。你能发布你的尝试吗?请用良好的缩进更新问题。看,这样会更容易得到答案对不起,我不熟悉这一点。。非常感谢你。我是tensorflow的新手。试着学会使用它。非常感谢你的回答