Training RNN with error evaluation at every time step

Viewed 394

I have a simpleRNN / LSTM that I'm trying to train on a sequential classification task using tensorflow. There is a sequence of data (300 time steps) that predicts a label at t=300. For my task I would like for the RNN to evaluate the error at every timestep (not just at the final time point) and propagate it backwards (as figure below).

enter image description here

After some responses below it seems I need to do a few things: use return_sequences flag; use the TimeDistributed layer to access the output from the LSTM/RNN; and also defined a custom loss function.

model = Sequential()
layer1 = LSTM(n_neurons, input_shape=(length, 1), return_sequences=True)
model.add(layer1)
layer2 = TimeDistributed(Dense(1))
model.add(layer2)
    
# Define custom loss
def custom_loss(layer1):
        
    # Create a loss function
    def loss(y_true,y_pred):
       # access layer1 at every time point and compute mean error
       # UNCLEAR HOW TO RUN AT EVERY TIME STEP
       err = K.mean(layer1(X) - y_true, axis=-1)
       return err
           
    # Return a function
    return loss

# Compile the model
model.compile(optimizer='adam', loss=custom_loss(layer), metrics=['accuracy'])
    

For now I'm a bit confused of the custom_loss function as it's not clear that how I can pass in layer1 and compute the error inside the inner most loss function.

Anyone has a suggestion or can point me to a more detailed answer?

1 Answers

The question is not easy to answer since it is not clear what you're trying to achieve (it shouldn't be the same using a FFNN or a RNN, and what works best depends definitely on the application).

Anyway, you might be confusing the training steps (say, the forward- and back- propagation over a minibatch of sequences) with the "internal" steps of the RNN. A single sequence (or a single minibatch) will always "unroll" entirely through time during the forward pass before any output is made available: only after (thus, at the end of the training step), you can use the predictions and compute the losses to backpropagate.

What you can do is return sequences of outputs (one y_predicted for every internal time step) including the argument return_sequences=True inside SimpleRNN(...). This will give you a sequence of 300 predictions, each of which depends only on the past inputs with respect to the considered internal time step. You can then use the outputs that you need to compute the loss, possibly in a custom loss function.

I hope I've been clear enough. Otherwise, let me know if I can help further.

Related