PyTorch入门笔记一

Stella981
• 阅读 740

张量

引入pytorch,生成一个随机的5x3张量

>>> from __future__ import print_function
>>> import torch
>>> x = torch.rand(5, 3)
>>> print(x)
tensor([[0.5555, 0.7301, 0.5655],
        [0.9998, 0.1754, 0.7808],
        [0.5512, 0.8162, 0.6148],
        [0.8618, 0.3293, 0.6236],
        [0.2787, 0.0943, 0.2074]])

声明一个5x3的张量,张量中所有元素初始化为0

>>> x = torch.zeros(5, 3, dtype=torch.long)

从数据直接构造张量,这里的数据一般是python数组

>>> x = torch.tensor([5.5, 3])
>>> print(x)
tensor([5.5000, 3.0000])

从一个已有的tensor上类似创建新的张量,新、旧张量的形状和数据类型相同,除非对dtype进行了覆盖声明

>>> x = x.new_ones(5, 3, dtype=torch.double) 
>>> print(x)
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
>>> y = torch.rand_like(x, dtype=torch.float)
>>> print(y)
tensor([[0.6934, 0.9637, 0.0594],
        [0.0863, 0.6638, 0.4728],
        [0.3416, 0.0892, 0.1761],
        [0.6831, 0.6404, 0.8307],
        [0.6254, 0.4180, 0.2174]])

张量的size,numpy里是shape

>>> print(x.size())
torch.Size([5, 3])

张量的操作

张量相加

>>> x=torch.rand(5, 3)
>>> y = torch.zeros(5, 3)
>>> print(x + y)
tensor([[0.8991, 0.9222, 0.2050],
        [0.2478, 0.7688, 0.4156],
        [0.4055, 0.9526, 0.2559],
        [0.9481, 0.8576, 0.4816],
        [0.0767, 0.3346, 0.0922]])

>>> print(torch.add(x, y))
tensor([[0.8991, 0.9222, 0.2050],
        [0.2478, 0.7688, 0.4156],
        [0.4055, 0.9526, 0.2559],
        [0.9481, 0.8576, 0.4816],
        [0.0767, 0.3346, 0.0922]])

>>> result = torch.empty(5, 3)
>>> torch.add(x, y, out=result)
tensor([[0.8991, 0.9222, 0.2050],
        [0.2478, 0.7688, 0.4156],
        [0.4055, 0.9526, 0.2559],
        [0.9481, 0.8576, 0.4816],
        [0.0767, 0.3346, 0.0922]])

>>> y.add_(x)
tensor([[0.8991, 0.9222, 0.2050],
        [0.2478, 0.7688, 0.4156],
        [0.4055, 0.9526, 0.2559],
        [0.9481, 0.8576, 0.4816],
        [0.0767, 0.3346, 0.0922]])

张量内元素访问形式和numpy保持一致,如输出张量y的第二维度上下标是1的所有元素

>>> print(y[:, 1])
tensor([0.9222, 0.7688, 0.9526, 0.8576, 0.3346])

iew函数改变tensor的形状,类似numpy的reshape

>>> x = torch.randn(4, 4)
>>> y = x.view(16)  # 变成1x16的张量
>>> z = x.view(-1, 8)  # 变成第二维度是8,第一维度自动计算的张量,结果是2x8的张量
>>> print(x.size(), y.size(), z.size())
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

只有一个元素的向量,取这个元素

>>> x = torch.randn(1)
>>> print(x)
tensor([0.8542])
>>> print(x.item())
0.8541867136955261

转换成numpy数组

>>> x = torch.rand(5, 3)
>>> x.numpy()
array([[0.9320856 , 0.473859  , 0.6787642 ],
       [0.14365482, 0.1112923 , 0.8280207 ],
       [0.4609589 , 0.51031697, 0.15313298],
       [0.18854082, 0.4548    , 0.49709243],
       [0.8351501 , 0.6160053 , 0.61391556]], dtype=float32)

除CharTensor外,所有的cpu张量从numpy转换成tensor

import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)

在cpu和gpu之间移动tensor,

if torch.cuda.is_available():
    device = torch.device("cuda")          # a CUDA device object
    y = torch.ones_like(x, device=device)  # 直接在GPU设备上创建
    x = x.to(device)                       # or just use strings ``.to("cuda")``
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))       # ``.to`` can also change dtype together!

构建网络和损失函数

损失函数用来衡量输入和目标之间的距离

from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    ## 定义了网络的结构
    def __init__(self):
        super(Net, self).__init__()
        ## input is channel 1, output 6 channels with 3x3 convulutionanl kernel
        self.conv1 = nn.Conv2d(1, 6, 3) 
        self.conv2 = nn.Conv2d(6, 16, 3)
        # an affine operation: y = Wx + b,  # 6*6 from image dimension
        self.fc1 = nn.Linear(16*6*6, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
    
    ## 前向传播,函数名必须是forward   
    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x
    
    def num_flat_features(self, x):
        size = x.size()[1:] # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features
    
## 新建一个Net对象
net = Net() 
print(net)  
params = list(net.parameters())
print(len(params))
print(params[0].size())  # conv1's .weight

# 声明一个1x1x32x32的4维张量作为网络的输入
input = torch.randn(1, 1, 32, 32) 
# input = torch.randn(1, 1, 32, 32)
output = net(input) 

# net.zero_grad()
# out.backward(torch.randn(1, 10))
target = torch.randn(10) 
target = target.view(1, -1) 
criterion = nn.MSELoss()
loss = criterion(output, target)
print(loss)

print(loss.grad_fn)  # MSELoss
print(loss.grad_fn.next_functions[0][0])  # Linear
print(loss.grad_fn.next_functions[0][0].next_functions[0][0])  # ReLU

网络的反向传播,为了反向传播损失(error)所做的只需要调用loss.backward()函数,如果没有清除已有的梯度,反向传播会累积梯度

调用loss.backward()函数,看以下conv1的bias的梯度在调用前后的差别。

net.zero_grad()     # zeroes the gradient buffers of all parameters

print('conv1.bias.grad before backward')
print(net.conv1.bias.grad)

loss.backward()

print('conv1.bias.grad after backward')
print(net.conv1.bias.grad)

 使用SGD更新权重

公式:weight = weight - learning_rate * gradient

可以用下面的torch代码实现

learning_rate = 0.01
for f in net.parameters():
    f.data.sub_(f.grad.data * learning_rate)

 但是torch已经实现了各种权重更新方式,比如SGD, Nesterov-SGD, Adam, RMSProp等,可以直接调用

import torch.optim as optim

# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)

# in your training loop:
optimizer.zero_grad()   # zero the gradient buffers
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step()    # Does the update
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这