一、Tensor基础

1、基本创建方法

Ⅰ,tensor创建

torch.Tensor(2,4) 创建一个2×4的二维张量;也可以通过列表进行自定义tensor元素
同理
torch.Tensor(5) 创建一个1×5的一维张量;
torch.Tensor(2,4,5) 创建一个2×4×5的三维张量,说白了也就是2个4×5的二维向量叠加

import torch

x = torch.Tensor(2,4)
x
"""
tensor([[7.8473e-44, 8.1275e-44, 7.0065e-44, 7.5670e-44],
        [8.1275e-44, 6.8664e-44, 7.0065e-44, 6.4460e-44]])
"""

x1 = torch.Tensor([[1,2,3],[4,1,2]])
x1
"""
tensor([[1., 2., 3.],
        [4., 1., 2.]])
"""

y = torch.Tensor(5)
y
"""
tensor([4.3911e-05, 4.0370e-08, 1.3733e-05, 1.0548e-08, 4.1886e-11])
"""


z = torch.Tensor(3,4,5)
z
"""
tensor([[[-9.4222e+03,  5.7173e-43, -2.5867e-08,  2.0179e-43,  0.0000e+00],
         [ 0.0000e+00,  2.1019e-44,  0.0000e+00, -9.1735e+03,  5.7173e-43],
         [-2.5867e-08,  2.0179e-43,  0.0000e+00,  0.0000e+00,  2.1019e-44],
         [ 0.0000e+00, -1.0077e+04,  5.7173e-43, -2.5867e-08,  2.0179e-43]],

        [[ 0.0000e+00,  0.0000e+00,  2.1019e-44,  0.0000e+00, -1.0126e+04],
         [ 5.7173e-43, -2.5867e-08,  2.0179e-43,  0.0000e+00,  0.0000e+00],
         [ 2.1019e-44,  0.0000e+00, -7.9165e+03,  5.7173e-43, -2.5867e-08],
         [ 2.0179e-43,  0.0000e+00,  0.0000e+00,  2.1019e-44,  0.0000e+00]],

        [[-9.9800e+03,  5.7173e-43, -2.5867e-08,  2.0179e-43,  0.0000e+00],
         [ 0.0000e+00,  2.1019e-44,  0.0000e+00, -1.0127e+04,  5.7173e-43],
         [-2.5867e-08,  2.0179e-43,  0.0000e+00,  0.0000e+00,  2.1019e-44],
         [ 0.0000e+00, -1.0132e+04,  5.7173e-43, -2.5867e-08,  2.0179e-43]]])
"""

Ⅱ,tpye()和dtpye

type()是python内置的函数,返回数据结构类型(list、dict、numpy.ndarray 等)

dtype返回数据元素的数据类型(int、float等)

import torch

x = torch.Tensor(2,4)
x.type() #'torch.FloatTensor'
x.dtype #torch.float32

Ⅲ,对List数据结构进行操作

List转tensor

import torch

list = [[1,2,3],[4,5,6],[7,8,9]]
torch.Tensor(list)
"""
tensor([[1., 2., 3.],
        [4., 5., 6.],
        [7., 8., 9.]])
"""

对tensor通过索引进行操作

import torch

x = torch.Tensor([[3,4,5],[1,2,3]])
x
"""
tensor([[3., 4., 5.],
        [1., 2., 3.]])
"""
x[0][2] # tensor(5.)

x[0][2] = 8
x
"""
tensor([[3., 4., 8.],
        [1., 2., 3.]])
"""

Ⅳ,常用快速创建tensor的函数

红色字体可以按需修改

函数 说明
torch.zeros(2,3) 创建2×3全0的tensor
troch.ones(3,2) 创建3×2全1的tensor
torch.rand(2,5) 创建在[0,1)区间的2×5的tensor
torch.arange(1,4,0.5) 创建在[1,4)区间0.5步长的一维tensor
torch.linspace(1,5,8) [1,5)区间均匀划分8个一维tensor
torch.eye(3) 创建3×3的单位矩阵tensor
import torch

x = torch.zeros(2,3)
x
"""
tensor([[0., 0., 0.],
        [0., 0., 0.]])
"""

y = torch.linspace(1,5,4)
y # tensor([1.0000, 2.3333, 3.6667, 5.0000])


z = torch.rand(2,5)
z 
"""
tensor([[0.7398, 0.4534, 0.8604, 0.0465, 0.8646],
        [0.7877, 0.5388, 0.6655, 0.3565, 0.9552]])
"""

Ⅴ,常用数学操作

举几个例子就行,其他的到后面实战的时候再查就行

函数 说明
torch.add(a,b) a和b为相同形状的tensor,每个元素对应相加
a.add(b) 以a为基础,将b的元素对应加到a上,不覆盖a对象
a.add_(b) 以a为基础,将b的元素对应加到a上,且覆盖a对象
torch.sum(a) 返回a中所有元素之和
torch.max(a) 返回a中所有元素的最大值
torch.abs(a) 返回a中所有元素的绝对值
import torch

a = torch.Tensor([[1,2,3],[1,2,2]])
b = torch.zeros(2,3)

torch.add(a,b)
"""
tensor([[1., 2., 3.],
        [1., 2., 2.]])
"""

b.add(a)
b
"""
tensor([[0., 0., 0.],
        [0., 0., 0.]])
"""

b.add_(a)
b
"""
tensor([[1., 2., 3.],
        [1., 2., 2.]])
"""

torch.max(a) # tensor(3.)

Ⅵ,常用线性代数运算

函数 说明
torch,dot(a,b) 向量a和向量点积(内积),行向量列向量 ,即对应元素相乘再相加,a和b为一维的tensor,
torch.mv(a,b) 矩阵a和向量b乘法,矩阵列向量,a为二维tensor,b为一维tensor
torch.mm(a,b) 矩阵a和矩阵b乘法,矩阵矩阵前行后列 ,a和b为二维tensor
import torch

a = torch.Tensor([2,2,2])
b = torch.Tensor([1,2,3])
c = torch.Tensor([[1,2,1],[2,1,1,],[3,2,2,]])

torch.dot(a,b) # tensor(12.)
torch.mv(c,a) # tensor([ 8.,  8., 14.])
torch.mm(c,c)
"""
tensor([[ 8.,  6.,  5.],
        [ 7.,  7.,  5.],
        [13., 12.,  9.]])
"""

Ⅶ,拼接

这个函数在神经网络模型中还是用的比较多的

函数 说明
torch.cat((a,b),0) a和b两个相同维度的tensor在第一个维度拼接,竖着拼接,联想0是竖着的
torch.cat((a,b),1) a和b两个相同维度的tensor在第二个维度拼接,横着拼接,联想汉字一是横着的
import torch

x = torch.Tensor([[1,2,3],[2,3,1]])
y = torch.Tensor([[4,3,2],[9,9,8]])

torch.cat((x,y),1)
"""
tensor([[1., 2., 3., 4., 3., 2.],
        [2., 3., 1., 9., 9., 8.]])
"""

torch.cat((x,y),0)
"""
tensor([[1., 2., 3.],
        [2., 3., 1.],
        [4., 3., 2.],
        [9., 9., 8.]])
"""

Ⅷ,切片

函数 说明
torch.chunk(a,3,1) a对象中元素切分为3块,按第一维度(0,上下分)进行切分
torch.chunk(a,2,1) a对象中元素切分为2块,按第二维度(1,左右分)进行切分
torch.t(a) a为二维tensor对象,求其转置矩阵
import torch

a = torch.Tensor([[1,2,3],[3,3,2,],[1,2,3],[9,8,2]])
a
"""
tensor([[1., 2., 3.],
        [3., 3., 2.],
        [1., 2., 3.],
        [9., 8., 2.]])
"""

torch.chunk(a,3,1)
"""
(tensor([[1.],
         [3.],
         [1.],
         [9.]]),
 tensor([[2.],
         [3.],
         [2.],
         [8.]]),
 tensor([[3.],
         [2.],
         [3.],
         [2.]]))
"""

torch.chunk(a,2,0)
"""
(tensor([[1., 2., 3.],
         [3., 3., 2.]]),
 tensor([[1., 2., 3.],
         [9., 8., 2.]]))
"""

Ⅸ,改变现状

这个函数在神经网络模型中还是用的比较多的

函数 说明
a.view(2,3) a这个tensor转换为2×3的tensor
a.view(-1,3) a这个tensor转换为x×3的tensor,其中x会自动进行计算
import torch

x = torch.rand(5,4)
x.size() # torch.Size([5, 4])

y = x.view(2,10)
y.size() # torch.Size([2, 10])

z = x.view(-1,5)
z.size() # torch.Size([4, 5])
 

2、Autograd自动微分

Ⅰ,前向传播

Tensor对象都有有个属性requires_grad,将其设置为True即可让PyTroch自动计算Tensor对象的微分,grad_fn为微分函数

import torch
x = torch.Tensor([[1,2],[3,2]])
x
"""
tensor([[1., 2.],
        [3., 2.]])
"""

x.requires_grad = True
x
"""

tensor([[1., 2.],
        [3., 2.]], requires_grad=True)
"""

Ⅱ,反向传播

调用backward()函数,可实现对整个图进行反向传播,从而求出微分值
在这里插入图片描述

因为输出节点y为非标量,使用backward()函数的时候需要传入一个与输出节点现状保持一致且元素值均为1的参数,即y.backward(torch.ones(y.size()))

import torch

x = torch.Tensor([[1,3],[4,5]])
x.requires_grad = True
x
"""
tensor([[1., 3.],
        [4., 5.]], requires_grad=True)
"""

y = 2*x*x
y
"""
tensor([[ 2., 18.],
        [32., 50.]], grad_fn=<MulBackward0>)
"""

y.backward(torch.ones(y.size()))

x.grad
"""
tensor([[ 4., 12.],
        [16., 20.]])
"""

猜你喜欢

转载自blog.csdn.net/qq_41264055/article/details/127422463
今日推荐