【问题标题】:Loss manually calculated after fitting an order of magnitude higher than loss of last epoch在拟合比上一个时期的损失高一个数量级后手动计算的损失
【发布时间】:2019-05-13 22:27:55
【问题描述】:

我有以下神经网络

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 - loss: 0.0055 - customLoss: 0.0055

然而,当我打印 customLoss (y_train , train_predictions))

我得到 0.06469738

我读过训练期间的损失是整个时期的平均值,但可以肯定的是,最终结果不应该更糟,而且肯定不会相差一个数量级? 我对 keras 比较陌生,所以任何建议都值得赞赏 谢谢!

【问题讨论】:

    标签: python machine-learning keras


    【解决方案1】:

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

    解决了问题

    【讨论】:

      猜你喜欢
      • 2021-12-18
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 2022-01-24
      • 2022-06-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      相关资源
      最近更新 更多