pytorch函数 - ge,cat,randn

torch.ge(input, other, out=None)

对比每一个input和other是否有如下关系 input other \text{input} \geq \text{other} ,input和other均为tensor,输出为一个二值tensor。

举例:

torch.ge(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))

	out:	tensor([[ 1,  1],
            [ 0,  1]], dtype=torch.uint8)

torch生成随机数

  • torch.randn(sizes, out=None)
    返回服从标准正态分布的一组随机数,形状由参数size确定,返回类型为张量。

sizes (int…) - 整数序列,定义了输出张量的形状
out (Tensor, optinal) - 结果张量

举例:

torch.randn(2, 3)

  • torch.rand(sizes, out=None)
    返回服从(0,1)均匀分布的一组随机数,形状由参数size确定,返回类型为张量。
    举例:

torch.rand(2, 3)

  • torch.normal(mean, std, out=None)
    返回服从指定均值means和标准差std的离散正态分布的一组随机数,返回类型为张量;
    均值和标准差均是一个张量,mean包含每个输出元素相关的正态分布均值,std包含每个输出元素相关的正态分布标准差;
    参数均值和标准差的shape>1时,shape要一样!
    举例:

torch.normal(mean=0.5, std=torch.arange(1, 6))
torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))

  • torch.linspace(start,end,steps=100,out=None)
    返回 区间start和end上均匀间隔的step个点,输出长度由step决定,返回为一个一维张量。
    举例

torch.linspace(3, 10, steps=5)

torch堆叠函数

  • cat(tensors, dim=0, out=None)
    对数据沿着某一维度进行拼接
    举例:对两个2维tensor(2乘3,1乘3)进行拼接

x = torch.randn(2,3)
y = torch.randn(1,3)
torch.cat((x,y),0) #按列拼接

  • stack(seq, dim=0, out=None)
    如对两个1乘2维的tensor在第0个维度上stack,则会变为2乘1乘2的tensor;在第1个维度上stack,则会变为1乘2 乘2的tensor。

a=torch.rand((1,2))
b=torch.rand((1,2))
c=torch.stack((a,b),0)

参考资料

1)生成随机数Tensor的方法汇总
2)torch中的堆叠函数

猜你喜欢

转载自blog.csdn.net/qq_39446239/article/details/89165065
Ge