PyTorch--Tensor的创建

Tensor,即张量,是PyTorch中的基本操作对象,可以看做是包含单一数据类型元素的多维矩阵。从使用角度来看,Tensor与NumPy的ndarrays非常类似,相互之间也可以自由转换。

Tensor的创建:

1、torch.Tensor()

torch.Tensor 表示张量一个类,可以通过它的构造函数直接创建 Tensor 实例

使用 torch.Tensor() 创建 Tensor:

a = torch.Tensor(2, 3)  # 两行三列
print(a.dtype)
print(a)

# 运行结果:
# torch.float32
# tensor([[1.4586e-19, 1.1578e+27, 2.0780e-07],
#         [6.0542e+22, 7.8675e+34, 4.6894e+27]])

使用 torch.Tensor() 创建的 Tensor 的数据类型默认为 torch.float32(CPU/GPU类型为FloatTensor)。也可以使用 torch.DoubleTensor()、torch.IntTensor() 等其他张量类型的构造函数创建 Tensor 实例

2、torch.tensor()
b = torch.tensor(10)  # 创建出来的是0维张量(也就是一个标量)
print(b.dtype)
print(b)
print()
c = torch.tensor([[1, 2, 3], [2, 3, 4]])  # 用list列表
print(c.dtype)
print(c)
print()
d = torch.tensor([[1, 2, 3], [2, 3, 4]], dtype=torch.float32)  # torch数据类型指定为torch.float32
print(d.dtype)
print(d)
print()
e = torch.tensor([[1, 2.0, 3], [2, 3, 4]])  # 包含浮点数则默认推断为torch.float32
print(e.dtype)
print(e)
print()

# torch.int64
# tensor(10)
#
# torch.int64
# tensor([[1, 2, 3],
#         [2, 3, 4]])
#
# torch.float32
# tensor([[1., 2., 3.],
#         [2., 3., 4.]])
#
# torch.float32
# tensor([[1., 2., 3.],
#         [2., 3., 4.]])
3、随机创建 Tensor
a = torch.rand((2, 3))  # 创建在 [0.0, 1.0) 区间内均匀分布的一组随机数张量,形状(2,3)
print(a)
b = torch.randint(0, 9, (2, 3))  # 创建在 [0, 9) 区间内均匀分布的一组随机数整数张量,形状(2,3)
print(b)
c = torch.randn((3, 6))  # 创建符合 标准正态分布(均值为0,方差为1)的随机数张量,形状(3,6)
print(c)

# tensor([[0.9357, 0.0482, 0.2621],
#         [0.5435, 0.5679, 0.2222]])
# tensor([[5, 7, 0],
#         [4, 3, 3]])
# tensor([[ 0.1771,  0.1324,  1.0718,  0.0105,  0.1360, -0.7622],
#         [-0.7345, -0.5138,  1.3841, -0.9215,  2.3659, -0.8878],
#         [ 1.0106,  0.4476, -1.4537,  0.2541, -0.7330,  1.8842]])

d = torch.rand_like(a)  # 创建在 [0.0, 1.0) 区间内均匀分布的一组随机数张量,其形状与a相同。
print(d)
e = torch.randint_like(c, 9)  # 创建在 [0, 9) 区间内均匀分布的一组随机数整数张量,其形状与c相同
print(e)
f = torch.randn_like(c)  # 创建符合 标准正态分布(均值为0,方差为1)的随机数张量,其形状与输入的c相同。
print(f)
4、根据范围策略创建 Tensor
a = torch.arange(0, 10, 2)  # 在 [0, 10) 范围内,以 2 为步长创建一维张量
print(a)
b = torch.linspace(0, 10, 5)  # 在 [0, 10] 范围内,构造均匀分布的 等间距,有 5 个元素的一维张量。
print(b)
c = torch.logspace(0, 4, 5, 2)  # 生成一个大小为 5 的一维张量, 元素值为 2^x,其中 x 是 torch.linspace() 产生的序列值。
print(c)
c1 = torch.linspace(0, 4, 5)
print(c1)

# tensor([0, 2, 4, 6, 8])
# tensor([ 0.0000,  2.5000,  5.0000,  7.5000, 10.0000])
# tensor([ 1.,  2.,  4.,  8., 16.])
# tensor([0., 1., 2., 3., 4.])
5、创建特殊 Tensor
a = torch.zeros(3, 3)  # 创建全0张量
b = torch.ones(3, 4)  # 创建全1张量
c = torch.eye(4, 4)  # 创建单位张量
d = torch.full([2, 3], 6)  # 根据指定形状,填充指定数值
print(a)
print(b)
print(c)
print(d)

# tensor([[0., 0., 0.],
#         [0., 0., 0.],
#         [0., 0., 0.]])
# tensor([[1., 1., 1., 1.],
#         [1., 1., 1., 1.],
#         [1., 1., 1., 1.]])
# tensor([[1., 0., 0., 0.],
#         [0., 1., 0., 0.],
#         [0., 0., 1., 0.],
#         [0., 0., 0., 1.]])
# tensor([[6, 6, 6],
#         [6, 6, 6]])
numpy数组与张量的相互转化:
a = np.array([[1, 2], [3, 4]])  # numpy二维数组
print(type(a))
print(a)
print()
b = torch.tensor([[1, 2], [3, 4]])  # 二维tensor张量
print(type(b))
print(b)
print()
c = b.numpy()  # tensor张量转换成numpy数组
print(type(c))
print(c)
print()
d = torch.from_numpy(c)  # numpy数组转换成tensor张量
print(type(d))
print(d)

# <class 'numpy.ndarray'>
# [[1 2]
#  [3 4]]
# 
# <class 'torch.Tensor'>
# tensor([[1, 2],
#         [3, 4]])
# 
# <class 'numpy.ndarray'>
# [[1 2]
#  [3 4]]
# 
# <class 'torch.Tensor'>
# tensor([[1, 2],
#         [3, 4]])

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ww'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值