I have an RNN model that looks essentially like this:So I have input sequences $x_{i0},x_{i1},...,x_{it}$ with true outputs $y_{i0},y_{i1},...,y_{it}$ and predicted outputs $h_{i0},h_{i1},...,h_{it}$. My loss function is cumulative over each output, so that for each sequence $i$:$$L_i=\frac{1}{t}\sum_{m=0}^tf(g(h_{im}),y_{im}).$$For an easy example, lets just say that:$$L_i=\frac{1}{t}\sum_{m=0}^t(\exp(h_{im})-y_{im})^2.$$Note: This is just an example, and I'd like a general way for doing this for any $f$.import numpy as npfrom keras.models import Sequentialfrom keras.layers import Densefrom keras.layers import LSTMfrom keras.layers import Activationfrom keras.layers import Maskingfrom keras.optimizers import RMSpropfrom keras import backend as k# Initialize modelmodel = Sequential()# Mask lookback period to fix sequences of varying lengthsmodel.add(Masking(mask_value=0., input_shape=(max_time, 24)))# LSTM model.add(LSTM(50, input_dim=10, return_sequences=True))# Outputmodel.add(Dense(1))#Exponential activation function will be wrapped up in lossmodel.add(Activation(k.linear))model.compile(loss=custom_loss, optimizer=RMSprop(lr=.001))anddef custom_loss(y,h_out,name=None):    return k.mean(k.pow(h_out - y,2))What confuses me is whether Keras is automatically calculating the gradient of the loss, or just using a numerical approximation?If the weights of the RNN are $\theta$, then the gradients look something like:$$\frac{\partial f}{\partial \theta}=\frac{2}{t}(\exp(h_{im}-y_{im})\exp(h_{im})\frac{\partial h_{im}}{\partial \theta}.$$Assuming Keras doesn't automatically calculate gradients, how would I pass this correctly to the LSTM, as it should need to infer the time $m$ of $h_{im}$ to correctly back propagate?I would love to see a reference for how to do this correctly. 
Specifying gradients in Keras RNN
neural network;keras;rnn