Skip to content

Instantly share code, notes, and snippets.

@mickypaganini
Created September 28, 2017 16:21
Show Gist options
  • Save mickypaganini/a2291691924981212b4cfc8e600e52b1 to your computer and use it in GitHub Desktop.
Save mickypaganini/a2291691924981212b4cfc8e600e52b1 to your computer and use it in GitHub Desktop.
Get magnitude of gradients from keras Model
# model is a keras Model
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)
inputs = [X_1, # X input data
[1], # sample weights
[[1]], # y labels
0 # learning phase in TEST mode
]
print [a for a in zip(weights, get_gradients(inputs))]
@getamu
Copy link

getamu commented Oct 2, 2018

What if I want to get the weights for all the points in training_data?
Should I pass my training data vector and label in inputs? What will be sample weights?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment