tensorflow 遇见的错误

这个bolg主要讲自己使用tensorflow踩过得坑
(1)

TypeError: The value of a feed cannot be a tf.Tensor object. Acceptable feed values include Python scalars, strings, lists, numpy ndarrays, or TensorHandles.

产生以上错误的代码为:

xs,ys = mnist.train.next_batch(batch_size)
xs = tf.reshape((batch_size,time_steps,input_x))
_,tmp_loss = sess.run([optimizer,loss],feed_dict={x:xs,y:ys})

这是因为如果我们用tensorflow里面的reshape,那么这相当于一个计算图的节点,是不能feed的,改为以下:

xs,ys = mnist.train.next_batch(batch_size)
xs = xs.reshape((batch_size,time_steps,input_x))
 _,tmp_loss = sess.run([optimizer,loss],feed_dict={x:xs,y:ys})

(2)

TypeError: Fetch argument 2.8452306 has invalid type <class 'numpy.float32'>, must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)

产生错误的代码:

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_pred,labels=y),name='loss')
_,loss = sess.run([optimizer,loss],feed_dict={x:xs,y:ys})

这是因为你的loss定义的节点和你run以后的loss重名了,只需要改下名字就可以,比如可以以下这样:

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_pred,labels=y),name='loss')
_,tmp_loss = sess.run([optimizer,loss],feed_dict={x:xs,y:ys})
阅读更多
换一批

没有更多推荐了,返回首页