Python 如何收集与Keras/tf2.0自定义丢失函数中的真实标签不对应的y_pred元素?

Python 如何收集与Keras/tf2.0自定义丢失函数中的真实标签不对应的y_pred元素?,python,tensorflow,keras,tensorflow2.0,loss-function,Python,Tensorflow,Keras,Tensorflow2.0,Loss Function,下面是我想做的一个简单的numpy示例: import numpy as np y_true = np.array([0,0,1]) y_pred = np.array([0.1,0.2,0.7]) yc = (1-y_true).astype('bool') desired = y_pred[yc] >>> desired >>> array([0.1, 0.2]) 所以对应于地面真值的预测值是0.7,我想对一个数组进行运算,该数组包含y_pred

下面是我想做的一个简单的numpy示例:

import numpy as np

y_true = np.array([0,0,1])
y_pred = np.array([0.1,0.2,0.7])

yc = (1-y_true).astype('bool')

desired = y_pred[yc]

>>> desired
>>> array([0.1, 0.2])
所以对应于地面真值的预测值是0.7,我想对一个数组进行运算,该数组包含y_pred的所有元素,除了地面真值元素

我不确定如何在Keras内实现这一点。下面是损失函数问题的一个工作示例。现在“渴望”并没有完成任何事情,但这正是我需要解决的问题:

# using tensorflow 2.0.0 and keras 2.3.1

import tensorflow.keras.backend as K
import tensorflow as tf
from tensorflow.keras.layers import Input,Dense,Flatten
from tensorflow.keras.models import Model
from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Normalize data.
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

# Convert class vectors to binary class matrices.
y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)

input_shape = x_train.shape[1:]


x_in = Input((input_shape))

x = Flatten()(x_in)
x = Dense(256,'relu')(x)
x = Dense(256,'relu')(x)
x = Dense(256,'relu')(x)

out = Dense(10,'softmax')(x)




def loss(y_true,y_pred):


    yc = tf.math.logical_not(kb.cast(y_true, 'bool'))
    desired = tf.boolean_mask(y_pred,yc,axis = 1)    #Remove and it runs


    CE = tf.keras.losses.categorical_crossentropy(
        y_true,
        y_pred)

    L = CE

    return L



model = Model(x_in,out)

model.compile('adam',loss = loss,metrics = ['accuracy'])


model.fit(x_train,y_train)
我最后犯了一个错误

ValueError: Shapes (10,) and (None, None) are incompatible
其中10是类别数。最终目的是实现这一点:
在Keras中,我的问题似乎是第26-28行。

您可以从
布尔_掩码中删除
axis=1
,它将运行。坦白说,我不明白为什么这里需要axis=1

def loss(y_true,y_pred):


    yc = tf.math.logical_not(K.cast(y_true, 'bool'))
    print(yc.shape)
    desired = tf.boolean_mask(y_pred, yc)    #Remove axis=1 and it runs


    CE = tf.keras.losses.categorical_crossentropy(
        y_true,
        y_pred)

    L = CE

    return L

这可能就是发生的情况。您有
y_pred
,它是一个二维张量(
N=2
)。然后就有了一个二维遮罩(
K=2
)。但是使用thushv89的答案有这个条件
K+轴,下面是我如何在参考文献中的LeNet上实现的完整代码。一个技巧是,我实际上并不是在两个目标之间来回翻转,而是有一个随机权重翻转
s

# using tensorflow 2.0.0 and keras 2.3.1

import tensorflow.keras.backend as kb
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, Input, Dense,Flatten,AveragePooling2D,GlobalAveragePooling2D
from tensorflow.keras.models import Model
from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Normalize data.
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

#exapnd dims to fit chn format
x_train = np.expand_dims(x_train,axis=3)
x_test = np.expand_dims(x_test,axis=3)


# Convert class vectors to binary class matrices.
y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)

input_shape = x_train.shape[1:]

x_in = Input((input_shape))

act = 'tanh'
x = Conv2D(32, (5, 5), activation=act, padding='same',strides = 1)(x_in)
x = AveragePooling2D((2, 2),strides = (2,2))(x)
x = Conv2D(16, (5, 5), activation=act)(x)
x = AveragePooling2D((2, 2),strides = (2,2))(x)

conv_out = Flatten()(x)
z = Dense(120,activation = act)(conv_out)#120
z = Dense(84,activation = act)(z)#84
last = Dense(10,activation = 'softmax')(z)

model = Model(x_in,last)



def loss(y_true,y_pred, axis=-1):

    s = kb.round(tf.random.uniform( (1,), minval=0, maxval=1, dtype=tf.dtypes.float32))
    s_ = 1 - s

    y_pred = y_pred + 1e-8

    yg = kb.max(y_pred,axis=1)
    yc = tf.math.logical_not(kb.cast(y_true, 'bool'))
    yp_c = tf.boolean_mask(y_pred, yc)  

    ygc_ = 1/(1-yg+1e-8)
    ygc_ = kb.expand_dims(ygc_,axis=1)

    Px = yp_c*ygc_ +1e-8

    COT = kb.mean(Px*kb.log(Px),axis=1)

    CE = -kb.mean(y_true*kb.log(y_pred),axis=1)

    L = s*CE +s_*(1/(10-1))*COT

    return L


model.compile(loss=loss, 
              optimizer='adam', metrics=['accuracy'])


model.fit(x_train,y_train,epochs=20,batch_size = 128,validation_data= (x_test,y_test))

pred = model.predict(x_test)

pred_label = np.argmax(pred,axis=1)
label = np.argmax(y_test,axis=1)

cor = (pred_label == label).sum()
acc = print('acc:',cor/label.shape[0])

请提供工作正常的rest之后的代码。所以我们得到了你试图使用的全部损失。我添加了一个例子,你可以运行它来重现相同的错误。是的,这是有道理的。我将尝试完成代码的其余部分。当我运行它时,我发现,
WARNING:tensorflow:Entity无法转换,将按原样执行。请向签名组报告。在归档bug时,将详细度设置为10(在Linux上,`export AUTOGRAPH\u verbosity=10`),并附加完整的输出。原因:
它仍在运行,但知道为什么吗?@NickMerrill,不太清楚为什么会出现这种情况。我去看看