keras如何在验证集加噪声_Keras中用于验证集的不同损失函数

I have unbalanced training dataset, thats why I built custom weighted categorical cross entropy loss function. But the problem is my validation set is balanced one and I want to use the regular categorical cross entropy loss. So can I pass different loss function for validation set within Keras? I mean the wighted one for training and regular one for validation set?

def weighted_loss(y_pred, y_ture):

'

'

'

return loss

model.compile(loss= weighted_loss, metric='accuracy')

解决方案

You can try the backend function K.in_train_phase(), which is used by the Dropout and BatchNormalization layers to implement different behaviors in training and validation.

def custom_loss(y_true, y_pred):

weighted_loss = ... # your implementation of weighted crossentropy loss

unweighted_loss = K.sparse_categorical_crossentropy(y_true, y_pred)

return K.in_train_phase(weighted_loss, unweighted_loss)

The first argument of K.in_train_phase() is the tensor used in training phase, and the second is the one used in test phase.

For example, if we set weighted_loss to 0 (just to verify the effect of K.in_train_phase() function):

def custom_loss(y_true, y_pred):

weighted_loss = 0 * K.sparse_categorical_crossentropy(y_true, y_pred)

unweighted_loss = K.sparse_categorical_crossentropy(y_true, y_pred)

return K.in_train_phase(weighted_loss, unweighted_loss)

model = Sequential([Dense(100, activation='relu', input_shape=(100,)), Dense(1000, activation='softmax')])

model.compile(optimizer='adam', loss=custom_loss)

model.outputs[0]._uses_learning_phase = True # required if no dropout or batch norm in the model

X = np.random.rand(1000, 100)

y = np.random.randint(1000, size=1000)

model.fit(X, y, validation_split=0.1)

Epoch 1/10

900/900 [==============================] - 1s 868us/step - loss: 0.0000e+00 - val_loss: 6.9438

As you can see, the loss in training phase is indeed the one multiplied by 0.

Note that if there's no dropout or batch norm in your model, you'll need to manually "turn on" the _uses_learning_phase boolean switch, otherwise K.in_train_phase() will have no effect by default.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值