I set up a neural network and I am trying to give it the inbuilt mean_relative_error as a loss function. I set it up as follows
def customLoss(yTrue,yPred):
err, loss_value = mean_relative_error(yTrue, yPred, yTrue)
return loss_value
def model(inp_size):
inp = Input(shape=(inp_size,))
x1 = Dense(100, activation='relu')((inp))
for i in range (6):
x1 = Dense(100, activation='relu')(x1)
x1 = Dense(1, activation = 'linear')(x1)
x2 = Dense(100, activation='relu')(inp)
for i in range (6):
x2 = Dense(100, activation='relu')(x2)
x2 = Dense(1, activation = 'linear')(x2)
x3 = Dense(100, activation='relu')(inp)
for i in range (6):
x3 = Dense(100, activation='relu')(x3)
x3 = Dense(1, activation = 'linear')(x3)
x4 = Dense(100, activation='relu')(inp)
for i in range (6):
x4 = Dense(100, 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 = model(X_train.shape[1])
NN_model.compile(loss=customLoss, optimizer='Adamax', metrics=[customLoss])
NN_model.build(X_train.shape)
#NN_model.summary()
NN_model.fit(X_train, y_train, epochs=2,verbose = 1)
train_predictions = NN_model.predict(X_train)
predictions = NN_model.predict(X_test)
However, I get the following error
ValueError: An operation has
None
for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.
Does anyone have any idea? Thanks!