gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:29:08 24 4
gpt4 key购买 nike

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

架构(简化):

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

在方便时更改 alphabeta

因此,整个事情可能是:

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))

关于python - Keras 使用自定义损失优化两个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49646304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com