# Training LSTM for time series prediction with nan labels

I have a time series of features $$x_1,x_2,x_3,...,x_n$$. I want to make a prediction $$y_1,y_2,y_3,...,y_n$$ for each timestep. However, in my training data some of the $$y$$ can be nan. I'd like the fit to just ignore these (i.e. the cost for this pair measured $$y$$ and predicted $$y$$ is zero). I'm currently using tensorflow through Keras.

Is there an analogue of the masking layer for the label? I'm currently using tensorflow through Keras. Alternatively, it might be possible to change the loss function, but I don't know how, expecially while retaining numerical efficiency.

## 2 Answers

I suggest implementing it this way :

1. Set the nan value to 0 or any other value
2. when compiling keras model use parameter sample_weight_mode='temporal'
3. You can use masking on top of this by supplying the weight as the mask (sequence of values 1 if not nan 0 otherwise).

The steps above should give you the desired result.

• I am trying to implement this solution but it seems that my model tries to fit the missing data (which I set to -1 as you suggested in 3). Do you guys have a working example? For example, is it ok to use .fit(..,shuffle=False, validation_split=0.1)? There are several posts on StackOverflow having trouble with sample_weight, incl. mine stackoverflow.com/questions/63744387/… – aless80 Sep 4 at 18:20

You can use mean fill or predict that missing y value. Given the $$y_1,y_2,y_3,\cdots ,y_n$$ for each training sample, you can fill in the missing by mean of that $$y$$ across training examples, or interpolate