Pytorch中的dim和Tensorflow中的axis有什么区别?

时间:2020-06-11 20:30:30

标签: tensorflow deep-learning pytorch tensor

我有两条线,我想了解它们是否会产生相同的输出?
在tensorflow中:tf.norm(my_tensor, ord=2, axis=1)
在pytorch中:torch.norm(my_tensor, p=2, dim=1)
说my_tensor的形状为[100,2]

以上两行给出的结果是否相同?还是 axis属性不同于dim

1 个答案:

答案 0 :(得分:1)

是的,它们是相同的!

import tensorflow as tf
tensor = [[1., 2.], [4., 5.], [3., 6.], [7., 8.], [5., 2.]]
tensor = tf.convert_to_tensor(tensor, dtype=tf.float32)
t_norm = tf.norm(tensor, ord=2, axis=1)
print(t_norm)

输出

tf.Tensor([ 2.236068   6.4031243  6.708204  10.630146   5.3851647], shape=(5,), dtype=float32)
import torch
tensor = [[1., 2.], [4., 5.], [3., 6.], [7., 8.], [5., 2.]]
tensor = torch.tensor(tensor, dtype=torch.float32)
t_norm = torch.norm(tensor, p=2, dim=1)
print(t_norm)

输出

tensor([ 2.2361,  6.4031,  6.7082, 10.6301,  5.3852])