前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytorch入坑之Tensor

pytorch入坑之Tensor

作者头像
Max超
发布2020-03-27 11:33:22
1.3K0
发布2020-03-27 11:33:22
举报
文章被收录于专栏:蓝桥杯历年省赛真题集
一、张量的定义

张量 标量:0维张量 向量:1维张量 矩阵:2维张量 张量:一个多维数组,标量、向量、矩阵的高维扩展 Tensor

属性

意义

data

张量数据值

dtype

张量数据类型

shape

张量形状

device

张量所在设备

requires_grad

是否需要梯度

grad

求导梯度值

grad_fn

求导过来的操作

is_leaf

是否是叶子结点

二、张量的创建

直接创建

方法

说明

torch.tesor()

从data创建tensor

torch.from_numpy

从numpy创建tensor

演示

代码语言:javascript
复制
import torch
import numpy as np

data = np.arange(1,10)
tensor1 = torch.tensor(data)
tensor2 = torch.from_numpy(data)#与data共用一个地址
print("data:",data)
print("tensor1:",tensor1)
print("tensor2:",tensor2)
for num in tensor2:
    num+=10
print("after data:",data)

结果

代码语言:javascript
复制
data: [1 2 3 4 5 6 7 8 9]
tensor1: tensor([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=torch.int32)
tensor2: tensor([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=torch.int32)
after data: [11 12 13 14 15 16 17 18 19]

依据数值创建

方法

说明

torch.zeros()

创建全0张量

torch.zeros_like()

依input创建全0张量

torch.full()

创建规定的统一值张量

torch.full_like()

依input创建统一张量

torch.arange()

创建等差的1维张量

torch.linspace()

创建均分的1维张量

torch.eye()

创建单位对角矩阵

演示

代码语言:javascript
复制
import torch
import numpy as np

tensor = torch.tensor([1])
tensor1 = torch.tensor([[1,2],[3,4]])
#out会改变原有张量的地址
zeros = torch.zeros((3,3),out = tensor)
zerosLike = torch.zeros_like(tensor1)

#等差值为
arange = torch.arange(1,10,2)
#分成5分
linspace = torch.linspace(0,10,5)

print("tensor:",tensor)
print("zeros:",zeros)
print("zerosId:",id(zeros))
print("tensorId:",id(tensor))
print("zerosLike",zerosLike)
print("arange:",arange)
print("linspace: ",linspace)

结果

代码语言:javascript
复制
tensor: tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
zeros: tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
zerosId: 35145912
tensorId: 35145912
zerosLike tensor([[0, 0],
        [0, 0]])
arange tensor([1, 3, 5, 7, 9])
linspace tensor([ 0.0000,  2.5000,  5.0000,  7.5000, 10.0000])

依据概率分布创建

方法

用法

torch.normal()

按照正态分布创建张量

torch.randn()

按照标准正态分布

torch.rand

[0,1)均匀分布

torch.randint()

生成整数均匀分布

torch.randperm()

[0,1)随机排列

二、张量的操作

拼接与切分

方法

用法

torch.cat()

在原有维度拼接

torch.stack()

创建新的维度拼接

torch.chunk()

按维度平均切分

torch.split()

指定长度切分

演示

代码语言:javascript
复制
import torch
import numpy as np

tensor1 = torch.tensor([[1,1],[3,3],[2,2]])
cat0 = torch.cat([tensor1,tensor1],dim=0)
cat1 = torch.cat([tensor1,tensor1],dim=1)
print("cat0:",cat0)
print("cat1:",cat1)

#创建新的维度拼接
stack0 = torch.stack([tensor1,tensor1],dim = 0)
print("stack0:",stack0,stack0.shape)

tensor2 = torch.ones((3,8))
chunk0 = torch.chunk(tensor2,4,dim=1)
list_chunk0 = [n.shape for n in chunk0]
print("list_chunk0:",list_chunk0)
#取整不够则向上取
chunk1 = torch.chunk(tensor2,3,dim=1)
list_chunk1 = [n.shape for n in chunk1]
print("list_chunk1:",list_chunk1)

split = torch.split(tensor2,[1,2,3,2],dim=1)
list_split = [n.shape for n in split]
print("list_split:",list_split)

结果

代码语言:javascript
复制
cat0: tensor([[1, 1],
        [3, 3],
        [2, 2],
        [1, 1],
        [3, 3],
        [2, 2]])
cat1: tensor([[1, 1, 1, 1],
        [3, 3, 3, 3],
        [2, 2, 2, 2]])
stack0: tensor([[[1, 1],
         [3, 3],
         [2, 2]],

        [[1, 1],
         [3, 3],
         [2, 2]]]) torch.Size([2, 3, 2])
list_chunk0: [torch.Size([3, 2]), torch.Size([3, 2]), torch.Size([3, 2]), torch.Size([3, 2])]
list_chunk1: [torch.Size([3, 3]), torch.Size([3, 3]), torch.Size([3, 2])]
list_split: [torch.Size([3, 1]), torch.Size([3, 2]), torch.Size([3, 3]), torch.Size([3, 2])]

张量索引

方法

说明

torch.index_select()

在维度dim上,按inde索引

torch.masked_select()

按照mask中true进行索引

张量变换

方法

说明

torch.reshape()

变换张量形状

torch.transpose()

交换两个维度

torch.t()

二维张量互换

torch.squeeze()

压缩长度为1的维度

torch.unsqueeze()

扩展长度为1的维度

三、自动求导

torch.autograd.backward() 演示

代码语言:javascript
复制
import torch

w = torch.tensor([1.],requires_grad=True)
x = torch.tensor([2.],requires_grad=True)

a = torch.add(w,x)
b = torch.add(w,1)
#y = (w+x)*(w+1)
y = torch.mul(a,b) 

y.backward()
print(w.grad)

结果

代码语言:javascript
复制
tensor([5.])

torch.autograd.grad 演示

代码语言:javascript
复制
import torch

x = torch.tensor([3.],requires_grad=True)
y = torch.pow(x,2)
grad_1 = torch.autograd.grad(y,x,create_graph=True)
print(grad_1)
grad_2 = torch.autograd.grad(grad_1[0],x)
print(grad_2)

结果

代码语言:javascript
复制
(tensor([6.], grad_fn=<MulBackward0>),)
(tensor([2.]),) 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、张量的定义
  • 二、张量的创建
  • 二、张量的操作
  • 三、自动求导
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档