我写了一个自定义损失函数adjusted_r2
。我正在尝试在函数内部打印张量值,但是在打印Logs时,我什么也看不到。有人可以帮我吗?
def coeff_determination(y_true, y_pred):
from keras import backend as K
SS_res = K.sum(K.square( y_true-y_pred ))
SS_tot = K.sum(K.square( y_true - K.mean(y_true) ) )
SS_res = K.print_tensor(SS_res, message='SS_res = ')
SS_tot = K.print_tensor(SS_tot, message='SS_tot = ')
r_squared = 1 - SS_res/(SS_tot + K.epsilon())
r_squared = K.print_tensor(r_squared, message='r_squared = ')
adj_r_squared = 1 -( (1-r_squared)*K.cast(K.shape(y_true)[0]-1,"float32")/K.cast((K.shape(y_true)[0]-n_features-1),"float32"))
adj_r_squared = K.print_tensor(adj_r_squared, message='adj_r_squared = ')
return -adj_r_squared
日志为:
1/250 [..............................] - ETA: 51:44 - loss: -6.7060 - coeff_determination: -6.7060 - mean_squared_error: 40.5785
2/250 [..............................] - ETA: 42:56 - loss: -7.2036 - coeff_determination: -7.2036 - mean_squared_error: 48.8251
3/250 [..............................] - ETA: 41:30 - loss: -8.0279 - coeff_determination: -8.0279 - mean_squared_error: 48.1565
4/250 [..............................] - ETA: 40:48 - loss: -9.1016 - coeff_determination: -9.1016 - mean_squared_error: 51.9965
答案 0 :(得分:0)
在评估张量时,K.print_tensor()
函数起作用(请参见documentation here)。调用自定义损失函数时,张量不会初始化。这就是为什么您不能从损失函数中评估张量值的原因。自定义损失函数的参数是张量,它们充当占位符,而没有附加实际数据。
此thread中也讨论了相同的问题。