pytorch的Tensor基礎(chǔ)

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.float32ortorch.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ū)間[base^{start}, base^{end}]的一維張量

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., [2,3]\to[2,2,3]

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

image.png

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

image.png

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ù)部分疆拘,帶符號

image.png

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

image.png

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 Computes \text{input} \geq \text{other} element-wise.
torch.gt(input, other, out=None) → Tensor Computes \text{input} > \text{other} element-wise.
torch.le(input, other, out=None) → Tensor Computes \text{input} \leq \text{other} element-wise.
torch.lt(input, other, out=None) → Tensor Computes \text{input} < \text{other} element-wise.
torch.ne(input, other, out=None) → Tensor Computes \text{input} \neq \text{other} 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') 計(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中輸入元素的累積積蹬屹。

image.png

torch.cumsum(input, dim, out=None, dtype=None) → Tensor 返回維度dim中輸入元素的累積和。

image.png

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末塘慕,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蒂胞,更是在濱河造成了極大的恐慌图呢,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蛤织,居然都是意外死亡赴叹,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進(jìn)店門指蚜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來乞巧,“玉大人,你說我怎么就攤上這事摊鸡√罚” “怎么了?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵柱宦,是天一觀的道長些椒。 經(jīng)常有香客問我,道長掸刊,這世上最難降的妖魔是什么免糕? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮忧侧,結(jié)果婚禮上石窑,老公的妹妹穿的比我還像新娘。我一直安慰自己蚓炬,他們只是感情好松逊,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著肯夏,像睡著了一般经宏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上驯击,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天烁兰,我揣著相機(jī)與錄音,去河邊找鬼徊都。 笑死沪斟,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的暇矫。 我是一名探鬼主播主之,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼李根!你這毒婦竟也來了槽奕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤朱巨,失蹤者是張志新(化名)和其女友劉穎史翘,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡琼讽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年必峰,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片钻蹬。...
    茶點(diǎn)故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡吼蚁,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出问欠,到底是詐尸還是另有隱情肝匆,我是刑警寧澤,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布顺献,位于F島的核電站旗国,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏注整。R本人自食惡果不足惜能曾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望肿轨。 院中可真熱鬧寿冕,春花似錦、人聲如沸椒袍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽驹暑。三九已至玫恳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間岗钩,已是汗流浹背纽窟。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工肖油, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留兼吓,地道東北人。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓森枪,卻偏偏與公主長得像视搏,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子县袱,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,066評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Tensor的創(chuàng)建除了構(gòu)造器浑娜,還提供了工廠模式的創(chuàng)建方式:函數(shù),同時提供了基本運(yùn)算函數(shù)的封裝式散。本主題就專門數(shù)理這些...
    楊強(qiáng)AT南京閱讀 2,099評論 0 1
  • 概述 在新版本中筋遭,PyTorch引入了許多令人興奮的新特性,主要的更新在于 Variable和Tensor的合并 ...
    古de莫寧閱讀 6,146評論 0 1
  • 1.安裝 https://pytorch.orgpip3 config set global.index-url ...
    dingtom閱讀 350評論 0 0
  • 這是我家的貓。 多么和諧的畫面漓滔,貓?jiān)趯P闹轮镜暮人嘟龋粭l小魚游了過來,朝它吐泡泡响驴,貓咪任不為所動透且,瞇著眼睛,享受...
    淡墨清影閱讀 207評論 0 3
  • 它一直立著豁鲤,還沒回頭秽誊,世間的滄桑與甘甜都無法打動它,皺一皺眉頭琳骡,動一動手锅论,它用塵封的古鏡照入石上的旅者,用湖水的玉...
    青云劍客閱讀 327評論 1 3