0

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)
jam_pyt
  • 5
  • 2
  • Does this answer your question? [Get gradients with respect to inputs in Keras ANN model](https://stackoverflow.com/questions/61968875/get-gradients-with-respect-to-inputs-in-keras-ann-model) – maciek97x Oct 28 '22 at 23:23
  • @maciek97x tnx for the comment. I am using a data generator and batch size to partition and feed my large data (~1,000000 rows and 150 columns) to the neural network. I don't know how to implement that code to my data since I don't know what would be my x and y in my case! training_generator = data_generatorp(df_array (normalized data), batch_size_gen = 20,000, target_col_idx (index of the target column), input_cols_idx, train_idx, partition_set = 'Training',gen_indices = True, shuffle=False) – jam_pyt Nov 02 '22 at 16:26

0 Answers0