学习TensorFlow的过程中,调用matplotlib库画图,结果出现了ValueError: x and y must be the same size错误
源代码显示如下:
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
x=[float(1) for x in range(2)]
y=[float(2) for x in range(2)]
x_data=np.linspace(-1,1,300,dtype='float32')[:,np.newaxis]
noise=np.random.normal(0,0.05,x_data.shape)
y_data=tf.square(x_data)-0.5+noise
fig =plt.figure()
ax=fig.add_subplot(1,1,1)
ax.scatter(x_data,y_data)
plt.show()
出现错误:
raise ValueError("x and y must be the same size")
ValueError: x and y must be the same size
解决方式:
将y_data=tf.square(x_data)-0.5+noise换成numpy的开方
即y_data=np.square(x_data)-0.5+noise
原因:在执行下行语句时,显示tensor没有size属性,而x_data的size则可以打印出来
print(x_data.size,y_data.size)
AttributeError: 'Tensor' object has no attribute 'size'