Skip to main content
 首页 » 编程设计

python之Keras 使用自定义损失优化两个输出

2024年09月07日6lhb25

我最近一直在尝试实现一个模型,可以描述如下:给定一个输入矩阵和一组目标,让模型同时学习矩阵表示以及通过自定义的目标损失函数。

架构(简化):

input_matrix = Input(shape=(i_shape,)) 
layer1 = Dense(100)(input_matrix) 
output = Dense(3)(layer1) 
 
autoencoder_mid = Dense(100)(input_matrix) 
autoencoder_output = Dense(i_shape)(autoencoder_mid) 

我对损失函数的看法:

def customLoss(true_matrix,pred_matrix): 
    def combined_loss(y_true,y_pred): 
        return K.abs(y_true-y_pred) 
        a = K.mean( K.square(y_pred - y_true) * K.exp(-K.log(1.7) * (K.log(1. + K.exp((y_true - 3)/5 )))),axis=-1  ) 
        b = K.mean( K.square(pred_matrix - true_matrix) * K.exp(-K.log(1.7) * (K.log(1. + K.exp((true_matrix - 3)/5 )))),axis=-1) 
        return a+b 
    return combined_loss 

我将模型编译为:

net = Model(input_matrix, [output,autoencoder_output]) 
net = net.compile(optimizer='adam', loss=customLoss(true_matrix=X,pred_matrix=autoencoder_output)) 

我尝试让网络适应标准的地方:

 net.fit(X, 
         target, 
         epochs=10, 
         batch_size=10) 

我得到的错误是:

ValueError: Tensor conversion requested dtype float32 for Tensor with dtype float64: 'Tensor("loss/dense_4_loss/Log_3:0", shape=(389, 3890), dtype=float64, device=/device:GPU:0)' 

我的问题是,还有其他方法吗?如果是这样,能否请您指出一个可能的解决方案。非常感谢。

请您参考如下方法:

你可以试试这个:

def customLoss(true_matrix): 
    def combined_loss(y_true,y_pred): 
        y_pred, pred_matrix = y_pred 
        ... 
    return combined_loss 
 
net = Model(input_matrix, [output,autoencoder_output]) 
net.compile(optimizer='adam', loss=customLoss(X)) 

因为原始的 y_pred 将是一个带有 (output,autoencoder_output) 的双元组。

关于双重 return,该函数只会返回第一个,所以我会删除两个返回行中的一个或合并两个输出,例如:

alpha = 0.5 
beta = 0.5 
... 
loss1, loss2 = K.abs(y_true-y_pred), a+b 
 
return alpha*loss1 + beta*loss2  

在方便时更改 alpha 和 beta。

因此,整个事情可能是:

def customLoss(true_matrix, alpha = 0.5, beta = 0.5): 
    def combined_loss(y_true,y_pred): 
        y_pred, pred_matrix = y_pred 
        a = K.mean( K.square(y_pred - y_true) * K.exp(-K.log(1.7) * (K.log(1. + K.exp((y_true - 3)/5 )))),axis=-1  ) 
        b = K.mean( K.square(pred_matrix - true_matrix) * K.exp(-K.log(1.7) * (K.log(1. + K.exp((true_matrix - 3)/5 )))),axis=-1) 
        loss1, loss2 = K.abs(y_true-y_pred), a+b 
        return alpha*loss1 + beta*loss2  
    return combined_loss 
 
net = Model(input_matrix, [output,autoencoder_output]) 
net.compile(optimizer='adam', loss=customLoss(X))