天天看點

Pytorch的學習筆記(一:屬性統計&&範數講解)Pytorch的學習筆記(一:屬性統計&&範數講解)

Pytorch的學習筆記(一:屬性統計&&範數講解)

參考部落格連結:https://blog.csdn.net/wohu1104/article/details/107283396

                         https://zhuanlan.zhihu.com/p/68833474

tensor的基本運算函數

(一)mean均值    sum和    median中位數    mode衆數

(二)norm範數    dist距離

(三)std标準差(待更)    var方差(待更)

(四)cumsum累加    cumprod累積

(五)張量torch和數組numpy的轉換

以上大多數函數都有一個參數dim,用來指定這些操作是在哪個次元上執行的

(一)

import torch
a = torch.Tensor([[2, 4, 3], [4, 4, 4], [2, 2, 1]])
print(a)
print(a.size())

print("=" * 40) 

print(torch.mean(a))
print(a.mean())
print(torch.mean(a, dim = 0))
print(a.mean(dim = 0))
print(torch.mean(a, dim = 1))

print("=" * 40)

print(a.sum())
print(a.sum(dim = 0))
print(a.sum(dim = 1))

print("=" * 40)
#中位數
print(a.median())
print(a.median(dim = 0))
print(a.median(dim = 1))

print("=" * 40)
#衆數
print(a.mode())
print(a.mode(dim = 0))
print(a.mode(dim = 1))
           
tensor([[2., 4., 3.],
        [4., 4., 4.],
        [2., 2., 1.]])
torch.Size([3, 3])
========================================
tensor(2.8889)
tensor(2.8889)
tensor([2.6667, 3.3333, 2.6667])
tensor([2.6667, 3.3333, 2.6667])
tensor([3.0000, 4.0000, 1.6667])
========================================
tensor(26.)
tensor([ 8., 10.,  8.])
tensor([ 9., 12.,  5.])
========================================
tensor(3.)
torch.return_types.median(
values=tensor([2., 4., 3.]),
indices=tensor([0, 0, 0]))
torch.return_types.median(
values=tensor([3., 4., 2.]),
indices=tensor([2, 0, 0]))
========================================
torch.return_types.mode(
values=tensor([2., 4., 2.]),
indices=tensor([0, 2, 1]))
torch.return_types.mode(
values=tensor([2., 4., 1.]),
indices=tensor([2, 1, 2]))
torch.return_types.mode(
values=tensor([2., 4., 2.]),
indices=tensor([0, 2, 1]))
           

(二)

向量範數

0-範數:向量中非零元素的個數

1-範數:即向量元素絕對值之和

2-範數:(歐幾裡得範數、常用計算向量長度),即向量元素絕對值的平方和再開方、優化正則化項、避免過拟合

Pytorch的學習筆記(一:屬性統計&&範數講解)Pytorch的學習筆記(一:屬性統計&&範數講解)

-範數:即所有向量元素絕對值中最大值

p-範數:向量元素絕對值的p次方和的1/p次幂

矩陣範數

1-範數:所有矩陣列向量絕對值之和的最大值(列和範數)

2-範數:即

Pytorch的學習筆記(一:屬性統計&&範數講解)Pytorch的學習筆記(一:屬性統計&&範數講解)

矩陣最大特征值的開平方、

Pytorch的學習筆記(一:屬性統計&&範數講解)Pytorch的學習筆記(一:屬性統計&&範數講解)

Pytorch的學習筆記(一:屬性統計&&範數講解)Pytorch的學習筆記(一:屬性統計&&範數講解)

的最大特征值、

Pytorch的學習筆記(一:屬性統計&&範數講解)Pytorch的學習筆記(一:屬性統計&&範數講解)
Pytorch的學習筆記(一:屬性統計&&範數講解)Pytorch的學習筆記(一:屬性統計&&範數講解)

-範數:所有矩陣行向量絕對值之和的最大值(行和範數)

F-範數:Frobenius範數,即矩陣元素絕對值的平方和再開平方

核範數:矩陣奇異值之和【矩陣奇異值(待更)】

dist距離(預設即為歐式距離)

p = 1 歐式距離(相減的平方和 的 開方)

p = 2 哈密爾頓距離(相減絕對值累加)

import torch 
import torch.tensor as tensor
import numpy 
a = torch.ones(2, 3)
print(a)

#預設即為二範數
print(a.norm())
a2 = torch.norm(a)
print(a2)
a1 = torch.norm(a, p = 1)
print(a1)
a0 = torch.norm(a, p = 0)
print(a0)
ainf = torch.norm(a, p = float('inf'))
print(ainf)

a = torch.Tensor([[2, 4],[1, 3]])
print(a.norm())
print(a.norm(dim = 0))
print(a.norm(dim = 1))

b = torch.Tensor([4, 0])
print(a.dist(b))
print(torch.dist(a, b, p = 2))
print(torch.dist(a, b, p = 1))
           
tensor([[1., 1., 1.],
        [1., 1., 1.]])
tensor(2.4495)
tensor(2.4495)
tensor(6.)
tensor(6.)
tensor(1.)
tensor(5.4772)
tensor([2.2361, 5.0000])
tensor([4.4721, 3.1623])
tensor(6.1644)
tensor(6.1644)
tensor(12.)
           

(三)

import torch 
a = torch.Tensor([[2, 4], [1, 3]])

print(a.std())
print(a.std(dim = 0))
print(a.std(dim = 1))

print(a.var())
print(a.var(dim = 0))
print(a.var(dim = 1))
           
tensor(1.2910)
tensor([0.7071, 0.7071])
tensor([1.4142, 1.4142])
tensor(1.6667)
tensor([0.5000, 0.5000])
tensor([2., 2.])
           

(四)

cumsum、cumprod 累加累乘

import torch
a = torch.Tensor([[2, 4], [1, 3]])

print(a.cumsum(dim = 0))
print(a.cumsum(dim = 1))

print(a.cumprod(dim = 0))
print(a.cumprod(dim = 1))
           
tensor([[2., 4.],
        [3., 7.]])
tensor([[2., 6.],
        [1., 4.]])
tensor([[ 2.,  4.],
        [ 2., 12.]])
tensor([[2., 8.],
        [1., 3.]])
           

(五)

張量Tensors類似于numpy的ndarrays, 另外還可以在GPU上使用Tensors來加速計算

(張量的操作、移調,索引,切片,數學運算,線性代數,随機數等等描述)

import torch
x = torch.ones(5, 3)
print(x)
print(x.size())
y = torch.rand(5, 3)
print(x + y)
print(torch.add(x, y))

result = torch.Tensor(5, 3)
torch.add(x, y, out = result)
print(result)

y.add_(x)
print(y)

#輸出第一列的資訊
print(y[:, 0])
           
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
torch.Size([5, 3])
tensor([[1.1555, 1.2997, 1.1562],
        [1.1778, 1.8985, 1.9427],
        [1.4613, 1.5878, 1.8851],
        [1.8738, 1.6801, 1.8564],
        [1.6713, 1.5740, 1.1392]])
tensor([[1.1555, 1.2997, 1.1562],
        [1.1778, 1.8985, 1.9427],
        [1.4613, 1.5878, 1.8851],
        [1.8738, 1.6801, 1.8564],
        [1.6713, 1.5740, 1.1392]])
tensor([[1.1555, 1.2997, 1.1562],
        [1.1778, 1.8985, 1.9427],
        [1.4613, 1.5878, 1.8851],
        [1.8738, 1.6801, 1.8564],
        [1.6713, 1.5740, 1.1392]])
tensor([[1.1555, 1.2997, 1.1562],
        [1.1778, 1.8985, 1.9427],
        [1.4613, 1.5878, 1.8851],
        [1.8738, 1.6801, 1.8564],
        [1.6713, 1.5740, 1.1392]])
tensor([1.1555, 1.1778, 1.4613, 1.8738, 1.6713])
           

Numpy轉換

numpy數組和tensor張量可以互相轉換(可以使用.cuda功能将傳感器移動到GPU上 torch.cuda.is_available())

import torch
import numpy as np

a = torch.ones(5)
print(a)
#将tensor張量轉換為numpy數組
b = a.numpy()
print(b)

a.add_(1)
print(a)
print(b)

#numpy數組轉換成tensor張量
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out = a)
print(a)
print(b)
a[0] += 1
print(a)
print(b)
b[0] += 1
print(a)
print(b)
           
tensor([1., 1., 1., 1., 1.])
[1. 1. 1. 1. 1.]
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
[3. 2. 2. 2. 2.]
tensor([3., 2., 2., 2., 2.], dtype=torch.float64)
[4. 2. 2. 2. 2.]
tensor([4., 2., 2., 2., 2.], dtype=torch.float64)
           

繼續閱讀