[英]Handling and Combining two loss function in Keras TF
Is there a way to have two loss functions in Keras in which the second loss function takes the output from the first loss function?
我正在使用 Keras 研究神經網絡,我想在 model.compile() 中的損失項中添加另一個自定義 function 以對其進行正則化並以某種方式對其進行處理:
model.compile(loss_1='mean_squared_error', optimizer=Adam(lr=learning_rate), metrics=['mae'])
我想添加另一個損失 function 作為來自 Loss_1 輸出的預測值的總和,以便我可以告訴神經網絡最小化來自 Loss_1 model 的預測值的總和。 我該怎么做(loss_2)?
就像是:
model.compile(loss_1='mean_squared_error', loss_2= np.sum(****PREDICTED_OUTPUT_FROM_LOSS_FUNCTION_1****), optimizer=Adam(lr=learning_rate), metrics=['mae'])
如何實施?
您應該定義一個自定義損失 function
def custom_loss_function(y_true, y_pred):
squared_difference = tf.square(y_true - y_pred)
absolute_difference = tf.abs(y_true - y_pred)
loss = tf.reduce_mean(squared_difference, axis=-1) + tf.reduce_mean(absolute_difference, axis=-1)
return loss
model.compile(optimizer='adam', loss=custom_loss_function)
我相信這會解決你的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.