【问题标题】:Can I disable val_loss caculation in keras?我可以在 keras 中禁用 val_loss 计算吗?
【发布时间】:2020-01-14 02:15:12
【问题描述】:

其实我想在训练和验证阶段使用不同的损失函数。我试过 in_tarin_phase 但它不起作用。

所以我只是想知道我可以禁用 val_loss 计算吗?

【问题讨论】:

  • 为什么要这样做?除了常规损失之外,您能否创建自己的自定义指标,让 keras 评估?

标签: python tensorflow keras loss-function loss


【解决方案1】:

下面有一个自定义的损失函数:

# Build a model
inputs = Input(shape=(128,))
layer1 = Dense(64, activation='relu')(inputs)
layer2 = Dense(64, activation='relu')(layer1)
predictions = Dense(10, activation='softmax')(layer2)
model = Model(inputs=inputs, outputs=predictions)

# Define custom loss
def custom_loss(layer):

    # Create a loss function that adds the MSE loss to the mean of all squared activations of a specific layer
    def loss(y_true,y_pred):
        return K.mean(K.square(y_pred - y_true) + K.square(layer), axis=-1)

    # Return a function
    return loss

# Compile the model
model.compile(optimizer='adam',
              loss=custom_loss(layer), # Call the loss function with the selected layer
              metrics=['accuracy'])

# train
model.fit(data, labels) 

【讨论】:

    猜你喜欢
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    相关资源
    最近更新 更多