与张量创建方法

时间:2025-01-22 21:24:45

 ()   创建任意数据类型的张量

 () 只创建类型的张量

 所以() 是() 的特例

empty()返回一个包含未初始化数据的张量。使用参数可以指定张量的形状、输出张量、数据类型。

举例

empty = (2, 3)  # Returns a tensor filled with uninitialized data.
print(empty)  # 每一次跑的结果都不一样
# tensor([[2.6999e-06, 1.7377e-04, 1.0431e-08],
#         [4.0046e-11, 1.4013e-45, 2.6407e-06]])

===========================================================================

tensor = ([2, 8])  # Constructs a tensor with data
print(tensor)  # tensor([2, 8])

# WARNING(注意!!!)
# () always copies data.
# If you have a Tensor data and want to avoid a copy, use .requires_grad_()
# or (). If you have a NumPy ndarray and want to avoid a copy, use torch.as_tensor().

以及一些其他创建张量的方法,源码来自知乎专栏by李小伟

/p/86984581

import torch


# =======================================================================
empty_like = torch.empty_like(empty)  # Returns an uninitialized tensor with the same size as input
print(empty_like)  # 创建一个 size 和 (此处是empty) 一样的张量
# tensor([[1.0293e-37, 0.0000e+00, 1.4013e-45],
#         [0.0000e+00, 8.9683e-44, 0.0000e+00]])

# =======================================================================
# (n, m=None, out=None)
# 参数:
# n (int ) – 行数
# m (int, optional) – 列数.如果为None,则默认为n
# out (Tensor, optinal) - Output tensor

eye = (3)  # 返回一个2维张量,对角线位置全1,其它位置全0;即n维单位矩阵
print(eye)
# tensor([[1., 0., 0.],
#         [0., 1., 0.],
#         [0., 0., 1.]])

# =======================================================================
zeros_3_2 = (3, 2)  # 返回一个全为标量 0 的张量,形状由可变参数sizes 定义
print(zeros_3_2)
# tensor([[0., 0.],
#         [0., 0.],
#         [0., 0.]])
zeros_5 = (5)
print(zeros_5)
# tensor([0., 0., 0., 0., 0.])
# torch.zeros_like() # 和 torch.empty_like() 功能类似

# =======================================================================
ones = (2, 3)  # 返回一个全为1 的张量,形状由可变参数sizes定义。
print(ones)
# tensor([[1., 1., 1.],
#         [1., 1., 1.]])
# torch.ones_like()

# =======================================================================
rand = (2, 3)  # 返回一个张量,包含了从区间[0,1)的均匀分布中抽取的一组随机数,形状由可变参数sizes 定义。
print(rand)  # 随机初始化的值在 [0,1) 之间
# tensor([[0.5047, 0.8442, 0.1771],
#         [0.3896, 0.5468, 0.9686]])
# torch.rand_like()

# =======================================================================
randn = (2, 3)  # 返回一个张量,包含了从标准正态分布(均值为0,方差为 1,即高斯白噪声)中抽取一组随机数,形状由可变参数sizes定义
print(randn)
# tensor([[ 0.0539,  0.8289, -1.6746],
#         [-1.7428,  0.6285,  0.1432]])
# torch.randn_like()

# =======================================================================
# Returns a tensor filled with random integers generated uniformly between low (inclusive) and high (exclusive).
# The shape of the tensor is defined by the variable argument size.
# 其中的参数 high和size是必要的,low是可选的(默认为0)
randint_01 = (3, 5, (3,))
print(randint_01)
# tensor([3, 4, 3])

randint_02 = (10, (2, 2))
print(randint_02)  # low 默认是 0
# tensor([[6, 0],
#         [4, 5]])

randint_03 = (3, 10, (2, 2))
print(randint_03)
# tensor([[7, 3],
#         [6, 9]])
# torch.randint_like()

# =======================================================================