Tensor 基本運算

2021-10-07 06:22:18 字數 4424 閱讀 9649

powsqrt/sqrt

exp/log

近似值梯度裁剪

expand、without copying data

key idea

add(+)

>>> import torch

>>> a = torch.rand(3,4)

>>> b = torch.rand(4)

>>> a+b,torch.add(a,b)

(tensor([[1.1636, 1.1454, 1.3575, 1.1242],

[1.6967, 1.2132, 0.9738, 1.2507],

[0.9603, 1.6887, 1.3814, 0.7453]]),

tensor([[1.1636, 1.1454, 1.3575, 1.1242],

[1.6967, 1.2132, 0.9738, 1.2507],

[0.9603, 1.6887, 1.3814, 0.7453]]))

可見使用「+」和使用add函式結果是相同的。也可以驗證一下:

>>> import torch

>>> a = torch.rand(3,4)

>>> b = torch.rand(4)

>>> torch.all(torch.eq(a+b,torch.add(a,b)))

tensor(true)

sub(-)

mul(*)

div(/)

只能用於二維矩陣的運算。

>>> import torch

>>> a = torch.full([2,2],3)

>>> b = torch.ones(2,2)

>>> torch.mm(a,b)

tensor([[6., 6.],

[6., 6.]])

可以用於二維矩陣計算,也可以是多維。

二維:

>>> import torch

>>> a = torch.full([2,2],3)

>>> b = torch.ones(2,2)

>>> torch.matmul(a,b)

tensor([[6., 6.],

[6., 6.]])

>>> import torch

>>> a = torch.full([2,2],3)

>>> b = torch.ones(2,2)

>>> a@b

tensor([[6., 6.],

[6., 6.]])

降維度

>>> import torch

>>> a = torch.rand(4,784)

>>> x = torch.rand(4,784)

>>> w = torch.rand(512,784)

>>> ([email protected]()).shape

torch.size([4, 512])

>二維

比如四維,計算的時候就是前兩維不變,後兩維進行計算。

>>> import torch

>>> a = torch.rand(4,3,28,64)

>>> b = torch.rand(4,3,64,32)

>>> torch.matmul(a,b).shape

torch.size([4, 3, 28, 32])

broadcast

通道數目不同時,符合broadcast情況:

>>> import torch

>>> a = torch.rand(4,3,28,64)

>>> b = torch.rand(4,1,64,32)

>>> torch.matmul(a,b).shape

torch.size([4, 3, 28, 32])

維度不同、通道數目也不等時。不符合broadcast時會報錯:

>>> import torch

>>> a = torch.rand(4,3,28,64)

>>> b = torch.rand(4,64,32)

>>> torch.matmul(a,b).shape

traceback (most recent call last):

file "", line 1, in runtimeerror: the size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 1

兩種方法:

①任意次冪

>>> import torch

>>> a = torch.full([2,2],6)

>>> a.pow(3)

tensor([[216., 216.],

[216., 216.]])

②僅限平方

>>> import torch

>>> a = torch.full([2,2],6)

>>> a**2

tensor([[36., 36.],

[36., 36.]])

平方根:

>>> import torch

>>> a = torch.full([2,2],1024)

>>> a.sqrt(),a.rsqrt, a**0.5

(tensor([[32., 32.],

[32., 32.]]),

tensor([[32., 32.],

[32., 32.]]))

>>>

平方根的倒數:

>>> import torch

>>> a = torch.full([2,2],1024)

>>> a.rsqrt()

( tensor([[0.0312, 0.0312],

[0.0312, 0.0312]]))

我變懶了因為我困了!!三種:exp、log、log2寫在一起吧:

>>> import torch

>>> a = torch.exp(torch.ones(2,2))

>>> a,torch.log(a), torch.log2(a)

(tensor([[2.7183, 2.7183],

[2.7183, 2.7183]]),

tensor([[1., 1.],

[1., 1.]]),

tensor([[1.4427, 1.4427],

[1.4427, 1.4427]]))

>>> import torch

>>> a = torch.tensor(3.1415926)

>>> a.floor(), a.ceil(), a.trunc(), a.frac()

(tensor(3.), tensor(4.), tensor(3.), tensor(0.1416))

>>> import torch

>>> a = torch.tensor(3.499)

>>> b = torch.tensor(4.501)

>>> a.round(), b.round()

(tensor(3.), tensor(5.))

>>> import torch

>>> grad = torch.rand(2,3)*15

>>> grad.max(), grad.min()

(tensor(12.5018), tensor(4.8845))

>>> import torch

>>> grad = torch.rand(2,3)*15

>>> grad, grad.clamp(10),grad.clamp(5,10)

(tensor([[11.7561, 0.7950, 7.1729],

[11.2285, 5.1940, 14.5741]]),

tensor([[11.7561, 10.0000, 10.0000],

[11.2285, 10.0000, 14.5741]]),

tensor([[10.0000, 5.0000, 7.1729],

[10.0000, 5.1940, 10.0000]]))

沒睡午覺真的太睏了,最後這點**就放在一起列印吧。

tf建立tensor 建立Tensor

建立tensor from numpy,list zeros,ones,fill random if big dimension,random initial constant numpy,list numpy import numpy as np import tensorflow as tf t...

tf建立tensor 建立Tensor

建立tensor from numpy,list zeros,ones,fill random if big dimension,random initial constant numpy,list numpy import numpy as np import tensorflow as tf t...

tensor 增加維度 tensor維度變換

維度變換是tensorflow中的重要模組之一,前面mnist實戰模組我們使用了資料的壓平操作,它就是維度變換的應用之一。在詳解維度變換的方法之前,這裡先介紹一下view 檢視 的概念。所謂view,簡單的可以理解成我們對乙個tensor不同維度關係的認識。舉個例子,乙個 b,28,28,1 的te...