Created
June 11, 2021 01:47
-
-
Save EdisonLeeeee/0495d853ffbd51e7e717b6e90ea68169 to your computer and use it in GitHub Desktop.
tensorflow clip_by_norm in pytorch and numpy
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
import numpy as np | |
import torch | |
import tensorflow as tf | |
import numpy as np | |
import torch | |
import tensorflow as tf | |
# Numpy | |
np.random.seed(42) | |
x = np.random.randn(10, 10) | |
l2_norm = np.linalg.norm(x) | |
print(l2_norm) # 9.095637931826113 | |
clip_norm = 5.0 | |
transformed_x = x * clip_norm / l2_norm | |
l2_norm = np.linalg.norm(transformed_x) | |
print(l2_norm) # 5.0 | |
# pytorch | |
torch_x = torch.tensor(x) | |
l2_norm = torch.norm(torch_x, p=2) | |
transformed_x = torch_x * clip_norm / l2_norm | |
l2_norm = torch.norm(transformed_x, p=2) | |
print(l2_norm) # 5.0 | |
# tensorflow | |
transformed_x = tf.clip_by_norm(x, 5.0) | |
print(tf.norm(transformed_x, ord=2)) # 5.0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment