【PyTorch】张量的创建

1. 总结:

  • 0维
torch.tensor(5)
torch.Tensor(5)
torch.ones(())
torch.zeros(())
torch.full((), 7)
torch.rand(())
torch.randn(())
torch.randint(0,10,())
  • 1维
torch.tensor([1,2])
torch.arange(0,5)
torch.linspace(0,10,5)
torch.ones(3)
torch.zeros(3)
torch.rand(3)
  • 2维
torch.ones(2,3)
torch.zeros(2,3)
torch.rand(2,3)
torch.randint(0,10,(2,3))
  • 3维
torch.ones(2,2,3)
torch.zeros(2,2,3)
torch.rand(2,2,3)

2. 示例

    1. tensor() —— 自动识别 dtype
import torch

# 0维
t0 = torch.tensor(5)
print(t0)         # tensor(5)
print(t0.dim())   # 0

# 1维
t1 = torch.tensor([1, 2, 3])
print(t1)         # tensor([1, 2, 3])
print(t1.dim())   # 1

# 2维
t2 = torch.tensor([[1,2],[3,4]])
print(t2)
# tensor([[1, 2],
#         [3, 4]])
print(t2.dim())   # 2

# 3维
t3 = torch.tensor([[[1,2],[3,4]],[[5,6],[7,8]]])
print(t3)
# tensor([[[1, 2],
#          [3, 4]],
#
#         [[5, 6],
#          [7, 8]]])
print(t3.dim())   # 3
    1. Tensor () —— 强制 float32(不推荐)
import torch

# 0维
t0 = torch.Tensor([5])
print(t0)      # tensor([5.])

# 1维
t1 = torch.Tensor([1,2,3])
print(t1)      # tensor([1., 2., 3.])

# 2维
t2 = torch.Tensor([[1,2],[3,4]])
print(t2)
# tensor([[1., 2.],
#         [3., 4.]])

# 3维
t3 = torch.Tensor([[[1,2],[3,4]],[[5,6],[7,8]]])
print(t3)
# tensor([[[1., 2.],
#          [3., 4.]],
#
#         [[5., 6.],
#          [7., 8.]]])
    1. arange () —— int64,左闭右开
    • 【注意】:arange 天然 1 维,高维靠 reshape 修改张量形状
import torch

# 1维
t1 = torch.arange(0, 5)
print(t1)   # tensor([0, 1, 2, 3, 4])

# 2维
t2 = torch.arange(0, 6).reshape(2,3)
print(t2)
# tensor([[0, 1, 2],
#         [3, 4, 5]])

# 3维
t3 = torch.arange(0, 12).reshape(2,2,3)
print(t3)
# tensor([[[ 0,  1,  2],
#          [ 3,  4,  5]],
#
#         [[ 6,  7,  8],
#          [ 9, 10, 11]]])
    1. linspace () —— float32,均分
    • 【同arange】:天然 1 维,高维靠 reshape 修改张量形状
import torch

# 1维 0~10 均分5个点
t1 = torch.linspace(0, 10, 5)
print(t1)   # tensor([ 0.0000,  2.5000,  5.0000,  7.5000, 10.0000])

# 2维
t2 = torch.linspace(0, 10, 6).reshape(2,3)
print(t2)
# tensor([[ 0.0000,  2.0000,  4.0000],
#         [ 6.0000,  8.0000, 10.0000]])

# 3维
t3 = torch.linspace(0, 10, 12).reshape(2,2,3)
print(t3)
# tensor([[[ 0.0000,  0.9091,  1.8182],
#          [ 2.7273,  3.6364,  4.5455]],
#
#         [[ 5.4545,  6.3636,  7.2727],
#          [ 8.1818,  9.0909, 10.0000]]])
    1. zeros / zeros_like
import torch

# 0维
t0 = torch.zeros(())
print(t0)      # tensor(0.)

# 1维
t1 = torch.zeros(3)
print(t1)      # tensor([0., 0., 0.])

# 2维
t2 = torch.zeros(2,3)
print(t2)
# tensor([[0., 0., 0.],
#         [0., 0., 0.]])

# 3维
t3 = torch.zeros(2,2,3)
print(t3)
# tensor([[[0., 0., 0.],
#          [0., 0., 0.]],
#
#         [[0., 0., 0.],
#          [0., 0., 0.]]])

# zeros_like
x = torch.tensor([[1,2],[3,4]])
tl = torch.zeros_like(x)
print(tl)
# tensor([[0, 0],
#         [0, 0]])
    1. ones / ones_like
import torch

# 0维
t0 = torch.ones(())
print(t0)      # tensor(1.)

# 1维
t1 = torch.ones(3)
print(t1)      # tensor([1., 1., 1.])

# 2维
t2 = torch.ones(2,3)
print(t2)
# tensor([[1., 1., 1.],
#         [1., 1., 1.]])

# 3维
t3 = torch.ones(2,2,3)
print(t3)
# tensor([[[1., 1., 1.],
#          [1., 1., 1.]],
#
#         [[1., 1., 1.],
#          [1., 1., 1.]]])

# ones_like
x = torch.tensor([[5,6],[7,8]])
tl = torch.ones_like(x)
print(tl)
# tensor([[1, 1],
#         [1, 1]])
    1. full /full_like —— 填充固定值
import torch

# 0维 填充7
t0 = torch.full((), 7)
print(t0)      # tensor(7)

# 1维
t1 = torch.full((3,), 7)
print(t1)      # tensor([7, 7, 7])

# 2维
t2 = torch.full((2,3), 7)
print(t2)
# tensor([[7, 7, 7],
#         [7, 7, 7]])

# 3维
t3 = torch.full((2,2,3), 7)
print(t3)
# tensor([[[7, 7, 7],
#          [7, 7, 7]],
#
#         [[7, 7, 7],
#          [7, 7, 7]]])

# full_like
x = torch.randn(2,2)
tl = torch.full_like(x, 99)
print(tl)
# 全是99的2维浮点张量
    1. rand () 均匀分布 [0,1)
import torch

t0 = torch.rand(())
print(t0)   # 标量小数,如 tensor(0.4963)

t1 = torch.rand(3)
print(t1)   # tensor([0.7682, 0.0885, 0.1321])

t2 = torch.rand(2,3)
print(t2)
t3 = torch.rand(2,2,3)
print(t3)
    1. randn () 标准正态分布 (-1,1)
import torch

# 0维
t0 = torch.randn(())
print(t0)  # tensor(0.1587)

# 1维
t1 = torch.randn(3)
print(t1)  # tensor([-0.3421, -0.2346,  1.5863])

# 2维
t2 = torch.randn(2,3)

# 3维
t3 = torch.randn(2,2,3)
    1. randint () 随机整数张量
import torch

# 0~10 随机整数
t0 = torch.randint(0, 10, ())
print(t0)  # 单个整数标量

t1 = torch.randint(0, 10, (3,))
print(t1)  # tensor([6, 3, 7])

t2 = torch.randint(0, 10, (2,3))
t3 = torch.randint(0, 10, (2,2,3))
    1. 随机种子
import torch

# 获取当前种子
s = torch.random.initial_seed()
print(s)

# 固定种子,结果可复现
torch.manual_seed(2025)
posted @ 2026-05-22 19:17  静心笃行。  阅读(4)  评论(0)    收藏  举报