torch.ones,normal,max

torch.normal(means, std, out=None)

  • means (Tensor) – 均值
  • std (Tensor) – 标准差
  • out (Tensor) – 可选的输出张量
>>> n_data = torch.ones(5, 2)
>>> print(n_data)
    tensor([[1., 1.],
            [1., 1.],
            [1., 1.],
            [1., 1.],
            [1., 1.]])
>>> print(n_data.shape)
torch.Size([5, 2])
>>> x0 = torch.normal(2*n_data, 1)
>>> print(x0)
    tensor([[3.2688, 1.4834],
            [1.8288, 0.7327],   
            [3.2382, 4.0835],
            [2.8337, 2.1901],
            [3.3097, 2.4447]])
#每个元素是从 均值=2*n_data中对应位置的取值,标准差为1的正态分布中随机生成的
     
>>> print(2*n_data)
    tensor([[2., 2.],
            [2., 2.],
            [2., 2.],
            [2., 2.],
            [2., 2.]])


torch.max(参数1, 1)[1]

torch.max()返回两个结果,第一个是最大值,第二个是对应的索引值;第二个参数 0 代表按列取最大值并返回对应的行索引值,1 代表按行取最大值并返回对应的列索引值。

torch.max()[0], 只返回最大值的每个数

troch.max()[1], 只返回最大值的每个索引

torch.max()[1].data 只返回variable中的数据部分(去掉Variable containing:)

torch.max()[1].data.numpy() 把数据转化成numpy ndarry

torch.max()[1].data.numpy().squeeze() 把数据条目中维度为1 的删除掉

torch.max(tensor1,tensor2) element-wise 比较tensor1 和tensor2 中的元素,返回较大的那个值
发布了888 篇原创文章 · 获赞 93 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/weixin_36670529/article/details/104031215