自定义指标Keras

自定义指标Keras,keras,metrics,Keras,Metrics,我试图用Keras解决一个有3个输出的回归问题。我需要创建一个自定义指标来比较这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)

我试图用Keras解决一个有3个输出的回归问题。我需要创建一个自定义指标来比较这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)
yclass=3#输出类的数量
序列样本=2个#批次中的序列样本数
有效样本=最大(1,np.int(0.2*系列样本))#批次中的验证样本数
cont=np.零((序列样本+有效样本,循环类))
def nossa_metrica(y_true,y_pred):
对于范围内的l(0,y_true.shape[0]):
对于范围(0,y_true.shape[0])内的c:
如果c==0:
如果y_真[l][c]8500:
续[l][c]=1
elif c==1:
如果y_真[l][c]2300:
续[l][c]=1
elif c==2:
如果(y_真[l][c]0.075):
续[l][c]=1
返回np.总和(续)/((序列样本+有效样本)*YCLASS)
但是,我得到了以下错误:

---------------------------------------------------------------------------
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
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
68
69#model.compile(loss='binary_crossentropy',optimizer='adam',verbose=2,metrics=['binary_accurity']))
--->70 model.compile(loss='mse',optimizer='adam',verbose=2,metrics=[nossa_metrica])
71范本摘要()
72
10帧
在nossa_metrica(y_true,y_pred)
15印刷品(续)
16 def nossa_metrica(y_true,y_pred):
--->17对于范围内的l(0,y_真。形状[0]):
18对于范围内的c(0,y_真。形状[0]):
19如果c==0:
TypeError:“非类型”对象不能解释为整数
那么,错误与我的
for
循环有关?我真的不知道如何使用循环和条件来定义自定义度量。任何帮助都将不胜感激


提前谢谢

您是否传入一个空批次,即形状为
(0,…)
?不,没有空批次。