【问题标题】:DNN Linear Regression. MAE measurement errorDNN 线性回归。 MAE 测量误差
【发布时间】:2018-04-01 06:22:11
【问题描述】:

我正在尝试将 MAE 实现为我的 DNN 回归模型的性能测量。我正在使用 DNN 来预测 facebook 帖子将获得的 cmets 数量。据我了解,如果是分类问题,那么我们使用准确性。如果是回归问题,那么我们使用 RMSE 或 MAE。我的代码如下:

with tf.name_scope("eval"):
    correct = tf.metrics.mean_absolute_error(labels = y, predictions = logits)
    mae = tf.reduce_mean(tf.cast(correct, tf.int64))
    mae_summary = tf.summary.scalar('mae', accuracy)

由于某种原因,我收到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-396-313ddf858626> in <module>()
      1 with tf.name_scope("eval"):
----> 2     correct = tf.metrics.mean_absolute_error(labels = y, predictions = logits)
      3     mae = tf.reduce_mean(tf.cast(correct, tf.int64))
      4     mae_summary = tf.summary.scalar('mae', accuracy)

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/metrics_impl.py in mean_absolute_error(labels, predictions, weights, metrics_collections, updates_collections, name)
    736   predictions, labels, weights = _remove_squeezable_dimensions(
    737       predictions=predictions, labels=labels, weights=weights)
--> 738   absolute_errors = math_ops.abs(predictions - labels)
    739   return mean(absolute_errors, weights, metrics_collections,
    740               updates_collections, name or 'mean_absolute_error')

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py in binary_op_wrapper(x, y)
    883       if not isinstance(y, sparse_tensor.SparseTensor):
    884         try:
--> 885           y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")
    886         except TypeError:
    887           # If the RHS is not a tensor, it might be a tensor aware object

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, preferred_dtype)
    834       name=name,
    835       preferred_dtype=preferred_dtype,
--> 836       as_ref=False)
    837 
    838 

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
    924 
    925     if ret is None:
--> 926       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    927 
    928     if ret is NotImplemented:

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
    772     raise ValueError(
    773         "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
--> 774         (dtype.name, t.dtype.name, str(t)))
    775   return t
    776 

ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int64: 'Tensor("eval_9/remove_squeezable_dimensions/cond_1/Merge:0", dtype=int64)'

【问题讨论】:

  • 所以,我改变了: y = tf.placeholder(tf.int64, shape=(None), name="y") 到 y = tf.placeholder(tf.float32, shape=(None ), name="y") 并且问题消失了。但是,这是正确的解决方案吗?

标签: python-3.x tensorflow neural-network


【解决方案1】:

代码中的这一行:

correct = tf.metrics.mean_absolute_error(labels = y, predictions = logits)

以 TensorFlow 首先从标签中减去预测的方式执行,如 backrace 所示:

absolute_errors = math_ops.abs(predictions - labels)

为了做减法,两个张量需要是相同的数据类型。大概您的预测(logits)是 float32 并且从错误消息中您的标签是 int64。您要么必须使用 tf.to_float 进行显式转换,要么使用您在评论中建议的隐式转换:首先将占位符定义为 float32,并信任 TensorFlow 在处理提要字典时进行转换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-09
    • 2013-08-20
    • 1970-01-01
    • 2018-04-02
    • 2021-10-16
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多