Pytorch创建张量


在这里插入图片描述


1.torch.from_numpy()

torch.from_numpy(ndarray)
"""
ndarray:NumPy 数组对象,用于创建对应的张量。
"""
import numpy as np
import torch

# 创建一个 NumPy 数组
numpy_array = np.array([1, 2, 3, 4, 5])

# 使用 torch.from_numpy() 创建张量
tensor = torch.from_numpy(numpy_array)

print(tensor)  # 输出: tensor([1, 2, 3, 4, 5])
print(type(tensor))  # 输出: <class 'torch.Tensor'>

# 修改张量
tensor[0] = 10

print(tensor)  # 输出: tensor([10,  2,  3,  4,  5])

# 修改原始 NumPy 数组
print(numpy_array)  # 输出: [10  2  3  4  5]
"""
在 NumPy 中,默认情况下,当打印一个一维数组时,元素之间是不带逗号分隔的。
这是 NumPy 打印数组的默认行为。
如果希望在打印 NumPy 数组时显示逗号分隔的元素可以
使用 numpy.set_printoptions() 方法来更改打印选项。
"""

2. torch.zeros()

  torch.zeros() 函数是 PyTorch 中用于创建元素全为零的张量的函数。

torch.zeros(*size, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:
*size:表示张量的形状,可以是一个整数或一个元组。
dtype:表示张量的数据类型,默认为 torch.float32。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个形状为 (2, 3) 的零张量
zeros_tensor = torch.zeros(2, 3)
print(zeros_tensor)
# 输出:
# tensor([[0., 0., 0.],
#         [0., 0., 0.]])

# 创建一个形状为 (3, 4, 2) 的零张量,数据类型为整数
zeros_int_tensor = torch.zeros((3, 4, 2), dtype=torch.int)
print(zeros_int_tensor)
# 输出:
# tensor([[[0, 0],
#          [0, 0],
#          [0, 0],
#          [0, 0]],
#
#         [[0, 0],
#          [0, 0],
#          [0, 0],
#          [0, 0]],
#
#         [[0, 0],
#          [0, 0],
#          [0, 0],
#          [0, 0]]], dtype=torch.int32)

3. torch.ones()

  torch.ones() 函数是 PyTorch 中用于创建元素全为一的张量的函数。

torch.ones(*size, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:
*size:表示张量的形状,可以是一个整数或一个元组。
dtype:表示张量的数据类型,默认为 torch.float32。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个形状为 (2, 3) 的全为一的张量
ones_tensor = torch.ones(2, 3)
print(ones_tensor)
# 输出:
# tensor([[1., 1., 1.],
#         [1., 1., 1.]])

# 创建一个形状为 (3, 4, 2) 的全为一的张量,数据类型为整数
ones_int_tensor = torch.ones((3, 4, 2), dtype=torch.int)
print(ones_int_tensor)
# 输出:
# tensor([[[1, 1],
#          [1, 1],
#          [1, 1],
#          [1, 1]],
#
#         [[1, 1],
#          [1, 1],
#          [1, 1],
#          [1, 1]],
#
#         [[1, 1],
#          [1, 1],
#          [1, 1],
#          [1, 1]]], dtype=torch.int32)

4. torch.arange()

  PyTorch 提供了一个名为 torch.arange() 的函数,用于创建具有指定范围内连续值的张量。

torch.arange(start, end=None, step=1, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:
start:起始值(包含)。
end:结束值(不包含)。
step:步长,默认为 1。
dtype:表示张量的数据类型,默认为 None,即自动推断。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个从 0 到 9 的张量
tensor = torch.arange(10)
print(tensor)
# 输出: tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

# 创建一个从 5 到 15 步长为 2 的张量
tensor = torch.arange(5, 15, 2)
print(tensor)
# 输出: tensor([ 5,  7,  9, 11, 13])

5. torch.linspace()

torch.linspace() 是 PyTorch 中用于创建一维等间隔数值的张量的函数

torch.linspace(start, end, steps=100, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:
start:起始值。
end:结束值。
steps:生成的数值的数量,默认为 100。
dtype:表示张量的数据类型,默认为 None,即自动推断。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个从 0 到 1(包含 1)的等间隔张量,共有 5 个数值
tensor = torch.linspace(0, 1, 5)
print(tensor)
# 输出: tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000])

# 创建一个从 10 到 20(包含 20)的等间隔张量,共有 11 个数值,数据类型为整数
tensor = torch.linspace(10, 20, 11, dtype=torch.int)
print(tensor)
# 输出: tensor([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], dtype=torch.int32)

6. torch.logspace()

torch.logspace(start, end, steps=100, base=10.0, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:

start:起始指数。
end:结束指数。
steps:生成的数值的数量,默认为 100。
base:对数的底数,默认为 10.0。
dtype:表示张量的数据类型,默认为 None,即自动推断。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个在 1e-2 到 1e2 之间的对数刻度张量,共有 5 个数值
tensor = torch.logspace(start=-2, end=2, steps=5)
print(tensor)
# 输出: tensor([  0.0100,   0.1000,   1.0000,  10.0000, 100.0000])

# 创建一个在 1 到 10^3 之间的以 2 为底的对数刻度张量,共有 4 个数值
tensor = torch.logspace(start=0, end=3, steps=4, base=2)
print(tensor)
# 输出: tensor([ 1.0000,  2.0000,  4.0000,  8.0000])

7. torch.eye()

 PyTorch 提供了一个名为 torch.eye() 的函数,用于创建单位矩阵的张量。

torch.eye(n, m=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:

n:生成的单位矩阵的行数。
m:生成的单位矩阵的列数。如果未指定,则默认为 n,即生成一个 n x n 的方阵。
out:输出张量的可选位置。
dtype:表示张量的数据类型,默认为 None,即自动推断。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个 3x3 的单位矩阵
eye_tensor = torch.eye(3)
print(eye_tensor)
# 输出:
# tensor([[1., 0., 0.],
#         [0., 1., 0.],
#         [0., 0., 1.]])

# 创建一个 4x4 的单位矩阵,数据类型为整数
eye_int_tensor = torch.eye(4, dtype=torch.int)
print(eye_int_tensor)
# 输出:
# tensor([[1, 0, 0, 0],
#         [0, 1, 0, 0],
#         [0, 0, 1, 0],
#         [0, 0, 0, 1]], dtype=torch.int32)

8. torch.empty()

torch.empty() 是 PyTorch 中用于创建一个未初始化的张量(tensor)的函数。

torch.empty(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:

*size:表示张量的形状,可以是一个整数或一个元组。
out:输出张量的可选位置。
dtype:表示张量的数据类型,默认为 None,即自动推断。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个形状为 (2, 3) 的未初始化张量
empty_tensor = torch.empty(2, 3)
print(empty_tensor)
# 输出:
# tensor([[1.8751e+34, 1.8788e+31, 3.0881e-41],
#         [0.0000e+00, 0.0000e+00, 0.0000e+00]])

# 创建一个形状为 (3, 4, 2) 的未初始化张量,数据类型为整数
empty_int_tensor = torch.empty((3, 4, 2), dtype=torch.int)
print(empty_int_tensor)
# 输出:
# tensor([[[  875812,   875812],
#          [  875812,   875812],
#          [  875812,   875812],
#          [  875812,   875812]],
#
#         [[  875812,   875812],
#          [  875812,   875812],
#          [  875812,   875812],
#          [  875812,   875812]],
#
#         [[  875812,   875812],
#          [  875812,   875812],
#          [  875812,   875812],
#          [  875812,   875812]]], dtype=torch.int32)

9. torch.full()

torch.full() 是 PyTorch 中用于创建指定形状和值的张量的函数。

torch.full(size, fill_value, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:
size:表示张量的形状,可以是一个整数或一个元组。
fill_value:表示要填充的值。
out:输出张量的可选位置。
dtype:表示张量的数据类型,默认为 None,即自动推断。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个形状为 (2, 3) 的张量,所有元素值均为 5.0
full_tensor = torch.full((2, 3), 5.0)
print(full_tensor)
# 输出:
# tensor([[5., 5., 5.],
#         [5., 5., 5.]])

# 创建一个形状为 (3, 2) 的张量,所有元素值均为 -1,数据类型为整数
full_int_tensor = torch.full((3, 2), -1, dtype=torch.int)
print(full_int_tensor)
# 输出:
# tensor([[-1, -1],
#         [-1, -1],
#         [-1, -1]], dtype=torch.int32)

10. torch.complex()

torch.complex() 是 PyTorch 中用于创建复数张量的函数。

torch.complex(real, imag)
参数说明:
real:实部的张量。
imag:虚部的张量。
import torch

# 创建实部和虚部的张量
real = torch.tensor([1.0, 2.0])
imag = torch.tensor([0.5, -0.5])

# 创建复数张量
complex_tensor = torch.complex(real, imag)

print(complex_tensor)
# 输出:
# tensor([1.+0.5000j, 2.-0.5000j])

10. torch.rand()

torch.rand() 是 PyTorch 中用于创建具有随机值的张量的函数。

torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:
*size:表示张量的形状,可以是一个整数或一个元组。
out:输出张量的可选位置。
dtype:表示张量的数据类型,默认为 None,即自动推断。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个形状为 (2, 3) 的随机值张量
rand_tensor = torch.rand(2, 3)
print(rand_tensor)
# 输出:
# tensor([[0.9184, 0.1503, 0.7769],
#         [0.8832, 0.6201, 0.0705]])

# 创建一个形状为 (3, 4, 2) 的随机值张量,数据类型为整数
rand_int_tensor = torch.rand((3, 4, 2), dtype=torch.int)
print(rand_int_tensor)
# 输出:
# tensor([[[0, 0],
#          [0, 0],
#          [0, 0],
#          [0, 0]],
#
#         [[0, 0],
#          [0, 0],
#          [0, 0],
#          [0, 0]],
#
#         [[0, 0],
#          [0, 0],
#          [0, 0],
#          [0, 0]]], dtype=torch.int32)

10. torch.randint()

torch.randint() 是 PyTorch 中用于创建具有随机整数值的张量的函数。

torch.randint(low, high, size, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:

high:表示随机整数范围的上界(不包含)。
low:可选参数,表示随机整数范围的下界(包含)。如果未提供 low 参数,则默认为 0。
size:表示张量的形状,可以是一个整数或一个元组。
dtype:表示张量的数据类型,默认为 None,即自动推断。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个形状为 (2, 3) 的随机整数张量,值范围为 [0, 10)
rand_int_tensor = torch.randint(10, (2, 3))
print(rand_int_tensor)
# 输出:
# tensor([[9, 2, 8],
#         [6, 1, 0]])

# 创建一个形状为 (3, 4, 2) 的随机整数张量,值范围为 [1, 6)
rand_int_tensor2 = torch.randint(1, 6, (3, 4, 2))
print(rand_int_tensor2)
# 输出:
# tensor([[[3, 4],
#          [4, 5],
#          [2, 2],
#          [5, 1]],
#
#         [[1, 5],
#          [3, 4],
#          [5, 4],
#          [4, 3]],
#
#         [[3, 5],
#          [4, 4],
#          [3, 2],
#          [2, 4]]])

# 创建一个形状为 (3,) 的随机整数张量,值范围为 [-5, 5]
rand_int_tensor3 = torch.randint(-5, 6, (3,))
print(rand_int_tensor3)
# 输出:
# tensor([-1,  4,  5])

11. torch.randn

torch.randn() 是 PyTorch 中用于创建具有正态分布(高斯分布)随机值的张量的函数。

torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:
*size:表示张量的形状,可以是一个整数或一个元组。
out:输出张量的可选位置。
dtype:表示张量的数据类型,默认为 None,即自动推断。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个形状为 (2, 3) 的正态分布随机值张量
rand_tensor = torch.randn(2, 3)
print(rand_tensor)
# 输出:
# tensor([[ 0.1768, -0.1565, -0.3723],
#         [ 0.1222,  0.0453, -0.6545]])

# 创建一个形状为 (3, 4, 2) 的正态分布随机值张量,数据类型为整数
rand_int_tensor = torch.randn((3, 4, 2), dtype=torch.int)
print(rand_int_tensor)
# 输出:
# tensor([[[ 1,  0],
#          [-1, -1],
#          [-1,  0],
#          [-1,  1]],
#
#         [[ 0, -1],
#          [-1, -1],
#          [ 0,  1],
#          [ 0,  1]],
#
#         [[ 0,  0],
#          [-1,  0],
#          [ 0,  0],
#          [ 0,  0]]], dtype=torch.int32)

12. torch.normal()

torch.normal() 是 PyTorch 中用于创建具有指定均值和标准差的正态分布随机值的张量的函数。

torch.normal(mean, std, size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:
mean:表示正态分布的均值。
std:表示正态分布的标准差。
size:表示张量的形状,可以是一个整数或一个元组。
out:输出张量的可选位置。
dtype:表示张量的数据类型,默认为 None,即自动推断。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个形状为 (2, 3) 的正态分布随机值张量,均值为 0,标准差为 1
rand_tensor = torch.normal(0.0, 1.0, (2, 3))
print(rand_tensor)
# 输出:
# tensor([[ 0.2025, -1.2349, -0.8624],
#         [ 0.5945,  0.4696, -0.9300]])

# 创建一个形状为 (3, 4, 2) 的正态分布随机值张量,均值为 2,标准差为 0.5
rand_tensor2 = torch.normal(2.0, 0.5, (3, 4, 2))
print(rand_tensor2)
# 输出:
# tensor([[[ 1.7426,  2.8180],
#          [ 2.0582,  2.4961],
#          [ 2.4627,  2.1690],
#          [ 2.5418,  1.4430]],
#
#         [[ 1.6425,  1.7197],
#          [ 1.7202,  2.2826],
#          [ 2.1842,  2.1941],
#          [ 2.4640,  1.9483]],
#
#         [[ 1.6387,  2.2361],
#          [ 2.2822,  2.1503],
#          [ 2.0832,  1.5682],
#          [ 2.8316,  2.3017]]])

13. torch.uniform()

torch.uniform() 是 PyTorch 中用于创建具有均匀分布随机值的张量的函数。

torch.uniform(from, to, size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
"""
参数说明:
from:表示均匀分布的下界(包含)。
to:表示均匀分布的上界(不包含)。
size:表示张量的形状,可以是一个整数或一个元组。
out:输出张量的可选位置。
dtype:表示张量的数据类型,默认为 None,即自动推断。
layout:表示张量的布局,默认为 torch.strided。
device:表示张量所在的设备,默认为使用当前设备。
requires_grad:表示是否需要计算梯度,默认为 False。
"""
import torch

# 创建一个形状为 (2, 3) 的均匀分布随机值张量,范围为 [0, 1)
rand_tensor = torch.uniform(0.0, 1.0, (2, 3))
print(rand_tensor)
# 输出:
# tensor([[0.6585, 0.6943, 0.3527],
#         [0.5127, 0.9489, 0.2360]])

# 创建一个形状为 (3, 4, 2) 的均匀分布随机值张量,范围为 [-1, 1)
rand_tensor2 = torch.uniform(-1.0, 1.0, (3, 4, 2))
print(rand_tensor2)
# 输出:
# tensor([[[ 0.4801,  0.7084],
#          [-0.9697, -0.7110],
#          [ 0.1171,  0.4388],
#          [ 0.0897,  0.0358]],
#
#         [[ 0.1239, -0.1015],
#          [ 0.3444,  0.3462],
#          [-0.2054,  0.0474],
#          [-0.3595,  0.5682]],
#
#         [[-0.9373,  0.5579],
#          [ 0.6187, -0.1847],
#          [ 0.3249, -0.0422],
#          [-0.1907,  0.3521]]])

14. torch.as_tensor()

torch.as_tensor() 是 PyTorch 中的一个函数,用于将输入数据转换为张量。

torch.as_tensor(data, dtype=None, device=None) -> Tensor
"""
data:输入数据,可以是 Python 列表、NumPy 数组、标量或其他支持的数据类型。
dtype:可选参数,指定输出张量的数据类型。如果未提供,则会尝试从输入数据中推断数据类型。
device:可选参数,指定输出张量所在的设备。如果未提供,则使用默认设备。
"""

torch.as_tensor() 函数接受输入数据并返回一个张量。它具有以下行为:

  • 如果输入数据已经是 Tensor 类型,它将返回同一张量,不进行复制。
  • 如果输入数据是一个支持数组协议的对象(如 NumPy 数组),它将使用共享内存创建一个张量,而不进行数据复制。
  • 如果输入数据是一个 Python 列表或标量,它将创建一个新的张量,并将数据复制到该张量中。
import torch
import numpy as np

# 从 Python 列表创建张量
data_list = [1, 2, 3, 4, 5]
tensor_list = torch.as_tensor(data_list)
print(tensor_list)

# 从 NumPy 数组创建张量
data_np = np.array([1, 2, 3, 4, 5])
tensor_np = torch.as_tensor(data_np)
print(tensor_np)

# 从标量创建张量
data_scalar = 3.14
tensor_scalar = torch.as_tensor(data_scalar)
print(tensor_scalar)
tensor([1, 2, 3, 4, 5])
tensor([1, 2, 3, 4, 5])
tensor(3.1400)

15. torch.sparse_coo_tensor()

  torch.sparse_coo_tensor() 是 PyTorch 中用于创建稀疏张量的函数。稀疏张量是一种优化表示稀疏数据的数据结构,其中只有部分元素非零。

torch.sparse_coo_tensor(indices, values, size=None, dtype=None, device=None, requires_grad=False) -> Tensor
"""
indices:一个包含非零元素索引的张量,形状为 (N, D),其中 N 是非零元素的数量,D 是张量的维度。
values:一个包含非零元素的值的张量,形状为 (N,)。
size:可选参数,指定稀疏张量的形状。如果未提供,则根据索引中的最大值自动推断形状。
dtype:可选参数,指定稀疏张量的数据类型。如果未提供,则根据输入值的数据类型自动推断。
device:可选参数,指定稀疏张量所在的设备。如果未提供,则使用默认设备。
requires_grad:可选参数,指定是否需要计算梯度。默认为 False。
"""
import torch

# Create sparse tensor
indices = torch.tensor([[0, 1, 2], [1, 2, 0]])
values = torch.tensor([1.0, 2.0, 3.0])
size = (3, 3)

sparse_tensor = torch.sparse_coo_tensor(indices, values, size)

print(sparse_tensor)

16. torch.bernoulli()

torch.bernoulli() 是 PyTorch 中用于生成服从伯努利分布的张量的函数。伯努利分布是一种离散概率分布,它的取值只有两种可能,通常被表示为成功(取值为 1)和失败(取值为 0)。

torch.bernoulli(input, *, generator=None, out=None)
"""
input:一个张量,指定了伯努利分布的概率参数。input 中的每个元素都表示对应位置上成功的概率,取值范围为 [0, 1]。
generator:可选参数,一个随机数生成器对象,用于生成随机数。如果未提供,将使用默认的全局随机数生成器。
out:可选参数,用于指定输出张量的位置。
"""
import torch

# 生成服从伯努利分布的张量
probabilities = torch.tensor([0.3, 0.6, 0.8])
result = torch.bernoulli(probabilities)

print(result)
tensor([0., 1., 1.])

17. torch.multinomial()

torch.multinomial()是PyTorch中用于从多项分布中生成随机样本的函数。多项式分布是一种常见的概率分布,它描述了具有多个离散可能结果的试验的概率分布。

torch.multinomial(input, num_samples, replacement=False, generator=None, out=None)
"""
input:一个张量,表示每个类别的概率分布。input的形状可以是(N, K),其中N是样本数,K是类别数,或者可以是一个形状为(K,)的一维张量,其中K是类别数。
num_samples:要生成的样本数。
replacement:一个布尔值,指定是否可以有重复的样本。如果为True,则允许有重复样本;如果为False,则不允许有重复样本。
generator:可选参数,一个随机数生成器对象,用于生成随机数。如果未提供,将使用默认的全局随机数生成器。
out:可选参数,用于指定输出张量的位置。
"""
import torch

# 从多项分布中生成随机样本
probabilities = torch.tensor([0.2, 0.3, 0.5])
num_samples = 4

samples = torch.multinomial(probabilities, num_samples, replacement=True)

print(samples)
tensor([1, 0, 1, 0])

17. torch.poisson()

torch.poisson() 是 PyTorch 中用于生成服从泊松分布的张量的函数。泊松分布是一种离散概率分布,用于描述在一定时间或空间间隔内,事件发生的次数的概率分布。

torch.poisson(input, generator=None, out=None)
"""
input:一个张量,指定了泊松分布的概率参数。input 中的每个元素都表示对应位置上的平均事件发生次数。
generator:可选参数,一个随机数生成器对象,用于生成随机数。如果未提供,将使用默认的全局随机数生成器。
out:可选参数,用于指定输出张量的位置。
"""
import torch

# 生成服从泊松分布的张量
rate = torch.tensor([1.0, 2.0, 3.0])
result = torch.poisson(rate)

print(result)
tensor([1., 5., 3.])

18. torch.randperm()

torch.randperm() 是 PyTorch 中用于生成随机排列的函数。它返回一个从 0 到 n-1 的整数序列的随机排列,其中 n 是输入的参数。

torch.randperm(n, generator=None, out=None, dtype=torch.int64)
"""
n:一个非负整数,用于确定生成的随机排列的长度。
generator:可选参数,一个随机数生成器对象,用于生成随机数。如果未提供,将使用默认的全局随机数生成器。
out:可选参数,用于指定输出张量的位置。
dtype:可选参数,输出张量的数据类型,默认为 torch.int64。
"""
import torch

# 生成一个随机排列的整数序列
n = 5
perm = torch.randperm(n)

print(perm)
# tensor([2, 1, 3, 0, 4])
  • 33
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值