TypeError: must be real number, not Tensor
来源:2-7 神经元实现(二分类逻辑斯蒂回归模型实现)
慕前端9340651
2021-11-09
老师您好,我在跑以下方程时遇到 “TypeError: must be real number, not Tensor” 的问题 我现在tensorflow的版本是1.13, python版本是3. 您知道该如何解决这个问题吗?
我的代码为:
init = tf.global_variables_initializer()
batch_size = 20
train_steps = 1000
with tf.Session() as sess:
sess.run(init)
for i in range(0, train_steps):
batch_data, batch_lables = train_data.next_batch(batch_size)
loss_val, accur_val, _ = sess.run([loss, accuracy, train_op],
feed_dict = {x: batch_data, y: batch_lables})
if i % 500 == 0:
print ('[Train] Step: %d, loss: %4.5f, acc: %4.5f' \
% (i, loss_val, accuracy))
以下是报错信息
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/ht/s_lk_n7j245dpr0sk715tk500000gn/T/ipykernel_47765/1309685021.py in <module>
12 if i % 500 == 0:
13 print ('[Train] Step: %d, loss: %4.5f, acc: %4.5f' \
---> 14 % (i, loss_val, accuracy))
TypeError: must be real number, not Tensor
写回答
1回答
-
正十七
2021-11-15
同学你好,
print的时候,你应该用你代码里sess.run输出的accur_val,accuracy是tensor,是图的一部分,是无法print的。而accur_val则是当前输入下accuracy的值。
00
相似问题