所以,我正在 Build 一个完全卷积网络(FCN),基于Marvin Teichmann's tensorflow-fcn
我的输入图像数据暂时是750x750x3 RGB图像 . 在通过网络运行后,我使用shape [batch_size,750,750,2]的logits进行损失计算 .
这是一个二进制分类 - 我这里有2个类,[0,1]在我的标签中(形状[batch_sizex750x750] . 这些进入损失函数,如下:
def loss(logits, labels, num_classes):
with tf.name_scope('loss mine'):
logits = tf.to_float(tf.reshape(logits, [-1, num_classes]))
#CHANGE labels type to int, for sparse_softmax...
labels = tf.to_int64(tf.reshape(labels, [-1]))
print ('shape of logits: %s' % str(logits.get_shape()))
print ('shape of labels: %s' % str(labels.get_shape()))
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels, name='Cross_Entropy')
tf.add_to_collection('losses', cross_entropy)
loss = tf.add_n(tf.get_collection('losses'), name='total_loss')
return loss
这些是重塑后的logits和标签的形状:
shape of logits: (562500, 2)
shape of labels: (562500,)
在这里,它抛出一个ValueError说明:
Shapes () and (562500,) are not compatible
完整追溯如下:
File "train.py", line 89, in <module>
loss_train = loss.loss(logits, data.train.labels, 2)
File "/tensorflow-fcn/loss.py", line 86, in loss
loss = tf.add_n(tf.get_collection('losses'), name='total_loss')
File "/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 88, in add_n
result = _op_def_lib.apply_op("AddN", inputs=inputs, name=name)
File "/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 704, in apply_op
op_def=op_def)
File "/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2262, in create_op
set_shapes_for_outputs(ret)
File "/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1702, in set_shapes_for_outputs
shapes = shape_func(op)
File "/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 1557, in _AddNShape
merged_shape = merged_shape.merge_with(input_.get_shape())
File "/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 570, in merge_with
(self, other))
ValueError: Shapes () and (562500,) are not compatible
建议?我的 tf.add_to_collection('losses', cross_entropy)
执行错了吗?
更新:
我尝试在没有像素求和的情况下运行它(或者我认为),直接在上面的代码中返回 cross_entropy
作为丢失 .
它似乎有效 . (它现在从训练优化器函数抛出 ValueError
,说明: No gradients provided for any variable
. 假设这与我的权重初始化和正则化有关,而不是其他任何事情 .
更新2:
以上(关于由于没有梯度而导致的ValueError)是微不足道的 . 如上所述here,当定义的任何tf.Variable对象与正在最小化的丢失张量之间没有路径时,通常会遇到此消息 .
使用 tf.add_n
的初始问题仍然存在 . 我假设它与Graph集合在TensorFlow中的工作方式有关 . 初始化我的变量后,错误现在显示为:
Shapes () and (?,) are not compatible
1 回答
关闭 . 原来在损失函数中的代码缺少平均求和 . 对于遇到此问题的其他人,请修改如下所示的损失函数,它应该可以正常工作 .