I have the following neural network

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)

The last output is 3663/3663 [==============================] - 0s 103us/step - loss: 0.0055 - customLoss: 0.0055

however, when i print customLoss (y_train , train_predictions))

i get 0.06469738

I ve read that the loss during training is the average all through the epoch, but surely, the end result shouldnt be worse and certainly not an order of magnitude different? I am relatively new to keras, so any suggestion is appreciated Thanks!

1 Answers

0
john On Best Solutions

As it turns out, the training predictions were of the shape (3000 , 1) and the y_train (3000, ) train_predictions = NN_model.predict(X_train).flatten()

solved the problem