tensorflow.python.framework.errors_impl.InvalidArgumentError:无效的归约维数(2为具有2维的输入

时间:2019-02-03 16:41:19

标签: python tensorflow

我正在尝试训练具有2个输出的张量流模型,每个输出都有损失函数。一个叫做“ contour_segmentation”-可以很好地处理categorical_crossentropy损失,另一个叫做“边界”,为此我定义了自己的自定义损失函数。

我的自定义丢失看起来像这样:

def borders_loss_calc(y_true, y_pred):

    multi = tf.multiply(y_true, y_pred)
    sum_multi = tf.reduce_sum(multi)
    return sum_multi

y_predy_true均为[batch_size, 384*384*4]的形式。 (通常我使用batch_size=2,因为我认为这是很多数字)

很简单,所以我想-但是我遇到了真正的麻烦,无法在任何地方找到问题的答案。

我的相关实验:

  1. 当我在损失函数之外的数据上应用此简单代码时- 它在tensorflow中效果很好(显然在numpy中):

  2. 当我将损失定义为简单时:

    def borders_loss_calc(y_true, y_pred): sum_pred = tf.reduce_sum(y_pred) return sum_pred

    有效!

因此,可以定义损耗的方式和其中的功能都可以,但是它不能一起工作,并且很难在“ fit_generator”功能的执行中进行调试!

非常感谢尝试提供帮助的人!

完整的输出调试消息是:

2019-02-03 18:26:21.875161: W tensorflow/core/framework/op_kernel.cc:1273] OP_REQUIRES failed at reduction_ops_common.h:155 : Invalid argument: Invalid reduction dimension (2 for input with 2 dimension(s)
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pydev\pydevd.py", line 1741, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pydev\pydevd.py", line 1735, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pydev\pydevd.py", line 1135, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/work/code/segnet-contour/src/kobis_testing/new_testing/train_borders.py", line 179, in <module>
    tmh.train()
  File "C:/work/code/segnet-contour/src/kobis_testing/new_testing/train_borders.py", line 137, in train
    verbose=1, use_multiprocessing=False, callbacks=[tensorboard])
  File "C:\Users\kobih\Anaconda3\envs\python3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\kobih\Anaconda3\envs\python3\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
    initial_epoch=initial_epoch)
  File "C:\Users\kobih\Anaconda3\envs\python3\lib\site-packages\keras\engine\training_generator.py", line 217, in fit_generator
    class_weight=class_weight)
  File "C:\Users\kobih\Anaconda3\envs\python3\lib\site-packages\keras\engine\training.py", line 1217, in train_on_batch
    outputs = self.train_function(ins)
  File "C:\Users\kobih\Anaconda3\envs\python3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2715, in __call__
    return self._call(inputs)
  File "C:\Users\kobih\Anaconda3\envs\python3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2675, in _call
    fetched = self._callable_fn(*array_vals)
  File "C:\Users\kobih\Anaconda3\envs\python3\lib\site-packages\tensorflow\python\client\session.py", line 1439, in __call__
    run_metadata_ptr)
  File "C:\Users\kobih\Anaconda3\envs\python3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Invalid reduction dimension (2 for input with 2 dimension(s)
     [[{{node loss/borders_loss/Sum}} = Sum[T=DT_FLOAT, Tidx=DT_INT32, _class=["loc:@training/Adam/gradients/loss/borders_loss/Sum_grad/Tile"], keep_dims=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](loss/borders_loss/Mul, loss/borders_loss/Const)]]
     [[{{node loss/contour_segmentation_loss/Mean_2/_1647}} = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_12857_loss/contour_segmentation_loss/Mean_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

0 个答案:

没有答案