Skip to content

Instantly share code, notes, and snippets.

@sayakpaul
Created June 21, 2020 15:53
Show Gist options
  • Save sayakpaul/f1a07c1850ee9c1d1a29c6e0a249c88b to your computer and use it in GitHub Desktop.
Save sayakpaul/f1a07c1850ee9c1d1a29c6e0a249c88b to your computer and use it in GitHub Desktop.
for layer in model.layers:
if isinstance(layer, pruning_wrapper.PruneLowMagnitude):
for weight in layer.layer.get_prunable_weights():
print(np.allclose(
target_sparsity, get_sparsity(tf.keras.backend.get_value(weight)),
rtol=1e-6, atol=1e-6)
)
def get_sparsity(weights):
return 1.0 - np.count_nonzero(weights) / float(weights.size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment