参考博客链接: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-范数:(欧几里得范数、常用计算向量长度),即向量元素绝对值的平方和再开方、优化正则化项、避免过拟合
-范数:即所有向量元素绝对值中最大值
p-范数:向量元素绝对值的p次方和的1/p次幂
矩阵范数
1-范数:所有矩阵列向量绝对值之和的最大值(列和范数)
2-范数:即矩阵最大特征值的开平方、
为
的最大特征值、
-范数:所有矩阵行向量绝对值之和的最大值(行和范数)
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数组和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)