【问题标题】:Pass a tensor returned from keras.backend.argmax as indices to keras.backend,gather which expects 'An integer tensor of indices.'将从 keras.backend.argmax 返回的张量作为索引传递给 keras.backend,gather,它需要“索引的整数张量”。
【发布时间】:2018-09-27 20:17:08
【问题描述】:

我正在尝试实现自定义损失函数

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 is not in [0, 32) [[节点: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

【问题讨论】:

  • Argmax 取自最后一个轴,而gather 取自第一个轴。您在两个轴上没有相同数量的元素,因此这是意料之中的。 -- 你的张量的形状是什么?你想使用哪些轴?
  • 如何找到张量的形状。对不起,我是 python 和 keras 的新手
  • 这是你模型的输出形状,见model.summary()。 (编译前可以调用)
  • 我已编辑问题以添加模型摘要
  • 好吧……你有 256 个类……你想让损失函数只对所有样本的最大类起作用吗?还是仅针对所有类别的最大样本?

标签: python tensorflow machine-learning keras


【解决方案1】:

Argmax 取自最后一个轴,而gather 取自第一个轴。两个轴上的元素数量不同,因此这是意料之中的。

如果只在类上工作,请使用最后一个轴,所以我们将围绕着gather 方法进行古怪的处理:

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)

【讨论】:

    猜你喜欢
    • 2018-06-09
    • 2019-05-07
    • 2023-02-04
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2019-11-26
    • 1970-01-01
    相关资源
    最近更新 更多