在Keras中定义海关损失

时间:2018-06-28 00:53:17

标签: python tensorflow keras

我想使用支持tf的Keras定义自定义损失,看起来像这样-

def customLoss(yTrue, yPred):
    diff = K.square(yTrue - yPred)

    return K.dot(diff, K.constant([[16, 8, 4, 2, 1]])

我想对预测值和真实值之间的差异进行平方,每个值均假定为一维向量,并将向量中的每个元素提高为2的幂,如上所示(对于5个元素)。

运行时出现以下错误-

Traceback (most recent call last):
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 671, in _call_cpp_shape_fn_impl
    input_tensors_as_shapes, status)
  File "/home/user/anaconda3/lib/python3.6/contextlib.py", line 89, in __exit__
    next(self.gen)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 5 and 1 for 'MatMul' (op: 'MatMul') with input shapes: [?,5], [1,5].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/Desktop/hack/imlhack2018/nn_idea.py", line 64, in <module>
    main()
  File "/home/user/Desktop/hack/imlhack2018/nn_idea.py", line 44, in main
    model.compile(loss=customLoss, optimizer='adam')
  File "/home/user/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 840, in compile
    sample_weight, mask)
  File "/home/user/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 446, in weighted
    score_array = fn(y_true, y_pred)
  File "/home/user/Desktop/hack/imlhack2018/nn_idea.py", line 18, in customLoss
    return K.dot(diff, K.constant([[16, 8, 4, 2, 1]]))
  File "/home/user/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 978, in dot
    out = tf.matmul(x, y)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 1816, in matmul
    a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 1217, in _mat_mul
    transpose_b=transpose_b, name=name)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2508, in create_op
    set_shapes_for_outputs(ret)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1873, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1823, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
    debug_python_shape_fn, require_shape_fn)
  File "/home/user/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Dimensions must be equal, but are 5 and 1 for 'MatMul' (op: 'MatMul') with input shapes: [?,5], [1,5].

0 个答案:

没有答案