带有 TensorFlow 后端的 Keras 的自定义损失函数问题
Issue on custom loss function with Keras with TensorFlow backend
我试图定义一个三classclass化问题的损失函数如下:
def func_loss(y_true, y_pred):
return -K.mean(K.prod(K.cast(K.argmax(y_pred, axis=1), K.floatx()) - 1.0, K.cast(K.argmax(y_true, axis=1), K.floatx()) - 1.0))
我的 y
看起来像这样:
[[1,0,0], [0,1,0], [1,0,0], [0,0,1], ...]
直觉上,我的三 class 标签是 classes“-1”、“0”和“+1”的单热编码。我想最大化“+/-1”的正确标签,最小化“+/-1”的错误标签,并忽略所有“0”标签,无论它们是否正确。
当我用这个损失函数编译模型时,这是我得到的:
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 547, in compile
**kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 622, in compile
sample_weight, mask)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 324, in weighted
score_array = fn(y_true, y_pred)
File "", line 2, in func_loss
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 464, in prod
axis = _normalize_axis(axis, ndim(x))
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 435, in _normalize_axis
if axis is not None and axis < 0:
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 547, in nonzero
raise TypeError("Using a tf.Tensor as a Python bool is not allowed. "
TypeError: Using a tf.Tensor as a Python bool is not allowed. Use if t is not None: instead of if t: to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.
我一直在尝试对这个损失函数进行一些小的调整,但是当我编译模型时,所有这些都会导致一些错误。我想我可能对这个东西的工作原理有一些基本的误解。有人可以帮忙吗?
编辑:
新的损失函数:
def func_loss(y_true, y_pred):
return -K.mean((K.cast(K.argmax(y_pred, axis=1), K.floatx()) - 1.0 )* (K.cast(K.argmax(y_true, axis=1), K.floatx()) - 1.0))
我也在 Tensorflow Github 上回复了你。
您已经编辑了损失函数,但实际上您现在得到的错误略有不同。
之前,错误发生在 K.mean
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 490, in mean
axis = _normalize_axis(axis, ndim(x))
现在,如您所见,它发生在 K.prod
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 464, in prod
axis = _normalize_axis(axis, ndim(x))
但是,原因仍然相同:K.mean
和 K.prod
各取一个 Tensor,因此您传递的第二个 Tensor
被视为 axis
参数。
我试图定义一个三classclass化问题的损失函数如下:
def func_loss(y_true, y_pred):
return -K.mean(K.prod(K.cast(K.argmax(y_pred, axis=1), K.floatx()) - 1.0, K.cast(K.argmax(y_true, axis=1), K.floatx()) - 1.0))
我的 y
看起来像这样:
[[1,0,0], [0,1,0], [1,0,0], [0,0,1], ...]
直觉上,我的三 class 标签是 classes“-1”、“0”和“+1”的单热编码。我想最大化“+/-1”的正确标签,最小化“+/-1”的错误标签,并忽略所有“0”标签,无论它们是否正确。
当我用这个损失函数编译模型时,这是我得到的:
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 547, in compile
**kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 622, in compile
sample_weight, mask)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 324, in weighted
score_array = fn(y_true, y_pred)
File "", line 2, in func_loss
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 464, in prod
axis = _normalize_axis(axis, ndim(x))
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 435, in _normalize_axis
if axis is not None and axis < 0:
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 547, in nonzero
raise TypeError("Using a tf.Tensor as a Python bool is not allowed. "
TypeError: Using a tf.Tensor as a Python bool is not allowed. Use if t is not None: instead of if t: to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.
我一直在尝试对这个损失函数进行一些小的调整,但是当我编译模型时,所有这些都会导致一些错误。我想我可能对这个东西的工作原理有一些基本的误解。有人可以帮忙吗?
编辑: 新的损失函数:
def func_loss(y_true, y_pred):
return -K.mean((K.cast(K.argmax(y_pred, axis=1), K.floatx()) - 1.0 )* (K.cast(K.argmax(y_true, axis=1), K.floatx()) - 1.0))
我也在 Tensorflow Github 上回复了你。
您已经编辑了损失函数,但实际上您现在得到的错误略有不同。
之前,错误发生在 K.mean
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 490, in mean
axis = _normalize_axis(axis, ndim(x))
现在,如您所见,它发生在 K.prod
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 464, in prod
axis = _normalize_axis(axis, ndim(x))
但是,原因仍然相同:K.mean
和 K.prod
各取一个 Tensor,因此您传递的第二个 Tensor
被视为 axis
参数。