Keras:如何在自定义损失中获得张量尺寸?

mrg*_*oom 17 python neural-network deep-learning keras tensorflow

我正在尝试编写自定义丢失函数:我想应用于categorical_crossentropy输入向量的部分然后求和.

假设y_true,y_pred是1D向量.

码:

def custom_loss(y_true, y_pred):

    loss_sum= 0.0
    for i in range(0,y_true.shape[0],dictionary_dims):
        loss_sum+= keras.backend.categorical_crossentropy(y_true[i*dictionary_dims:(i+1)*dictionary_dims], y_pred[i*dictionary_dims:(i+1)*dictionary_dims])

    return loss_sum
Run Code Online (Sandbox Code Playgroud)

但是我收到一个错误:

    for i in range(0,y_true.shape[0],dictionary_dims):
TypeError: __index__ returned non-int (type NoneType)
Run Code Online (Sandbox Code Playgroud)

那么如何访问输入张量的形状来获得张量的子集呢?

更新: 还尝试直接通过tensorflow写丢失:

def custom_loss_tf(y_true, y_pred):

    print('tf.shape(y_true)',tf.shape(y_true)) #
    print('type(tf.shape(y_true))',type(tf.shape(y_true))) #

    sys.exit()

    loss_sum= 0.0
    for i in range(0,y_true.shape[0],dictionary_dims):
        loss_sum+= keras.backend.categorical_crossentropy(y_true[i*dictionary_dims:(i+1)*dictionary_dims], y_pred[i*dictionary_dims:(i+1)*dictionary_dims])

    return loss_sum
Run Code Online (Sandbox Code Playgroud)

输出:

tf.shape(y_true) Tensor("Shape:0", shape=(2,), dtype=int32)
type(tf.shape(y_true)) <class 'tensorflow.python.framework.ops.Tensor'>
Run Code Online (Sandbox Code Playgroud)

不确定是什么shape=(2,)意思,但这不是我所期待的,因为model.summary()显示最后一层是(None, 26):

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         (None, 80, 120, 3)        0
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 80, 120, 32)       896
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 40, 60, 32)        0
_________________________________________________________________
activation_1 (Activation)    (None, 40, 60, 32)        0
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 40, 60, 32)        9248
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 20, 30, 32)        0
_________________________________________________________________
activation_2 (Activation)    (None, 20, 30, 32)        0
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 20, 30, 64)        18496
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 10, 15, 64)        0
_________________________________________________________________
activation_3 (Activation)    (None, 10, 15, 64)        0
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 10, 15, 64)        36928
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 5, 7, 64)          0
_________________________________________________________________
activation_4 (Activation)    (None, 5, 7, 64)          0
_________________________________________________________________
flatten_1 (Flatten)          (None, 2240)              0
_________________________________________________________________
head (Dense)                 (None, 26)                58266
=================================================================
Run Code Online (Sandbox Code Playgroud)

Mar*_*jko 15

这里有两件事:

  1. 如果你想得到一个张量形状,你应该使用int_shape函数keras.backend.
  2. 第一个维度设置为批次维度,因此int_shape(y_true)[0]将返回批量大小.你应该用int_shape(y_true)[1].

  • 出于某种原因,`K.int_shape(y_true)`给了我`(无,无)`和'K.int_shape(y_pred)`它是`(无,26)`所以它看起来有效. (6认同)
  • 我认为这是因为 y_true 仅在训练期间已知,而当您编译模型时,y_pred 从模型中已知。 (2认同)