I have a keras sequential NN model which is finished training. I wanted to know if I would be able to get gradients of the loss function with respect to the input features or attributes. I have tried the following code on the model but get the following error. Any solution or suggestions to get around the error?
AttributeError: 'Sequential' object has no attribute 'total_loss'
model = Sequential()
input_dim = len(input_cols_idx)
batch_size = batch_size_gen
model.add(Dense(units=hidden_layer1_neurons, input_dim= input_dim,
kernel_initializer = initializers.RandomNormal(mean=0.0, stddev=0.05)))
model.add(Activation(activation_1))
model.add(Dense(units=1))
model.add(Activation(activation_2))
model.compile(loss='mean_squared_error', optimizer=Adam(lr=learning_rate), metrics=
['mae'])
#....
# Training
#...
#-----------------
#Gradient Calculation (post training)
weights = model.weights # weight tensors
gradients = model.optimizer.get_gradients(model.total_loss, weights) # gradient tensors
import keras.backend as K
input_tensors = [model.inputs[0], # input data
model.sample_weights[0], # sample weights
model.targets[0], # labels
K.learning_phase(), # train or test mode
]
get_gradients = K.function(inputs=input_tensors, outputs=gradients)