我如何在keras中的自定义损失中从张量y_pred和y_true提取数组

时间:2018-11-21 19:07:02

标签: python python-3.x keras

我正在用Keras创建一个神经网络,并在编译函数中编写了自定义损失函数。在这里,我需要调用其他具有numpy数组作为输入的函数。我正在使用K.eval()完成此操作,但遇到了很多问题。我在此处附加了代码和错误。

这是我的自定义损失:

def custom_objective (y_true, y_pred):
    n = 10

    outpar=K.eval(y_pred)
    alabel=K.eval(y_true)

    a_true = alabel[0:-5]
    labelloss = alabel[-5:]

    note = note_detector(a_true , labelloss[4] , True)
    a_pred , sr = fluteinput (outpar , frequency=note)
    e = HMSEN(a_true , a_pred , sr , n)

return e

在此处调用此函数:

opt = Adam (lr=learn_rate , beta_1=0.9 , beta_2=0.999 , epsilon=1e-08 , decay=decay)

model.compile(loss=custom_objective, optimizer=opt)

错误:

2018-11-21 12:55:38.922393: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-11-21 12:55:39.619330: W tensorflow/core/framework/allocator.cc:122] Allocation of 5360844800 exceeds 10% of system memory.
Traceback (most recent call last):
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call
    return fn(*args)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'conv2d_input' with dtype float and shape [?,2049,44,1]
     [[{{node conv2d_input}} = Placeholder[dtype=DT_FLOAT, shape=[?,2049,44,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/snap/pycharm-community/95/helpers/pydev/pydevd.py", line 1664, in <module>
    main()
  File "/snap/pycharm-community/95/helpers/pydev/pydevd.py", line 1658, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/snap/pycharm-community/95/helpers/pydev/pydevd.py", line 1068, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/snap/pycharm-community/95/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/lorenzo/ProgettoTesi/Codice/main.py", line 513, in <module>
    model=CNN(X_train,Y_train)
  File "/home/lorenzo/ProgettoTesi/Codice/main.py", line 478, in CNN
    model.compile (loss=custom_objective, optimizer=opt)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py", line 474, in _method_wrapper
    method(self, *args, **kwargs)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 617, in compile
    output_loss = weighted_loss(y_true, y_pred, sample_weight, mask)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 598, in weighted
    score_array = fn(y_true, y_pred)
  File "/home/lorenzo/ProgettoTesi/Codice/main.py", line 363, in custom_objective
    outpar=K.eval(y_pred)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/keras/backend.py", line 1006, in eval
    return to_dense(x).eval(session=get_session())
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 713, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 5157, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1152, in _run
    feed_dict_tensor, options, run_metadata)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1328, in _do_run
    run_metadata)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1348, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'conv2d_input' with dtype float and shape [?,2049,44,1]
     [[node conv2d_input (defined at /home/lorenzo/ProgettoTesi/Codice/main.py:448)  = Placeholder[dtype=DT_FLOAT, shape=[?,2049,44,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

Caused by op 'conv2d_input', defined at:
  File "/snap/pycharm-community/95/helpers/pydev/pydevd.py", line 1664, in <module>
    main()
  File "/snap/pycharm-community/95/helpers/pydev/pydevd.py", line 1658, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/snap/pycharm-community/95/helpers/pydev/pydevd.py", line 1068, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/snap/pycharm-community/95/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/lorenzo/ProgettoTesi/Codice/main.py", line 513, in <module>
    model=CNN(X_train,Y_train)
  File "/home/lorenzo/ProgettoTesi/Codice/main.py", line 448, in CNN
    model.add (Conv2D(CNN_layerSize[i],  3, padding='valid' ,data_format='channels_last', input_shape=(row_train , col_train, 1))) #, init='normal', strides?
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py", line 474, in _method_wrapper
    method(self, *args, **kwargs)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 155, in add
    name=layer.name + '_input')
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/input_layer.py", line 229, in Input
    input_tensor=tensor)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/input_layer.py", line 112, in __init__
    name=self.name)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1747, in placeholder
    return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 5206, in placeholder
    "Placeholder", dtype=dtype, shape=shape, name=name)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3274, in create_op
    op_def=op_def)
  File "/home/lorenzo/ProgettoTesi/venv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1770, in __init__
    self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'conv2d_input' with dtype float and shape [?,2049,44,1]
     [[node conv2d_input (defined at /home/lorenzo/ProgettoTesi/Codice/main.py:448)  = Placeholder[dtype=DT_FLOAT, shape=[?,2049,44,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]


Process finished with exit code 1
  • 我可以使用K.eval()吗?如果没有,我应该用什么来实现这一目标?
  • 为什么我会有关于CPU和内存的警告?该代码只会编译到model.compile为止,尚无任何培训。

谢谢。

0 个答案:

没有答案