Pytorch基礎

1、Tensor的數據類型

#torch.FloatTensor
#用於生成數據類型爲浮點型的tensor;可以是列表,也可以是一個維度值
import torch
a = torch.FloatTensor(2,3)
b = torch.FloatTensor([2,3,4,5])
print(a)
print(b)
tensor([[4.3911e-05, 2.5837e-06, 1.4013e-45],
        [0.0000e+00, 1.4013e-45, 0.0000e+00]])
tensor([2., 3., 4., 5.])
#torch.IntTensor
#用於生成數據類型爲整型的tensor;可以是列表也可以是一個維度值
import torch
a = torch.IntTensor(2,3)
b = torch.IntTensor([2,3,4,5])
print(a)
print(b)
tensor([[0, 0, 1],
        [0, 1, 0]], dtype=torch.int32)
tensor([2, 3, 4, 5], dtype=torch.int32)
#torch.rand
#用於生成數據類型爲浮點型維度指定的隨機Tensor,數據再0~1區間均勻分佈
import torch
a = torch.rand(2,3)
print(a)
tensor([[0.6461, 0.9960, 0.6996],
        [0.8205, 0.9701, 0.1077]])
#torch.randn
#用於生成數據類型爲浮點型且維度指定的隨機Tensor,隨機生成的浮點數的值滿足均值爲0、方差爲1的正太分佈
import torch
a = torch.randn(2,3)
print(a)
tensor([[ 2.5009,  1.9747, -0.7621],
        [-1.5001, -1.9020, -0.7367]])
#torch.range(start,end,stride)
#這個版本使用會提示出錯,可以使用torch.arange()
#數據類型爲浮點型,起始值、範圍的結束值和步長,步長用於指定從起始值到結束值得每步得數據間隔
import torch
a = torch.arange(1,20,1)
print(a)
tensor([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
        19])
#torch.zeros
#用於生成數據類型爲浮點型且維度指定的Tensor,全爲0
import torch
a = torch.zeros(2,3)
print(a)
tensor([[0., 0., 0.],
        [0., 0., 0.]])

2、Tensor運算
可以對tensor進行一些變量運算,來組合一些簡單或者複雜的算法

#torch.abs
#絕對值操作
import torch
a = torch.randn(2,3)
print(a)

b = torch.abs(a)
print(b)
tensor([[ 0.4567, -0.7084,  0.6732],
        [-0.1820, -0.5106, -0.2402]])
tensor([[0.4567, 0.7084, 0.6732],
        [0.1820, 0.5106, 0.2402]])
#torch.add:
#將兩個變量進行相加操作,可以都是變量,或者一個變量一個標量
import torch
a = torch.randn(2,3)
print(a)

b = torch.add(2,3)
print(b)

c = torch.randn(2,3)
print(c)

d = torch.add(c,10);
print(d)

e = torch.add(c,a)
print(e)
tensor([[-0.5469, -0.1745, -0.1805],
        [ 1.2738,  1.2487,  1.0765]])
tensor(5)
tensor([[ 0.5091, -0.2509,  1.0934],
        [ 0.5372, -0.3160, -0.4407]])
tensor([[10.5091,  9.7491, 11.0934],
        [10.5372,  9.6840,  9.5593]])
tensor([[-0.0379, -0.4255,  0.9129],
        [ 1.8110,  0.9328,  0.6358]])
#torch.clamp(待裁剪的變量,上邊界,下邊界)
#對參數在指定範圍進行裁剪操作
import torch
a = torch.randn(2,3)
print(a)

b = torch.clamp(a,-2,0.1)
print(b)
tensor([[ 0.2326, -0.9193, -0.2051],
        [-1.1646,  0.4585, -0.7207]])
tensor([[ 0.1000, -0.9193, -0.2051],
        [-1.1646,  0.1000, -0.7207]])
#torch.div
#將參數傳遞到torch.div後返回參數求商的結果
import torch
a = torch.randn(2,3)
print(a)

b = torch.randn(2,3)
print(b)

c = torch.div(a,b)
print(c)

d = torch.randn(2,3)
print(d)

e = torch.div(d,10)
print(e)
tensor([[ 0.8628, -0.6982,  1.8959],
        [ 2.0429, -1.3072,  0.6143]])
tensor([[ 1.1124, -0.8019, -0.6956],
        [-1.3661,  0.7264, -2.0969]])
tensor([[ 0.7756,  0.8707, -2.7255],
        [-1.4955, -1.7997, -0.2929]])
tensor([[ 1.0224, -1.5853,  2.2047],
        [-0.0252, -0.0237, -0.4011]])
tensor([[ 0.1022, -0.1585,  0.2205],
        [-0.0025, -0.0024, -0.0401]])
#torch.mul
#返回乘積的結果
import torch
a = torch.randn(2,3)
print(a)

b = torch.randn(2,3)
print(b)

c = torch.mul(a,b)
print(c)

d = torch.randn(2,3)
print(d)

e = torch.mul(d,10)
print(e)
tensor([[0.5938, 0.9085, 1.9268],
        [1.8739, 1.5410, 0.4270]])
tensor([[ 0.4848, -0.4941,  1.5471],
        [ 0.2296,  1.1991,  0.9234]])
tensor([[ 0.2879, -0.4489,  2.9809],
        [ 0.4303,  1.8478,  0.3943]])
tensor([[-0.0193,  0.6371, -0.9642],
        [-1.1746,  1.3586, -0.8532]])
tensor([[ -0.1926,   6.3712,  -9.6417],
        [-11.7459,  13.5864,  -8.5325]])
#torch.pow
#返回輸入參數的求冪結果作爲輸出
import torch
a = torch.IntTensor([2,3,4])
print(a)

b = torch.pow(a,2)
print(b)
tensor([2, 3, 4], dtype=torch.int32)
tensor([ 4,  9, 16], dtype=torch.int32)
#torch.mm
#返回矩陣相乘的結果
#兩者的維度必須滿足矩陣乘積的條件
import torch
a = torch.randn(2,3)
print(a)

b = torch.randn(3,2)
print(b)

c = torch.mm(a,b)
print(c)
tensor([[-0.9642,  1.4570, -0.0293],
        [-0.2923,  0.4503, -0.5410]])
tensor([[-0.9322, -0.8108],
        [ 1.3484,  0.8090],
        [ 0.8773,  0.2482]])
tensor([[2.8378, 1.9531],
        [0.4051, 0.4670]])
#torch.mv(矩陣,向量)
#返回輸入參數的求積結果
import torch
a = torch.randn(2,3)
print(a)

b = torch.randn(3)
print(b)

c = torch.mv(a,b)
print(c)
tensor([[ 1.5491, -0.0762, -2.0008],
        [ 1.1197, -1.2172, -0.0780]])
tensor([0.6538, 0.2944, 0.7295])
tensor([-0.4692,  0.3168])
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章