torch
Tensors
torch package包含data structure和mathematics operation,和tensor序列化的实用小程序。
torch.set_default_dtype(d) 设置
torch.tensor()
的默认浮点类型
d (torch.dtype
) – the floating point dtype to make the default
Example:
>>> torch.tensor([1.2, 3]).dtype # initial default for floating point is torch.float32
torch.float32
>>> torch.set_default_dtype(torch.float64)
>>> torch.tensor([1.2, 3]).dtype # a new floating point tensor
torch.float64
torch.get_default_dtype() → torch.dtype 获取当前默认的浮点数类型
>>> torch.get_default_dtype() # initial default for floating point is torch.float32
torch.float32
>>> torch.set_default_dtype(torch.float64)
>>> torch.get_default_dtype() # default is now changed to torch.float64
torch.float64
>>> torch.set_default_tensor_type(torch.FloatTensor) # 另一种设置方法 setting tensor type also affects this
>>> torch.get_default_dtype() # changed to torch.float32, the dtype for torch.FloatTensor
torch.float32
torch.numel(input) → int 返回输入张量中元素的总数。
input (Tensor) – the input tensor.
>>> a = torch.randn(1, 2, 3, 4, 5)
>>> torch.numel(a)
120
>>> a = torch.zeros(4,4)
>>> torch.numel(a)
16
torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None, sci_mode=None)
设置打印选项。主要用于debug。
Creation Ops
torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False) → Tensor
data (array_like) 初始data,可以是list,tuple,Numpy ndarray,scalar
dtype (torch.dtype
, optional) e.g., torch.float32
ortorch.float
, torch.float64
or torch.double
, torch.float16
or torch.half
, torch.int32
or torch.int
, torch.int64
or torch.long
, torch.bool
device (torch.device
, optional) e.g., torch.device('cuda:0')
, torch.device('cuda', 0)
, 'cuda:0'
, 'cpu'
, torch.device(1)
cuda Tensor, 1
requires_grad (bool, optional)
pin_memory (bool, optional)
torch.tensor()
总是会复制data。如果要避免复制data,data是一个tensor, torch.Tensor.requires_grad_()
or torch.Tensor.detach()
. 如果有一个 ndarray
,想要避免复制data,torch.as_tensor()
torch.tensor()
会构造一个leaf variable(叶变量),如果x
是一个tensor,torch.tensor(x)
等价于 x.clone().detach()
, torch.tensor(x, requires_grad=True)
等价于x.clone().detach().requires_grad_(True)
,推荐使用torch.Tensor.clone().detach()
torch.as_tensor(data, dtype=None, device=None) → Tensor
把数据转换成一个torch.Tensor
。如果数据已经是一个具有相同dtype和设备的张量,则不进行复制,否则,如果数据张量requires_grad=True,则返回一个新的张量,并保留计算图。类似地,如果数据是相应dtype的ndarray,而设备是cpu,则不执行复制。不会复制,是指修改torch.as_tensor()返回的tensor,原数据也会被修改
Example:
>>> a = numpy.array([1, 2, 3])
>>> t = torch.as_tensor(a)
>>> t
tensor([ 1, 2, 3])
>>> t[0] = -1
>>> a
array([-1, 2, 3])
>>> a = numpy.array([1, 2, 3])
>>> t = torch.as_tensor(a, device=torch.device('cuda'))
>>> t
tensor([ 1, 2, 3])
>>> t[0] = -1
>>> a
array([1, 2, 3])
torch.from_numpy(ndarray) → Tensor
从ndarray
创建一个Tensor
, 与ndarray
共享内存,返回的Tensor
不能改变大小。
>>> a = numpy.array([1, 2, 3])
>>> t = torch.from_numpy(a)
>>> t
tensor([ 1, 2, 3])
>>> t[0] = -1
>>> a
array([-1, 2, 3])
torch.from_numpy(ndarray)
与torch.as_tensor(data, dtype=None, device=None)
功能相似
torch.zeros(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
size (python:int...) – a sequence of integers
Example:
>>> torch.zeros(2, 3)
tensor([[ 0., 0., 0.],
[ 0., 0., 0.]])
>>> torch.zeros(5)
tensor([ 0., 0., 0., 0., 0.])
torch.zeros_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor
torch.zeros_like(input)
等价于 torch.zeros(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)
.
torch.ones(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
Example:
>>> torch.ones(2, 3)
tensor([[ 1., 1., 1.],
[ 1., 1., 1.]])
>>> torch.ones(5)
tensor([ 1., 1., 1., 1., 1.])
torch.ones_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor
torch.ones_like(input, out=output)
等价于torch.ones(input.size(), out=output)
torch.arange(start=0, end, step=1, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
返回区间[start, end)
的一维张量
Example:
>>> torch.arange(5)
tensor([ 0, 1, 2, 3, 4])
>>> torch.arange(1, 4)
tensor([ 1, 2, 3])
>>> torch.arange(1, 2.5, 0.5)
tensor([ 1.0000, 1.5000, 2.0000])
torch.linspace(start, end, steps=100, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
返回在起点和终点之间等距间隔的步长的一维张量。
Example:
>>> torch.linspace(3, 10, steps=5)
tensor([ 3.0000, 4.7500, 6.5000, 8.2500, 10.0000])
>>> torch.linspace(-10, 10, steps=5)
tensor([-10., -5., 0., 5., 10.])
>>> torch.linspace(start=-10, end=10, steps=5)
tensor([-10., -5., 0., 5., 10.])
>>> torch.linspace(start=-10, end=10, steps=1)
tensor([-10.])
torch.logspace(start, end, steps=100, base=10.0, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
返回区间的一维张量
Example:
>>> torch.logspace(start=-10, end=10, steps=5)
tensor([ 1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10])
>>> torch.logspace(start=0.1, end=1.0, steps=5)
tensor([ 1.2589, 2.1135, 3.5481, 5.9566, 10.0000])
>>> torch.logspace(start=0.1, end=1.0, steps=1)
tensor([1.2589])
>>> torch.logspace(start=2, end=2, steps=1, base=2)
tensor([4.0])
torch.eye(n, m=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
返回一个二维张量,对角线上有一个,其他位置为零。
Example:
>>> torch.eye(3)
tensor([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
torch.empty(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) → Tensor
返回填充有未初始化数据的张量。
torch.empty_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor
torch.empty_like(input)
等价于 torch.empty(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)
.
torch.full(size, fill_value, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
torch.full_like(input, fill_value, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
torch.full_like(input, fill_value)
等价于torch.full(input.size(), fill_value, dtype=input.dtype, layout=input.layout, device=input.device)
Indexing, Slicing, Joining, Mutating Ops
torch.cat(tensors, dim=0, out=None) → Tensor
tensors (sequence of Tensors) 相同类型的张量的任何python序列。提供的非空张量必须具有相同的形状,但cat尺寸除外。
dim (python:int, optional)
torch.cat()
可以被看为torch.split()
和torch.chunk()
的逆运算
torch.stack(tensors, dim=0, out=None) → Tensor
将张量的序列沿新维度连接起来。所有tensor大小一样,输出tensor会增加一个新的维度,e.g.,
torch.chunk(input, chunks, dim=0) → List of Tensors
将张量拆分为特定数量的块。如果沿着给定维度dim的张量大小无法被块整除,则最后一个块将更小。
torch.split(tensor, split_size_or_sections, dim=0)
split_size_or_sections (python:int) or (list(python:int)) – size of a single chunk or list of sizes for each chunk
可以指定块大小,或每一块的大小
torch.unbind(input, dim=0) → seq
将tensor解开,删除张量一个维度。 返回给定维度上所有切片的元组。
Example:
>>> torch.unbind(torch.tensor([[1, 2, 3],
>>> [4, 5, 6],
>>> [7, 8, 9]]))
(tensor([1, 2, 3]), tensor([4, 5, 6]), tensor([7, 8, 9]))
torch.gather(input, dim, index, out=None, sparse_grad=False) → Tensor
沿dim指定的轴收集值。
For a 3-D tensor the output is specified by:
out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2
Example:
>>> t = torch.tensor([[1,2],[3,4]])
>>> torch.gather(t, 1, torch.tensor([[0,0],[1,0]]))
tensor([[ 1, 1],
[ 4, 3]])
torch.take(input, index) → Tensor
Example:
>>> src = torch.tensor([[4, 3, 5],
[6, 7, 8]])
>>> torch.take(src, torch.tensor([0, 2, 5]))
tensor([ 4, 5, 8])
torch.index_select(input, dim, index, out=None) → Tensor
Example:
>>> x = torch.randn(3, 4)
>>> x
tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
[-0.4664, 0.2647, -0.1228, -1.1068],
[-1.1734, -0.6571, 0.7230, -0.6004]])
>>> indices = torch.tensor([0, 2])
>>> torch.index_select(x, 0, indices)
tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
[-1.1734, -0.6571, 0.7230, -0.6004]])
>>> torch.index_select(x, 1, indices)
tensor([[ 0.1427, -0.5414],
[-0.4664, -0.1228],
[-1.1734, 0.7230]])
torch.masked_select(input, mask, out=None) → Tensor
Example:
>>> x = torch.randn(3, 4)
>>> x
tensor([[ 0.3552, -2.3825, -0.8297, 0.3477],
[-1.2035, 1.2252, 0.5002, 0.6248],
[ 0.1307, -2.0608, 0.1244, 2.0139]])
>>> mask = x.ge(0.5)
>>> mask
tensor([[False, False, False, False],
[False, True, True, True],
[False, False, False, True]])
>>> torch.masked_select(x, mask)
tensor([ 1.2252, 0.5002, 0.6248, 2.0139])
torch.narrow(input, dim, start, length) → Tensor
Example:
>>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> torch.narrow(x, 0, 0, 2)
tensor([[ 1, 2, 3],
[ 4, 5, 6]])
>>> torch.narrow(x, 1, 1, 2)
tensor([[ 2, 3],
[ 5, 6],
[ 8, 9]])
torch.nonzero(input, *, out=None, as_tuple=False) → LongTensor or tuple of LongTensors
返回一个张量,其中包含输入的所有非零元素的索引。
Example:
>>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]))
tensor([[ 0],
[ 1],
[ 2],
[ 4]])
>>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
[0.0, 0.4, 0.0, 0.0],
[0.0, 0.0, 1.2, 0.0],
[0.0, 0.0, 0.0,-0.4]]))
tensor([[ 0, 0],
[ 1, 1],
[ 2, 2],
[ 3, 3]])
>>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]), as_tuple=True)
(tensor([0, 1, 2, 4]),)
>>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
[0.0, 0.4, 0.0, 0.0],
[0.0, 0.0, 1.2, 0.0],
[0.0, 0.0, 0.0,-0.4]]), as_tuple=True)
(tensor([0, 1, 2, 3]), tensor([0, 1, 2, 3]))
>>> torch.nonzero(torch.tensor(5), as_tuple=True)
(tensor([0]),)
torch.where(condition, x, y) → Tensor
Example:
>>> x = torch.randn(3, 2)
>>> y = torch.ones(3, 2)
>>> x
tensor([[-0.4620, 0.3139],
[ 0.3898, -0.7197],
[ 0.0478, -0.1657]])
>>> torch.where(x > 0, x, y)
tensor([[ 1.0000, 0.3139],
[ 0.3898, 1.0000],
[ 0.0478, 1.0000]])
torch.where(condition) → tuple of LongTensor
torch.where(condition)
is identical to torch.nonzero(condition, as_tuple=True)
torch.reshape(input, shape) → Tensor
与torch.Tensor.view()
类似
Example:
>>> a = torch.arange(4.)
>>> torch.reshape(a, (2, 2))
tensor([[ 0., 1.],
[ 2., 3.]])
>>> b = torch.tensor([[0, 1], [2, 3]])
>>> torch.reshape(b, (-1,))
tensor([ 0, 1, 2, 3])
torch.squeeze(input, dim=None, out=None) → Tensor
返回一个张量,其中所有大小为1的输入的维都已删除。
Example:
>>> x = torch.zeros(2, 1, 2, 1, 2)
>>> x.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x)
>>> y.size()
torch.Size([2, 2, 2])
>>> y = torch.squeeze(x, 0)
>>> y.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x, 1)
>>> y.size()
torch.Size([2, 2, 1, 2])
torch.unsqueeze(input, dim, out=None) → Tensor
Returns a new tensor with a dimension of size one inserted at the specified position.
Example:
>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
tensor([[ 1, 2, 3, 4]])
>>> torch.unsqueeze(x, 1)
tensor([[ 1],
[ 2],
[ 3],
[ 4]])
torch.t(input) → Tensor
对2维tensor进行转置
torch.transpose(input, dim0, dim1) → Tensor
返回张量,该张量是输入的转置版本。给定的尺寸dim0和dim1被交换。
Example:
>>> x = torch.randn(2, 3)
>>> x
tensor([[ 1.0028, -0.9893, 0.5809],
[-0.1669, 0.7299, 0.4942]])
>>> torch.transpose(x, 0, 1)
tensor([[ 1.0028, -0.1669],
[-0.9893, 0.7299],
[ 0.5809, 0.4942]])
Random sampling
torch.seed()
将用于生成随机数的种子设置为不确定的随机数。返回用于播种RNG的64位数字。即程序自动设置随机种子
torch.manual_seed(seed)
设置CPU用于生成随机数的种子。即手动设置随机种子
torch.cuda.manual_seed(seed)
为当前GPU生成随机数设置种子。如果没有CUDA,则可以安全地调用此函数;在这种情况下,它会被静默忽略。如果使用的是多GPU模型,则此功能不足以获得确定性。要播种所有GPU,请使用torch.cuda.manual_seed_all(seed)
torch.cuda.manual_seed_all(seed)
设置用于在所有GPU上生成随机数的种子。如果没有CUDA,则可以安全地调用此函数;在这种情况下,它会被静默忽略。
torch.initial_seed()
返回用于生成随机数的初始种子。即torch.seed()
或torch.manual_seed(seed)
设置的随机种子
Example:
>>> torch.seed()
1989945232164789487
>>> torch.initial_seed()
1989945232164789487
>>> torch.manual_seed(12345)
>>> torch.initial_seed()
12345
torch.bernoulli(input, *, generator=None, out=None) → Tensor
伯努利分布,二项分布
torch.multinomial(input, num_samples, replacement=False, *, generator=None, out=None) → LongTensor
多项式分布
torch.normal()
正态分布
torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
标准正态分布,mean=0 and variance=1 (also called the standard normal distribution).
torch.randn_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor
torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
间隔[0,1)上的均匀分布
torch.rand_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor
torch.rand_like(input)
等价于 torch.rand(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)
torch.randint(low=0, high, size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
low(包含)和high(不含)之间均匀生成的随机整数。
torch.randint_like(input, low=0, high, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
torch.randperm(n, out=None, dtype=torch.int64, layout=torch.strided, device=None, requires_grad=False) → LongTensor
返回0-n-1的随机整数排列
Example:
>>> torch.randperm(4)
tensor([2, 1, 0, 3])
In-place random sampling
In-place random sampling是指会改变
torch.Tensor
的值
Example:
>>> a=torch.zeros(3,3)
>>> a
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
>>> a.normal_()
tensor([[ 1.4271, -1.8701, -1.1962],
[-2.0440, -0.4560, -1.4295],
[-0.7175, 1.3922, 0.0811]])
>>> a
tensor([[ 1.4271, -1.8701, -1.1962],
[-2.0440, -0.4560, -1.4295],
[-0.7175, 1.3922, 0.0811]])
Serialization
torch.save(obj, f, pickle_module=
, pickle_protocol=2, _use_new_zipfile_serialization=False)
Example
>>> # Save to file
>>> x = torch.tensor([0, 1, 2, 3, 4])
>>> torch.save(x, 'tensor.pt')
torch.load(f, map_location=None, pickle_module=
, **pickle_load_args)
Example
>>> torch.load('tensors.pt')
# Load all tensors onto the CPU
>>> torch.load('tensors.pt', map_location=torch.device('cpu'))
# Load all tensors onto the CPU, using a function
>>> torch.load('tensors.pt', map_location='cpu')
# If the 'tensors.pt' contains GPU tensors, those tensors will be loaded to GPU by default. Therefore, use map_location='cpu' to load tensors to CPU from GPU.
Best practices
# save
torch.save(the_model.state_dict(), PATH)
# load
the_model = TheModelClass(*args, **kwargs)
the_model.load_state_dict(torch.load(PATH))
Locally disabling gradient computation
上下文管理器torch.no_grad()
,torch.enable_grad()
和torch.set_grad_enabled()
有助于局部禁用和启用梯度计算。
Example:
>>> x = torch.zeros(1, requires_grad=True)
>>> with torch.no_grad():
... y=x+2
...
>>> y.requires_grad
False
>>> with torch.enable_grad():
... y=x+2
...
>>> y.requires_grad
True
>>> with torch.set_grad_enabled(True):
... y=x+2
...
>>> y.requires_grad
True
>>> torch.set_grad_enabled(False)
>>> y=x+2
>>> y.requires_grad
False
Math operations
Pointwise Ops
torch.abs(input, out=None) → Tensor 绝对值
torch.add() 加
torch.sub() 减
torch.div() 除
torch.mul() 乘
或通过 + - * / 符号计算
c = a+b
c = a-b
c = a*b
c = a/b
torch.fmod(input, other, out=None) → Tensor 余数
torch.remainder(input, other, out=None) → Tensor 余数
torch.trunc(input, out=None) → Tensor 取截断整数部分,带符号
torch.frac(input, out=None) → Tensor 取小数部分,带符号
torch.ceil(input, out=None) → Tensor
torch.floor(input, out=None) → Tensor
torch.round(input, out=None) → Tensor 四舍五入
torch.clamp(input, min, max, out=None) → Tensor
Example:
>>> a = torch.randn(4)
>>> a
tensor([-1.7120, 0.1734, -0.0478, -0.0922])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000, 0.1734, -0.0478, -0.0922])
torch.sin(input, out=None) → Tensor
torch.cos(input, out=None) → Tensor
torch.tan(input, out=None) → Tensor
torch.neg(input, out=None) → Tensor 取负数
torch.reciprocal(input, out=None) → Tensor 取倒数
torch.pow() 幂
torch.sqrt(input, out=None) → Tensor 开根号
torch.exp(input, out=None) → Tensor
torch.log(input, out=None) → Tensor
torch.log10(input, out=None) → Tensor
torch.log1p(input, out=None) → Tensor
torch.lerp(input, end, weight, out=None)
线性插值
torch.sigmoid(input, out=None) → Tensor
torch.sign(input, out=None) → Tensor
Reduction Ops
torch.argmax()
torch.argmin()
torch.dist(input, other, p=2) → Tensor 求两个tensor差的范数,欧式距离
torch.norm(input, p='fro', dim=None, keepdim=False, out=None, dtype=None) 求范数
torch.median() 中位数
torch.mode(input, dim=-1, keepdim=False, values=None, indices=None) -> (Tensor, LongTensor) 众数
torch.sum() 和
torch.prod() 返回所有元素的乘积
torch.mean() 平均数
torch.std() 标准差
torch.std_mean() 标准差和均值
torch.var() 方差
torch.var_mean() 方差和均值
torch.unique(input, sorted=True, return_inverse=False, return_counts=False, dim=None) 消除非连续重复值,返回输入张量的唯一元素,索引,计数
torch.unique_consecutive(input, return_inverse=False, return_counts=False, dim=None) 只消除连续的重复值,从每个连续的等效元素组中除去除第一个元素外的所有元素。
Example:
>>> x = torch.tensor([1, 1, 2, 2, 3, 1, 1, 2])
>>> output = torch.unique(x)
>>> output
tensor([1, 2, 3])
>>> x = torch.tensor([1, 1, 2, 2, 3, 1, 1, 2])
>>> output = torch.unique_consecutive(x)
>>> output
tensor([1, 2, 3, 1, 2])
Comparison Ops
torch.allclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False) → bool
torch.sort(input, dim=-1, descending=False, out=None) -> (Tensor, LongTensor)
torch.argsort(input, dim=-1, descending=False, out=None) → LongTensor
返回按值升序对给定维度上的张量排序的索引。ascending order
torch.max()
torch.min()
torch.kthvalue(input, k, dim=None, keepdim=False, out=None) -> (Tensor, LongTensor) 返回第k个最小值和索引
torch.topk(input, k, dim=None, largest=True, sorted=True, out=None) -> (Tensor, LongTensor) 返回k个最大的元素
torch.isfinite() 返回每个元素是否为有限。
torch.isinf(tensor) 每个元素是否为+/- INF。
torch.isnan() 是否每个元素都是NaN
Example:
>>> torch.isfinite(torch.tensor([1, float('inf'), 2, float('-inf'), float('nan')]))
tensor([True, False, True, False, False])
torch.eq(input, other, out=None) → Tensor 计算每个元素是否相等
torch.equal(input, other) → bool size和value是否相等
torch.ge(input, other, out=None) → Tensor Computes element-wise.
torch.gt(input, other, out=None) → Tensor Computes element-wise.
torch.le(input, other, out=None) → Tensor Computes element-wise.
torch.lt(input, other, out=None) → Tensor Computes element-wise.
torch.ne(input, other, out=None) → Tensor Computes element-wise.
Other Operations
torch.bincount(input, weights=None, minlength=0) → Tensor
Example:
>>> input = torch.randint(0, 8, (5,), dtype=torch.int64)
>>> weights = torch.linspace(0, 1, steps=5)
>>> input, weights
(tensor([4, 3, 6, 3, 4]),
tensor([ 0.0000, 0.2500, 0.5000, 0.7500, 1.0000])
>>> torch.bincount(input)
tensor([0, 0, 0, 2, 2, 0, 1])
>>> input.bincount(weights)
tensor([0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 0.0000, 0.5000])
torch.broadcast_tensors(*tensors) → List of Tensors
Example:
>>> x = torch.arange(3).view(1, 3)
>>> y = torch.arange(2).view(2, 1)
>>> a, b = torch.broadcast_tensors(x, y)
>>> a.size()
torch.Size([2, 3])
>>> a
tensor([[0, 1, 2],
[0, 1, 2]])
torch.cartesian_prod(*tensors) 笛卡儿积
Example:
>>> a = [1, 2, 3]
>>> b = [4, 5]
>>> list(itertools.product(a, b))
[(1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)]
>>> tensor_a = torch.tensor(a)
>>> tensor_b = torch.tensor(b)
>>> torch.cartesian_prod(tensor_a, tensor_b)
tensor([[1, 4],
[1, 5],
[2, 4],
[2, 5],
[3, 4],
[3, 5]])
torch.cdist(x1, x2, p=2, compute_mode='use_mm_for_euclid_dist_if_necessary') 计算批处理行向量的两个集合的每对之间的p范数距离。欧式距离
Example
>>> a = torch.tensor([[0.9041, 0.0196], [-0.3108, -2.4423], [-0.4821, 1.059]])
>>> a
tensor([[ 0.9041, 0.0196],
[-0.3108, -2.4423],
[-0.4821, 1.0590]])
>>> b = torch.tensor([[-2.1763, -0.4713], [-0.6986, 1.3702]])
>>> b
tensor([[-2.1763, -0.4713],
[-0.6986, 1.3702]])
>>> torch.cdist(a, b, p=2)
tensor([[3.1193, 2.0959],
[2.7138, 3.8322],
[2.2830, 0.3791]])
torch.combinations(input, r=2, with_replacement=False) → seq
长度为2的所有可能组合
Example:
>>> a = [1, 2, 3]
>>> list(itertools.combinations(a, r=2))
[(1, 2), (1, 3), (2, 3)]
>>> list(itertools.combinations(a, r=3))
[(1, 2, 3)]
>>> list(itertools.combinations_with_replacement(a, r=2)) # 有放回
[(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]
>>> tensor_a = torch.tensor(a)
>>> torch.combinations(tensor_a)
tensor([[1, 2],
[1, 3],
[2, 3]])
>>> torch.combinations(tensor_a, r=3)
tensor([[1, 2, 3]])
>>> torch.combinations(tensor_a, with_replacement=True)
tensor([[1, 1],
[1, 2],
[1, 3],
[2, 2],
[2, 3],
[3, 3]])
torch.cross(input, other, dim=-1, out=None) → Tensor 返回输入和其他维度的矢量的叉积
torch.cumprod(input, dim, out=None, dtype=None) → Tensor 返回维度dim中输入元素的累积积。
torch.cumsum(input, dim, out=None, dtype=None) → Tensor 返回维度dim中输入元素的累积和。
torch.diag(input, diagonal=0, out=None) → Tensor
torch.diag_embed(input, offset=0, dim1=-2, dim2=-1) → Tensor
torch.diagflat(input, offset=0) → Tensor
torch.diagonal(input, offset=0, dim1=0, dim2=1) → Tensor
Examples:
Get the square matrix where the input vector is the diagonal:
>>> a = torch.randn(3)
>>> a
tensor([ 0.5950,-0.0872, 2.3298])
>>> torch.diag(a)
tensor([[ 0.5950, 0.0000, 0.0000],
[ 0.0000,-0.0872, 0.0000],
[ 0.0000, 0.0000, 2.3298]])
>>> torch.diag(a, 1)
tensor([[ 0.0000, 0.5950, 0.0000, 0.0000],
[ 0.0000, 0.0000,-0.0872, 0.0000],
[ 0.0000, 0.0000, 0.0000, 2.3298],
[ 0.0000, 0.0000, 0.0000, 0.0000]])
torch.flatten(input, start_dim=0, end_dim=-1) → Tensor 展平一个张量中一个连续的dims范围。
Example:
>>> t = torch.tensor([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
>>> torch.flatten(t)
tensor([1, 2, 3, 4, 5, 6, 7, 8])
>>> torch.flatten(t, start_dim=1)
tensor([[1, 2, 3, 4],
[5, 6, 7, 8]])
torch.flip(input, dims) → Tensor
Example:
>>> x = torch.arange(8).view(2, 2, 2)
>>> x
tensor([[[ 0, 1],
[ 2, 3]],
[[ 4, 5],
[ 6, 7]]])
>>> torch.flip(x, [0, 1])
tensor([[[ 6, 7],
[ 4, 5]],
[[ 2, 3],
[ 0, 1]]])
torch.rot90(input, k, dims) → Tensor
torch.histc(input, bins=100, min=0, max=0, out=None) → Tensor
计算张量的直方图。
torch.meshgrid(*tensors, **kwargs)
Example:
>>> x = torch.tensor([1, 2, 3])
>>> y = torch.tensor([4, 5, 6])
>>> grid_x, grid_y = torch.meshgrid(x, y)
>>> grid_x
tensor([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
>>> grid_y
tensor([[4, 5, 6],
[4, 5, 6],
[4, 5, 6]])
torch.renorm(input, p, dim, maxnorm, out=None) → Tensor
返回一个张量,其中沿着维度dim的输入的每个子张量均被规范化,从而子张量的p范数小于maxnorm值
Example:
>>> x = torch.ones(3, 3)
>>> x[1].fill_(2)
tensor([ 2., 2., 2.])
>>> x[2].fill_(3)
tensor([ 3., 3., 3.])
>>> x
tensor([[ 1., 1., 1.],
[ 2., 2., 2.],
[ 3., 3., 3.]])
>>> torch.renorm(x, 1, 0, 5)
tensor([[ 1.0000, 1.0000, 1.0000],
[ 1.6667, 1.6667, 1.6667],
[ 1.6667, 1.6667, 1.6667]])
torch.repeat_interleave()
重复张量的元素。
Example:
>>> x = torch.tensor([1, 2, 3])
>>> x.repeat_interleave(2)
tensor([1, 1, 2, 2, 3, 3])
>>> y = torch.tensor([[1, 2], [3, 4]])
>>> torch.repeat_interleave(y, 2)
tensor([1, 1, 2, 2, 3, 3, 4, 4])
>>> torch.repeat_interleave(y, 3, dim=1)
tensor([[1, 1, 1, 2, 2, 2],
[3, 3, 3, 4, 4, 4]])
>>> torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0)
tensor([[1, 2],
[3, 4],
[3, 4]])
torch.roll(input, shifts, dims=None) → Tensor
将张量沿给定维数滚动。移动到最后位置之外的元素在第一个位置重新引入。如果不指定维数,张量在滚动之前会被压扁,然后恢复到原来的形状。
Example:
>>> x = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8]).view(4, 2)
>>> x
tensor([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
>>> torch.roll(x, 1, 0)
tensor([[7, 8],
[1, 2],
[3, 4],
[5, 6]])
>>> torch.roll(x, -1, 0)
tensor([[3, 4],
[5, 6],
[7, 8],
[1, 2]])
>>> torch.roll(x, shifts=(2, 1), dims=(0, 1))
tensor([[6, 5],
[8, 7],
[2, 1],
[4, 3]])
torch.trace(input) → Tensor
返回输入二维矩阵对角线元素的总和。
torch.tril(input, diagonal=0, out=None) → Tensor 返回张量的下三角部分
Example:
>>> a = torch.randn(3, 3)
>>> a
tensor([[-1.0813, -0.8619, 0.7105],
[ 0.0935, 0.1380, 2.2112],
[-0.3409, -0.9828, 0.0289]])
>>> torch.tril(a)
tensor([[-1.0813, 0.0000, 0.0000],
[ 0.0935, 0.1380, 0.0000],
[-0.3409, -0.9828, 0.0289]])
torch.triu(input, diagonal=0, out=None) → Tensor 返回张量的上三角部分
torch.dot(input, tensor) → Tensor 计算两个张量的点积(内积)
torch.eig(input, eigenvectors=False, out=None) -> (Tensor, Tensor) 计算实方矩阵的特征值和特征向量。
torch.inverse(input, out=None) → Tensor 取方阵输入的逆。
torch.det(input) → Tensor 计算平方矩阵或批次平方矩阵的行列式。
torch.matmul(input, other, out=None) → Tensor 两个张量的矩阵乘积。
torch.mm(input, mat2, out=None) → Tensor 矩阵乘积 此函数不广播。有关广播矩阵产品,请参见torch.matmul()
torch.matrix_rank(input, tol=None, symmetric=False) → Tensor 返回二维张量的rank。
torch.qr(input, some=True, out=None) -> (Tensor, Tensor) QR分解
torch.orgqr(input, input2) → Tensor 计算QR分解的正交矩阵Q
torch.pinverse(input, rcond=1e-15) → Tensor 计算二维时态的伪逆(也称为Moore-Penrose逆)
torch.svd(input, some=True, compute_uv=True, out=None) -> (Tensor, Tensor, Tensor) 奇异值分解