我正在尝试使用Keras解决3个输出的回归问题。我需要创建一个自定义指标来比较这三个变量中每个变量的真实值和预测值,并查看它们是否都在某个范围内,如果发生这种情况,我认为这是成功的。我已经尝试了一些您可以在下面看到的行中的内容
Yclasses = 3 #Number of output classes
train_samples = 2 # Number of train samples in the batch
valid_samples = max(1,np.int(0.2*train_samples)) # Number of validation samples in the batch
cont = np.zeros((train_samples+valid_samples,Yclasses))
def nossa_metrica(y_true, y_pred):
for l in range(0,y_true.shape[0]):
for c in range(0,y_true.shape[0]):
if c == 0:
if y_true[l][c] <= 4000 and y_pred[l][c] <= 4000:
cont[l][c] = 1
elif (y_true[l][c] > 4000 and y_true[l][c] <= 8500) and (y_pred[l][c] > 4000 and y_pred[l][c] <= 8500):
cont[l][c] = 1
elif y_true[l][c] > 8500 and y_pred[l][c] > 8500:
cont[l][c] = 1
elif c == 1:
if y_true[l][c] <= 1150 and y_pred[l][c] <= 1150:
cont[l][c] = 1
elif (y_true[l][c] > 1150 and y_true[l][c] <= 2300) and (y_pred[l][c] > 1150 and y_pred[l][c] <= 2300):
cont[l][c] = 1
elif y_true[l][c] > 2300 and y_pred[l][c] > 2300:
cont[l][c] = 1
elif c == 2:
if (y_true[l][c] <= 0.075 and y_pred[l][c] <= 0.075) or (y_true[l][c] > 0.075 and y_pred[l][c] > 0.075):
cont[l][c] = 1
return np.sum(cont)/((train_samples+valid_samples)*Yclasses)
但是,我遇到以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-1c70196ab01b> in <module>()
68
69 #model.compile(loss='binary_crossentropy',optimizer='adam',verbose = 2,metrics=['binary_accuracy'])
---> 70 model.compile(loss='mse',optimizer='adam',verbose = 2,metrics=[nossa_metrica])
71 model.summary()
72
10 frames
<ipython-input-20-1c70196ab01b> in nossa_metrica(y_true, y_pred)
15 print(cont)
16 def nossa_metrica(y_true, y_pred):
---> 17 for l in range(0,y_true.shape[0]):
18 for c in range(0,y_true.shape[0]):
19 if c == 0:
TypeError: 'NoneType' object cannot be interpreted as an integer
那么,错误与我的for
循环有关吗?而且我不太确定如何使用循环和条件来定义自定义指标。任何帮助将非常感激。
谢谢!