问题记录,keras 自定义损失 自动求导时出现None,后来想到是因为传入的变量没有使用,所以keras无法求出偏导,修改后问题解决。就是不愿使用的变量×0,求导后还是0就可以了。
def my_complex_loss_graph(y_label, emb_uid, lstm_out,y_true_1,y_true_2,y_true_3,out_1,out_2,out_3):
mse_out_1 = mean_squared_error(y_true_1, out_1)
mse_out_2 = mean_squared_error(y_true_2, out_2)
mse_out_3 = mean_squared_error(y_true_3, out_3)
# emb_uid= K.reshape(emb_uid, [-1, 32])
cosine_sim = tf.reduce_sum(0.5*tf.square(emb_uid-lstm_out))
cost=0*cosine_sim+K.sum([0.5*mse_out_1 , 0.25*mse_out_2,0.25*mse_out_3],axis=1,keepdims=True)
# print(mse_out_1)
final_loss = cost
return K.mean(final_loss)