我正在尝试实现自定义丢失功能
def lossFunction(self,y_true,y_pred):
maxi=K.argmax(y_true)
return K.mean((K.max(y_true) -(K.gather(y_pred,maxi)))**2)
在训练时出现以下错误
InvalidArgumentError(参见上面的回溯):indices [5] = 51不在[0,32]中 [[Node:loss / dense_3_loss / Gather = Gather [Tindices = DT_INT64,Tparams = DT_FLOAT,validate_indices = true,_device =“/ job:localhost / replica:0 / task:0 / device:CPU:0”](dense_3 / BiasAdd,metrics / acc / ArgMax)]]
模型摘要
_________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
input_1 (InputLayer) (None, 64, 50, 1) 0
____________________________________________________________________________________________________
input_2 (InputLayer) (None, 64, 50, 1) 0
____________________________________________________________________________________________________
conv2d_1 (Conv2D) (None, 32, 25, 16) 272 input_1[0][0]
____________________________________________________________________________________________________
conv2d_2 (Conv2D) (None, 32, 25, 16) 272 input_2[0][0]
____________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D) (None, 16, 12, 16) 0 conv2d_1[0][0]
____________________________________________________________________________________________________
max_pooling2d_2 (MaxPooling2D) (None, 16, 12, 16) 0 conv2d_2[0][0]
____________________________________________________________________________________________________
conv2d_3 (Conv2D) (None, 15, 11, 32) 2080 max_pooling2d_1[0][0]
____________________________________________________________________________________________________
conv2d_4 (Conv2D) (None, 15, 11, 32) 2080 max_pooling2d_2[0][0]
____________________________________________________________________________________________________
max_pooling2d_3 (MaxPooling2D) (None, 8, 6, 32) 0 conv2d_3[0][0]
____________________________________________________________________________________________________
max_pooling2d_4 (MaxPooling2D) (None, 8, 6, 32) 0 conv2d_4[0][0]
____________________________________________________________________________________________________
flatten_1 (Flatten) (None, 1536) 0 max_pooling2d_3[0][0]
____________________________________________________________________________________________________
flatten_2 (Flatten) (None, 1536) 0 max_pooling2d_4[0][0]
____________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 3072) 0 flatten_1[0][0]
flatten_2[0][0]
____________________________________________________________________________________________________
input_3 (InputLayer) (None, 256) 0
____________________________________________________________________________________________________
concatenate_2 (Concatenate) (None, 3328) 0 concatenate_1[0][0]
input_3[0][0]
____________________________________________________________________________________________________
dense_1 (Dense) (None, 512) 1704448 concatenate_2[0][0]
____________________________________________________________________________________________________
dense_2 (Dense) (None, 256) 131328 dense_1[0][0]
____________________________________________________________________________________________________
dense_3 (Dense) (None, 256) 65792 dense_2[0][0]
====================================================================================================
Total params: 1,906,272
Trainable params: 1,906,272
Non-trainable params: 0
答案 0 :(得分:0)
Argmax从最后一个轴开始,而聚集从第一个轴开始。您在两个轴上没有相同数量的元素,因此这是预期的。
对于只在类上工作,使用最后一个轴,所以我们要围绕聚集方法进行讨论:
def lossFunction(self,y_true,y_pred):
maxi=K.argmax(y_true) #ok
#invert the axes
y_pred = K.permute_dimensions(y_pred,(1,0))
return K.mean((K.max(y_true,axis=-1) -(K.gather(y_pred,maxi)))**2)