torch和numpy使用之间的区别

torch和numpy非常相似,只有少部分函数在使用时候返回的值有些区别。今天在实验的时候踩了坑,这里记录一下

import numpy as np
import torch
a = torch.rand(3,3)
print("***torch****{}".format(a))
ee = a.max(1)
print("****torch****{}".format(ee))
b = np.random.rand(3,3)
print(b)
c = b.max(1)
print(c)

关于max 的函数返回,在numpy里直接把每一行的最大写进一个array;而关于torch是返回tuple,只有在max之后在取[0]才能拿出最大的数组

***torch****tensor([[0.8228, 0.8324, 0.9386],
        [0.7421, 0.7891, 0.8004],
        [0.9199, 0.3035, 0.0836]])
****torch****(tensor([0.9386, 0.8004, 0.9199]), tensor([2, 2, 0]))
[[0.08102018 0.47674189 0.37479386]
 [0.34632241 0.37017852 0.36496977]
 [0.74771679 0.23109616 0.07765254]]
[0.47674189 0.37017852 0.74771679]

猜你喜欢

转载自blog.csdn.net/weixin_38267508/article/details/89314265