【发布时间】:2020-01-14 02:15:12
【问题描述】:
其实我想在训练和验证阶段使用不同的损失函数。我试过 in_tarin_phase 但它不起作用。
所以我只是想知道我可以禁用 val_loss 计算吗?
【问题讨论】:
-
为什么要这样做?除了常规损失之外,您能否创建自己的自定义指标,让 keras 评估?
标签: python tensorflow keras loss-function loss
其实我想在训练和验证阶段使用不同的损失函数。我试过 in_tarin_phase 但它不起作用。
所以我只是想知道我可以禁用 val_loss 计算吗?
【问题讨论】:
标签: python tensorflow keras loss-function loss
下面有一个自定义的损失函数:
# 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)
【讨论】: