比上一个时期的损失高一个数量级后,人工计算的损失

时间:2019-05-13 11:45:55

标签: python machine-learning keras

我有以下神经网络

def customLoss(yTrue,yPred):
    loss_value = np.divide(abs(yTrue - yPred) , yTrue)
    loss_value = tf.reduce_mean(loss_value)
    return loss_value

def model(inp_size):

   inp = Input(shape=(inp_size,))
   x1 = Dense(100, activation='relu')((inp))
   x1 = Dense(50, activation='relu')(x1)
   x1 = Dense(20, activation='relu')(x1)
   x1 = Dense(1, activation = 'linear')(x1)

    x2 = Dense(100, activation='relu')(inp)
    x2 = Dense(50, activation='relu')(x2)
    x2 = Dense(20, activation='relu')(x2)
    x2 = Dense(1, activation = 'linear')(x2)

    x3 = Dense(100, activation='relu')(inp)
    x3 = Dense(50, activation='relu')(x3)
    x3 = Dense(20, activation='relu')(x3)
    x3 = Dense(1, activation = 'linear')(x3)

    x4 = Dense(100, activation='relu')(inp)
    x4 = Dense(50, activation='relu')(x4)
    x4 = Dense(20, activation='relu')(x4)
    x4 = Dense(1, activation = 'linear')(x4)



    x1 = Lambda(lambda x: x * baseline[0])(x1)
    x2 = Lambda(lambda x: x * baseline[1])(x2)
    x3 = Lambda(lambda x: x * baseline[2])(x3)
    x4 = Lambda(lambda x: x * baseline[3])(x4)

    out = Add()([x1, x2, x3, x4])

    return Model(inputs = inp, outputs = out)
y_train=y_train.astype('float32')
y_test=y_test.astype('float32')



NN_model = Sequential()
NN_model = model(X_train.shape[1])
NN_model.compile(loss=customLoss, optimizer= 'Adamax', metrics=    [customLoss])

NN_model.fit(X_train, y_train, epochs=500,verbose = 1)
train_predictions = NN_model.predict(X_train)


predictions = NN_model.predict(X_test)
MAE  = customLoss (y_test, predictions)

最后的输出是 3663/3663 [==============================]-0s 103us / step-损失:0.0055-customLoss:0.0055

但是,当我打印时 customLoss(y_train,train_predictions)

我得到0.06469738

我已经读到,训练期间的损失是整个时期的平均水平,但可以肯定的是,最终结果不应该更糟,并且肯定不会有数量级的差异吗? 我是新来的喀拉拉邦,所以任何建议都值得赞赏 谢谢!

1 个答案:

答案 0 :(得分:0)

事实证明,训练预测的形状为(3000,1)和y_train(3000,) train_predictions = NN_model.predict(X_train).flatten()

解决了问题