torch
Tensors
torch package包含data structure和mathematics operation黔酥,和tensor序列化的實(shí)用小程序。
torch.set_default_dtype(d) 設(shè)置
torch.tensor()
的默認(rèn)浮點(diǎn)類型
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 獲取當(dāng)前默認(rèn)的浮點(diǎn)數(shù)類型
>>> 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) # 另一種設(shè)置方法 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 返回輸入張量中元素的總數(shù)具温。
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)
設(shè)置打印選項(xiàng)。主要用于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()
總是會復(fù)制data甥材。如果要避免復(fù)制data,data是一個tensor逊谋, torch.Tensor.requires_grad_()
or torch.Tensor.detach()
. 如果有一個 ndarray
擂达,想要避免復(fù)制data土铺,torch.as_tensor()
torch.tensor()
會構(gòu)造一個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
把數(shù)據(jù)轉(zhuǎn)換成一個torch.Tensor
究恤。如果數(shù)據(jù)已經(jīng)是一個具有相同dtype和設(shè)備的張量,則不進(jìn)行復(fù)制后德,否則部宿,如果數(shù)據(jù)張量requires_grad=True,則返回一個新的張量瓢湃,并保留計(jì)算圖理张。類似地,如果數(shù)據(jù)是相應(yīng)dtype的ndarray绵患,而設(shè)備是cpu雾叭,則不執(zhí)行復(fù)制。不會復(fù)制落蝙,是指修改torch.as_tensor()返回的tensor织狐,原數(shù)據(jù)也會被修改
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
創(chuàng)建一個Tensor
, 與ndarray
共享內(nèi)存,返回的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
返回區(qū)間[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
返回在起點(diǎn)和終點(diǎn)之間等距間隔的步長的一維張量移迫。
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
返回區(qū)間的一維張量
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
返回填充有未初始化數(shù)據(jù)的張量。
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()
的逆運(yùn)算
torch.stack(tensors, dim=0, out=None) → Tensor
將張量的序列沿新維度連接起來。所有tensor大小一樣亲善,輸出tensor會增加一個新的維度设易,e.g.,
torch.chunk(input, chunks, dim=0) → List of Tensors
將張量拆分為特定數(shù)量的塊。如果沿著給定維度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進(jìn)行轉(zhuǎn)置
torch.transpose(input, dim0, dim1) → Tensor
返回張量,該張量是輸入的轉(zhuǎn)置版本赃绊。給定的尺寸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()
將用于生成隨機(jī)數(shù)的種子設(shè)置為不確定的隨機(jī)數(shù)。返回用于播種RNG的64位數(shù)字碧查。即程序自動設(shè)置隨機(jī)種子
torch.manual_seed(seed)
設(shè)置CPU用于生成隨機(jī)數(shù)的種子运敢。即手動設(shè)置隨機(jī)種子
torch.cuda.manual_seed(seed)
為當(dāng)前GPU生成隨機(jī)數(shù)設(shè)置種子。如果沒有CUDA忠售,則可以安全地調(diào)用此函數(shù)传惠;在這種情況下,它會被靜默忽略稻扬。如果使用的是多GPU模型卦方,則此功能不足以獲得確定性。要播種所有GPU腐螟,請使用torch.cuda.manual_seed_all(seed)
torch.cuda.manual_seed_all(seed)
設(shè)置用于在所有GPU上生成隨機(jī)數(shù)的種子愿汰。如果沒有CUDA,則可以安全地調(diào)用此函數(shù)乐纸;在這種情況下衬廷,它會被靜默忽略。
torch.initial_seed()
返回用于生成隨機(jī)數(shù)的初始種子汽绢。即torch.seed()
或torch.manual_seed(seed)
設(shè)置的隨機(jī)種子
Example:
>>> torch.seed()
1989945232164789487
>>> torch.initial_seed()
1989945232164789487
>>> torch.manual_seed(12345)
<torch._C.Generator object at 0x7f869477cf30>
>>> torch.initial_seed()
12345
torch.bernoulli(input, *, generator=None, out=None) → Tensor
伯努利分布吗跋,二項(xiàng)分布
torch.multinomial(input, num_samples, replacement=False, *, generator=None, out=None) → LongTensor
多項(xiàng)式分布
torch.normal()
正態(tài)分布
torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
標(biāo)準(zhǔn)正態(tài)分布,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(不含)之間均勻生成的隨機(jī)整數(shù)。
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的隨機(jī)整數(shù)排列
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=<module 'pickle' from '/opt/conda/lib/python3.6/pickle.py'>, 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=<module 'pickle' from '/opt/conda/lib/python3.6/pickle.py'>, **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()
有助于局部禁用和啟用梯度計(jì)算酗宋。
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)
<torch.autograd.grad_mode.set_grad_enabled object at 0x7f86efe717c0>
>>> 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() 乘
或通過 + - * / 符號計(jì)算
c = a+b
c = a-b
c = a*b
c = a/b
torch.fmod(input, other, out=None) → Tensor 余數(shù)
torch.remainder(input, other, out=None) → Tensor 余數(shù)
torch.trunc(input, out=None) → Tensor 取截斷整數(shù)部分,帶符號
torch.frac(input, out=None) → Tensor 取小數(shù)部分疆拘,帶符號
torch.ceil(input, out=None) → Tensor
image.png
torch.floor(input, out=None) → Tensor
image.png
torch.round(input, out=None) → Tensor 四舍五入
torch.clamp(input, min, max, out=None) → Tensor
image.png
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 取負(fù)數(shù)
torch.reciprocal(input, out=None) → Tensor 取倒數(shù)
torch.pow() 冪
torch.sqrt(input, out=None) → Tensor 開根號
torch.exp(input, out=None) → Tensor
torch.log(input, out=None) → Tensor
image.png
torch.log10(input, out=None) → Tensor
image.png
torch.log1p(input, out=None) → Tensor
image.png
torch.lerp(input, end, weight, out=None)
線性插值
image.png
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差的范數(shù)蜕猫,歐式距離
torch.norm(input, p='fro', dim=None, keepdim=False, out=None, dtype=None) 求范數(shù)
torch.median() 中位數(shù)
torch.mode(input, dim=-1, keepdim=False, values=None, indices=None) -> (Tensor, LongTensor) 眾數(shù)
torch.sum() 和
torch.prod() 返回所有元素的乘積
torch.mean() 平均數(shù)
torch.std() 標(biāo)準(zhǔn)差
torch.std_mean() 標(biāo)準(zhǔn)差和均值
torch.var() 方差
torch.var_mean() 方差和均值
torch.unique(input, sorted=True, return_inverse=False, return_counts=False, dim=None) 消除非連續(xù)重復(fù)值荣回,返回輸入張量的唯一元素闺魏,索引向图,計(jì)數(shù)
torch.unique_consecutive(input, return_inverse=False, return_counts=False, dim=None) 只消除連續(xù)的重復(fù)值响鹃,從每個連續(xù)的等效元素組中除去除第一個元素外的所有元素。
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
返回按值升序?qū)o定維度上的張量排序的索引遏餐。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 計(jì)算每個元素是否相等
torch.equal(input, other) → bool size和value是否相等
torch.ge(input, other, out=None) → Tensor Computeselement-wise.
torch.gt(input, other, out=None) → Tensor Computeselement-wise.
torch.le(input, other, out=None) → Tensor Computeselement-wise.
torch.lt(input, other, out=None) → Tensor Computeselement-wise.
torch.ne(input, other, out=None) → Tensor Computeselement-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') 計(jì)算批處理行向量的兩個集合的每對之間的p范數(shù)距離旨涝。歐式距離
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 展平一個張量中一個連續(xù)的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
計(jì)算張量的直方圖慨默。
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的輸入的每個子張量均被規(guī)范化衬鱼,從而子張量的p范數(shù)小于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()
重復(fù)張量的元素业筏。
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
將張量沿給定維數(shù)滾動憔杨。移動到最后位置之外的元素在第一個位置重新引入鸟赫。如果不指定維數(shù),張量在滾動之前會被壓扁消别,然后恢復(fù)到原來的形狀抛蚤。
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 計(jì)算兩個張量的點(diǎn)積(內(nèi)積)
torch.eig(input, eigenvectors=False, out=None) -> (Tensor, Tensor) 計(jì)算實(shí)方矩陣的特征值和特征向量寻狂。
torch.inverse(input, out=None) → Tensor 取方陣輸入的逆岁经。
torch.det(input) → Tensor 計(jì)算平方矩陣或批次平方矩陣的行列式。
torch.matmul(input, other, out=None) → Tensor 兩個張量的矩陣乘積蛇券。
torch.mm(input, mat2, out=None) → Tensor 矩陣乘積 此函數(shù)不廣播缀壤。有關(guān)廣播矩陣產(chǎn)品,請參見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 計(jì)算QR分解的正交矩陣Q
torch.pinverse(input, rcond=1e-15) → Tensor 計(jì)算二維時態(tài)的偽逆(也稱為Moore-Penrose逆)
torch.svd(input, some=True, compute_uv=True, out=None) -> (Tensor, Tensor, Tensor) 奇異值分解
image.png