功能积分-Keras自定义损失

时间:2018-09-13 04:51:08

标签: python tensorflow keras

我正在尝试创建一个集成输入曲线的自定义损失函数。 曲线可以是多项式,指数和正弦曲线的混合。

输入具有以下形式:[x0,x1,....,x9]其中x0-x3是多项式系数,x4-x6是指数系数,x7-x9是正弦系数。

编辑:我可以编译以下内容,但我希望得到更精确的集成结果。

def integrate_loss(y_true, y_pred):
    x_range = K.arange(-5.0, 5.0, step=1, dtype='float32')

    y_true_pol = y_true[0]*x_range**3+y_true[1]*x_range**2+y_true[2]*x_range+y_true[3]
    y_pred_pol = y_pred[0]*x_range**3+y_pred[1]*x_range**2+y_pred[2]*x_range+y_pred[3]
    diff_pol = K.abs(K.sum(y_true_pol - y_pred_pol))

    y_true_exp = y_true[4]*K.exp(y_true[5]*(x_range-y_true[6]))
    y_pred_exp = y_pred[4]*K.exp(y_pred[5]*(x_range-y_pred[6]))
    diff_exp = K.abs(K.sum(y_true_exp - y_pred_exp))

    y_true_sin = y_true[7]*K.sin(y_true[8]*(x_range-y_true[9]))
    y_pred_sin = y_pred[7]*K.sin(y_pred[8]*(x_range-y_pred[9]))
    diff_sin = K.abs(K.sum(y_true_sin - y_pred_sin))

    area_between = diff_pol + diff_exp + diff_sin
    return area_between 

编辑:这会产生以下错误:

Epoch 1/150
 9/18 [==============>...............] - ETA: 1s - loss: 41.94232018-09-13 13:03:56.343324: W tensorflow/core/framework/op_kernel.cc:1158] Invalid argument: slice index 4 of dimension 0 out of bounds.
2018-09-13 13:03:56.343324: W tensorflow/core/framework/op_kernel.cc:1158] Invalid argument: slice index 2 of dimension 0 out of bounds.
2018-09-13 13:03:56.343324: W tensorflow/core/framework/op_kernel.cc:1158] Invalid argument: slice index 3 of dimension 0 out of bounds.
2018-09-13 13:03:56.343329: W tensorflow/core/framework/op_kernel.cc:1158] Invalid argument: slice index 6 of dimension 0 out of bounds.
2018-09-13 13:03:56.343331: W tensorflow/core/framework/op_kernel.cc:1158] Invalid argument: slice index 5 of dimension 0 out of bounds.
2018-09-13 13:03:56.343485: W tensorflow/core/framework/op_kernel.cc:1158] Invalid argument: slice index 8 of dimension 0 out of bounds.
2018-09-13 13:03:56.343556: W tensorflow/core/framework/op_kernel.cc:1158] Invalid argument: slice index 9 of dimension 0 out of bounds.
2018-09-13 13:03:56.343593: W tensorflow/core/framework/op_kernel.cc:1158] Invalid argument: slice index 7 of dimension 0 out of bounds.
2018-09-13 13:03:56.450277: W tensorflow/core/framework/op_kernel.cc:1158] Invalid argument: slice index 4 of dimension 0 out of bounds.
         [[Node: loss_1/dense_2_loss/strided_slice_8 = StridedSlice[Index=DT_INT32, T=DT_FLOAT, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/gpu:0"](_arg_dense_2_target_1_0_1/_243, loss_1/dense_2_loss/strided_slice_8/stack, loss_1/dense_2_loss/strided_slice_8/stack_1, loss_1/dense_2_loss/strided_slice_8/stack_2)]]
Traceback (most recent call last):
  File "training.py", line 152, in <module>
    verbose=1)
  File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 2230, in fit_generator
    class_weight=class_weight)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1883, in train_on_batch
    outputs = self.train_function(ins)
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2482, in __call__
    **self.session_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 789, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 997, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1132, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1152, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: slice index 4 of dimension 0 out of bounds.
         [[Node: loss_1/dense_2_loss/strided_slice_8 = StridedSlice[Index=DT_INT32, T=DT_FLOAT, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/gpu:0"](_arg_dense_2_target_1_0_1/_243, loss_1/dense_2_loss/strided_slice_8/stack, loss_1/dense_2_loss/strided_slice_8/stack_1, loss_1/dense_2_loss/strided_slice_8/stack_2)]]
         [[Node: loss_1/mul/_269 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_1800_loss_1/mul", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op u'loss_1/dense_2_loss/strided_slice_8', defined at:
  File "training.py", line 130, in <module>
    model.compile(optimizer=optimizers.Adam(lr=0.001), loss=integrate_loss)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 830, in compile
    sample_weight, mask)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 429, in weighted
    score_array = fn(y_true, y_pred)
  File "training.py", line 47, in integrate_loss
    y_true_exp = y_true[4]*K.exp(y_true[5]*(x_range-y_true[6]))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 499, in _SliceHelper
    name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 663, in strided_slice
    shrink_axis_mask=shrink_axis_mask)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3515, in strided_slice
    shrink_axis_mask=shrink_axis_mask, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): slice index 4 of dimension 0 out of bounds.
         [[Node: loss_1/dense_2_loss/strided_slice_8 = StridedSlice[Index=DT_INT32, T=DT_FLOAT, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/gpu:0"](_arg_dense_2_target_1_0_1/_243, loss_1/dense_2_loss/strided_slice_8/stack, loss_1/dense_2_loss/strided_slice_8/stack_1, loss_1/dense_2_loss/strided_slice_8/stack_2)]]
         [[Node: loss_1/mul/_269 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_1800_loss_1/mul", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 108, in worker
    debug('worker got sentinel -- exiting')
TypeError: 'NoneType' object is not callable

从上面可以看出,这会在随机点停止工作(我测试了很多次,有些工作正常,并根据需要减少了验证时间)。我还希望有一个更好的集成函数,因为目前它仅占用每个函数的高度(宽度为1),这会产生较大的错误。

任何建议将不胜感激。

0 个答案:

没有答案