【问题标题】:regarding the ValueError: If `inputs` don't all have same shape and dtype or the shape关于 ValueError:如果 `inputs` 的形状和 dtype 或形状不同
【发布时间】:2017-08-27 14:17:09
【问题描述】:

有一个程序定义损失函数如下:

def loss(hypes, decoded_logits, labels):
"""Calculate the loss from the logits and the labels.

Args:
  logits: Logits tensor, float - [batch_size, NUM_CLASSES].
  labels: Labels tensor, int32 - [batch_size].

Returns:
  loss: Loss tensor of type float.
"""
logits = decoded_logits['logits']
with tf.name_scope('loss'):
    logits = tf.reshape(logits, (-1, 2))
    shape = [logits.get_shape()[0], 2]
    epsilon = tf.constant(value=hypes['solver']['epsilon'])
    # logits = logits + epsilon
    labels = tf.to_float(tf.reshape(labels, (-1, 2)))

    softmax = tf.nn.softmax(logits) + epsilon

    if hypes['loss'] == 'xentropy':
        cross_entropy_mean = _compute_cross_entropy_mean(hypes, labels,
                                                         softmax)
    elif hypes['loss'] == 'softF1':
        cross_entropy_mean = _compute_f1(hypes, labels, softmax, epsilon)

    elif hypes['loss'] == 'softIU':
        cross_entropy_mean = _compute_soft_ui(hypes, labels, softmax,
                                              epsilon)



    reg_loss_col = tf.GraphKeys.REGULARIZATION_LOSSES

    print('******'*10)
    print('loss type ',hypes['loss'])
    print('type ', type(tf.get_collection(reg_loss_col)))
    print( "Regression loss collection: {}".format(tf.get_collection(reg_loss_col)))
    print('******'*10)


    weight_loss = tf.add_n(tf.get_collection(reg_loss_col))

    total_loss = cross_entropy_mean + weight_loss

    losses = {}
    losses['total_loss'] = total_loss
    losses['xentropy'] = cross_entropy_mean
    losses['weight_loss'] = weight_loss

return losses

运行程序会引发以下错误消息

File "/home/ decoder/kitti_multiloss.py", line 86, in loss
    name='reg_loss')
  File "/devl /tensorflow/tf_0.12/lib/python3.4/site-packages/tensorflow/python/ops/math_ops.py", line 1827, in add_n
    raise ValueError("inputs must be a list of at least one Tensor with the "
ValueError: inputs must be a list of at least one Tensor with the same dtype and shape

我查看了tf.add_n的功能,其实现如下。我的问题是如何检查tf.add_n中的第一个参数tf.get_collection(reg_loss_col)并打印其信息以找出错误消息产生的原因?

def add_n(inputs, name=None):
  """Adds all input tensors element-wise.
  Args:
    inputs: A list of `Tensor` objects, each with same shape and type.
    name: A name for the operation (optional).
  Returns:
    A `Tensor` of same shape and type as the elements of `inputs`.
  Raises:
    ValueError: If `inputs` don't all have same shape and dtype or the shape
    cannot be inferred.
  """
  if not inputs or not isinstance(inputs, (list, tuple)):
    raise ValueError("inputs must be a list of at least one Tensor with the "
                     "same dtype and shape")
  inputs = ops.convert_n_to_tensor_or_indexed_slices(inputs)
  if not all(isinstance(x, ops.Tensor) for x in inputs):
    raise ValueError("inputs must be a list of at least one Tensor with the "
                     "same dtype and shape")

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    为什么你甚至需要进入add_n 才能看到tf.get_collection(reg_loss_col) 是什么?您可以拥有tmp = tf.get_collection(reg_loss_col),然后查看其类型。顺便说一句,看起来您的图表中没有任何正则化损失,在这种情况下,tf.get_collection(reg_loss_col) 将返回一个空列表。

    在 Python 中查看对象的类型可以使用内置函数type。例如查看tmp的类型:print type(tmp)

    【讨论】:

    • 嗨,阿里,感谢您的回复。哪个函数可以让我看到 tmp=tf.get_collection(reg_loss_col) 的类型?另外,在原程序中,有 reg_loss_col = tf.GraphKeys.REGULARIZATION_LOSSES 是不是表示正则化损失?
    • 更新了答案以显示对象的锄头检查类型。 tf.GraphKeys.REGULARIZATION_LOSSES 是一个字符串,一个名称,通过调用tf.get_collection(),您正在请求具有该名称的图形节点。您需要在图表中定义损失。
    • stackoverflow.com/questions/37107223/… 可以帮助您了解tf.GraphKeys.REGULARIZATION_LOSSES 是什么。
    • 嗨,阿里,我打印了 tf.get_collection(reg_loss_col) ,它显示为 [] ,正如您所料,这是一个空列表。我更新了原帖,加入了相关的函数,在我看来,cross_entropy_mean 已经被定义为损失了,这就是你说的损失吗?
    【解决方案2】:

    作为一种解决方法,您可以将此行替换为:

    temp = tf.get_collection('losses')
    
    if temp == []:
                temp = [0]
            weight_loss = tf.add_n(temp, name='total_loss')
    

    由于添加零值不会影响最终结果,但会有效运行软件...您怎么看?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-05
      • 2021-02-23
      • 1970-01-01
      • 2021-05-12
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多