执行sess.run()

时间:2017-11-30 01:46:03

标签: python-3.x tensorflow conv-neural-network semantic-segmentation

我想执行我的图模型,但我遇到了困难。代码是:

epoch_x, epoch_y = features, labels
sess.run(optimizer, feed_dict = {"x:0": epoch_x, "y:0": epoch_y})

,错误是:

  

----------------------------------------------- ---------------------------- KeyError Traceback(最近一次调用   持续)   d:\ AnacondaIDE \ LIB \站点包\ tensorflow \ python的\客户端\ session.py   在_run中(self,handle,fetches,feed_dict,options,run_metadata)
  1067 subfeed_t = self.graph.as_graph_element(subfeed,   allow_tensor =真,    - > 1068 allow_operation = False)1069除了例外e:

     

D:\ AnacondaIDE \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py in   as_graph_element(self,obj,allow_tensor,allow_operation)2707
  与self._lock:    - > 2708返回self._as_graph_element_locked(obj,allow_tensor,allow_operation)2709

     

D:\ AnacondaIDE \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py in   _as_graph_element_locked(self,obj,allow_tensor,allow_operation)2749“存在。操作,%s,不   存在于“    - > 2750“图。” %(repr(name),repr(op_name)))2751尝试:

     

KeyError:“名称'x:0'指的是不存在的Tensor   操作'x',图中不存在。“

     

在处理上述异常期间,发生了另一个异常:

     

TypeError Traceback(最近一次调用   最后)in()        22#feed_dict = {x:epoch_x,y:epoch_y}        23   ---> 24 sess.run(optimizer,feed_dict = {“x:0”:epoch_x,“y:0”:epoch_y})        25 train_loss.append(sess.run(cost,feed_dict = {x:epoch_x,y:epoch_y}))        26 train_accuracy.append(sess.run(accr,feed_dict = {x:epoch_x,y:epoch_y}))

     

d:\ AnacondaIDE \ lib中\站点包\ tensorflow \蟒\客户\ session.py   在运行中(self,fetches,feed_dict,options,run_metadata)       893尝试:       894 result = self._run(None,fetches,feed_dict,options_ptr,    - > 895 run_metadata_ptr)       896如果run_metadata:       897 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

     

d:\ AnacondaIDE \ lib中\站点包\ tensorflow \蟒\客户\ session.py   在_run中(self,handle,fetches,feed_dict,options,run_metadata)
  1069除了例外e:1070加注   TypeError('无法将feed_dict键解释为Tensor:'    - > 1071 + e.args [0])1072 1073 if isinstance(subfeed_val,ops.Tensor):

     

TypeError:无法将feed_dict键解释为Tensor:名称'x:0'   是指不存在的张量。操作“x”没有   存在于图中。

我也尝试过以下声明:

sess.run(optimizer, feed_dict = {"x": epoch_x, "y": epoch_y})

然后错误是:

  

----------------------------------------------- ---------------------------- NameError Traceback(最近一次调用   最后)in()        22#feed_dict = {x:epoch_x,y:epoch_y}        23   ---> 24 sess.run(optimizer,feed_dict = {x:epoch_x,y:epoch_y})        25 train_loss.append(sess.run(cost,feed_dict = {x:epoch_x,y:epoch_y}))        26 train_accuracy.append(sess.run(accr,feed_dict = {x:epoch_x,y:epoch_y}))

     

NameError:名称'x'未定义

请注意print(features.shape)会产生:

  

(4000,6000,3)

我正在使用Tensorflow-gpu(1.3.0)。

1 个答案:

答案 0 :(得分:3)

在feed dict中应该没有引号,但键应该是指向你想要提供的占位符的python变量。

例如,如果在声明占位符时有类似

的内容
pl_ = tf.placeholder(...., name='placeholder_1')

然后你应该跑这个

sess.run(...., feed_dict={pl_: value})

而不是这个

sess.run(..., feed_dict={'placeholder_1': value})