Created
September 28, 2017 16:21
-
-
Save mickypaganini/a2291691924981212b4cfc8e600e52b1 to your computer and use it in GitHub Desktop.
Get magnitude of gradients from keras Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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))] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?