Keras Creating custom loss function that takes into account only certain features

Viewed 16

I want to create a custom loss function that takes into account some output features (not all). I am training sequence regression LSTM neural network on data that looks like this: my input shape is (number_of_samples, 200 timesteps, 4 features) and my output shape is (number_of_samples, 200 timesteps, 6 features). My first, basic model looks like this:

inputs1=Input(shape=(None,num_input_features))
lstm1=LSTM(10,return_sequences=True)(inputs1)
lstm2=LSTM(10,return_sequences=True)(lstm1)
outputs1=TimeDistributed(Dense(num_output_features))(lstm2)
model_proba=Model(inputs=inputs1,outputs=outputs1)

I want to train a model only on the first 4 features of my output (the last 2 features are not relevant for training, I just want to predict them without training on that data). I have tried creating a custom loss function that looks like this:

def custom_loss(y_true, y_pred): # Loss that doesn't take into account last 2 output features
    y_true_r=y_true[:,:,:4]
    y_pred_r=y_pred[:,:,:4]
    mse = MeanSquaredError()
    return mse(y_true_r, y_pred_r)

But the problem is that during the training my weights and biases connected to 2 output features (that are not in the loss function) are not trained, they have same initial values after the training.

As far as I understand, the loss doesn't depend on these weights and biases so the gradient is 0 and there is no weights and bias updates. So i want to know is it possible to train these weights and biases in relation to the global loss value?

p.s. I am using Adam optimizer.

0 Answers
Related