TensorFlow 错误ValueError: x and y must be the same size

学习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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值