【问题标题】:What's the difference between dim in Pytorch and axis in Tensorflow?Pytorch中的dim和Tensorflow中的axis有什么区别?
【发布时间】:2020-10-01 14:36:18
【问题描述】:

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

以上两行会给出相同的结果吗?还是轴属性和dim不一样

【问题讨论】:

    标签: tensorflow deep-learning pytorch tensor


    【解决方案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])
    

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 2023-02-14
      • 2014-04-02
      • 2019-12-23
      • 2018-09-13
      • 2021-09-17
      • 1970-01-01
      相关资源
      最近更新 更多