问题描述
我正在训练一个与这个示例中的CNN非常相似的CNN,用于图像分割.图片大小为 1500x1500x1,标签大小相同.
I'm training a CNN quite similar to the one in this example, for image segmentation. The images are 1500x1500x1, and labels are of the same size.
定义 CNN 结构后,并在此代码示例中启动会话:(conv_net_test.py
)
After defining the CNN structure, and in launching the session as in this code sample: (conv_net_test.py
)
with tf.Session() as sess:
sess.run(init)
summ = tf.train.SummaryWriter('/tmp/logdir/', sess.graph_def)
step = 1
print ("import data, read from read_data_sets()...")
#Data defined by me, returns a DataSet object with testing and training images and labels for segmentation problem.
data = import_data_test.read_data_sets('Dataset')
# Keep training until reach max iterations
while step * batch_size < training_iters:
batch_x, batch_y = data.train.next_batch(batch_size)
print ("running backprop for step %d" % step)
batch_x = batch_x.reshape(batch_size, n_input, n_input, n_channels)
batch_y = batch_y.reshape(batch_size, n_input, n_input, n_channels)
batch_y = np.int64(batch_y)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, keep_prob: dropout})
if step % display_step == 0:
# Calculate batch loss and accuracy
#pdb.set_trace()
loss, acc = sess.run([loss, accuracy], feed_dict={x: batch_x, y: batch_y, keep_prob: 1.})
step += 1
print "Optimization Finished"
我遇到了以下类型错误(下面的堆栈跟踪):
I hit upon the following TypeError (stacktrace below):
conv_net_test.py in <module>()
178 #pdb.set_trace()
--> 179 loss, acc = sess.run([loss, accuracy], feed_dict={x: batch_x, y: batch_y, keep_prob: 1.})
180 step += 1
181 print "Optimization Finished!"
tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
370 try:
371 result = self._run(None, fetches, feed_dict, options_ptr,
--> 372 run_metadata_ptr)
373 if run_metadata:
374 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
582
583 # Validate and process fetches.
--> 584 processed_fetches = self._process_fetches(fetches)
585 unique_fetches = processed_fetches[0]
586 target_list = processed_fetches[1]
tensorflow/python/client/session.pyc in _process_fetches(self, fetches)
538 raise TypeError('Fetch argument %r of %r has invalid type %r, '
539 'must be a string or Tensor. (%s)'
--> 540 % (subfetch, fetch, type(subfetch), str(e)))
TypeError: Fetch argument 1.4415792e+2 of 1.4415792e+2 has invalid type <type 'numpy.float32'>, must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.)
我在这一点上被难住了.也许这是转换类型的一个简单案例,但我不确定如何/在哪里.另外,为什么损失必须是一个字符串?(假设同样的错误也会出现在准确性上,一旦这个问题被修复了).
I am stumped at this point. Maybe this is a simple case of converting the type, but I'm not sure how/where. Also, why does the loss have to be a string? (Assuming the same error will pop up for the accuracy as well, once this is fixed).
感谢任何帮助!
推荐答案
在你使用 loss = sess.run(loss)
的地方,你在 python 中重新定义了变量 loss
.
Where you use loss = sess.run(loss)
, you redefine in python the variable loss
.
第一次运行正常.第二次,你会尝试做:
The first time it will run fine. The second time, you will try to do:
sess.run(1.4415792e+2)
因为 loss
现在是一个浮点数.
Because loss
is now a float.
您应该使用不同的名称,例如:
You should use different names like:
loss_val, acc = sess.run([loss, accuracy], feed_dict={x: batch_x, y: batch_y, keep_prob: 1.})
这篇关于类型错误:获取参数的类型为无效的 float32,必须是字符串或张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!