pytorch学习笔记

参考资料:

  1. 安装pytorch方法

    我是先右键访问那个whl文件的网址,然后浏览器就自动开始下,下好whl文件后用pip安装。

  2. 李理:详解卷积神经网络

    通过这个了解了卷积和池化的作用:降低参数数目及不变性。其实我很久以前就意识到了卷积很重要并写过几篇卷积的博客。

  3. 零基础入门深度学习(4) - 卷积神经网络

    卷积神经网络的具体参数的含义及计算。

  4. XavierLin的炼丹房

    这个博客逻辑上看着很舒服,首先要讲 计算图 computation graph,计算图理解后,再涉及到具体的 tensor,variable,function,model等 概念,用这些东西去 构建 计算图,最后再提 优化 部分,比如 backward,optim 等。这样的逻辑才是自然而然的,对初学者友好的。

  5. yunjey/pytorch-tutorial

    很干净漂亮的pytorch入门代码

  6. PyTorch深度学习:60分钟入门
  7. pytorch官方文档
  8. pytorch官方文档的中文版
  9. python笔记

    一份不错的python笔记。

    上述关于pytorch的参考资料是有先后顺序的,大致按照这个顺序来看的话比较容易上手

TODO:

  • torch.nn.functional.conv2d
  • torch.nn.Conv2d
  • torch.nn.functional.max_pool2d
  • 模型保存和载入
  • minist构建和训练

1. torch.nn.functional.conv2d

import numpy as np
import torch
from torch.autograd import Variable
import torch.nn.functional as F 

# nSamples x nChannels x Height x Width
#inputs = autograd.Variable(torch.randn(1,1,5,5))
#filters = autograd.Variable(torch.randn(1,1,3,3))
#F.conv2d(inputs, filters)
# 这个是官网文档的例子,注意randn生成的是 FloatTensor
# inputs 和 filters 也都是 4 维的

# 测试数据来源:https://www.zybuluo.com/hanbingtao/note/485480
dtype = torch.FloatTensor

inputs = np.array([[1,1,1,0,0],
                   [0,1,1,1,0],
                   [0,0,1,1,1],
                   [0,0,1,1,0],
                   [0,1,1,0,0]])
inputs = inputs[np.newaxis, np.newaxis, :]
inputs = torch.from_numpy(inputs)
inputs = Variable(inputs.type(dtype)) # 不转换成 FloatTensor 会报错

filters = np.array([[1,0,1],
                    [0,1,0],
                    [1,0,1]])
filters = filters[np.newaxis, np.newaxis, :]
filters = torch.from_numpy(filters)
filters = Variable(filters.type(dtype)) # 不转换成 FloatTensor 会报错

F.conv2d(inputs, filters)
#   结果如下,与参考链接中的一致
#    Variable containing:
#    (0 ,0 ,.,.) = 
#      4  3  4
#      2  4  3
#      2  3  4
#    [torch.FloatTensor of size 1x1x3x3]

2. torch.nn.Conv2d

参考资料:

Convolution arithmetic 动图演示

# torch.nn.Conv2d(in_channels,  # int – 输入信号的通道
#                out_channels, # int – 卷积产生的通道
#                kernel_size,  # int or tuple - 卷积核的尺寸
#                stride=1,     # int or tuple, optional - 卷积步长
#                padding=0,  # int or tuple, optional - 输入的每一条边补充0的层数
#                dilation=1, # int or tuple, optional – 卷积核元素之间的间距
#                groups=1,  # int, optional – 从输入通道到输出通道的阻塞连接数
#                bias=True)  # bool, optional - 如果bias=True,添加偏置
import torch
import torch.nn as nn

# 注意: torch.nn 只接受小批量的数据
# 整个torch.nn包只接受那种小批量样本的数据,而非单个样本。 例如,nn.Conv2d
# 能够接收一个四维的Tensor:  nSamples x nChannels x Height x Width。
# 如果你拿的是单个样本,使用input.unsqueeze(0)来加一个假维度就可以了。

inputs = Variable(torch.randn(1,1,5,5))
m = nn.Conv2d(1, 1, 3) #  1 input,1 output,kernelsize 3

output = m(inputs)
print output

3. torch.nn.functional.max_pool2d

import numpy as np
import torch
from torch.autograd import Variable
import torch.nn.functional as F 

inputs = np.array([[1,2,3,4],
                   [0,7,8,9],
                   [0,6,5,1],
                   [0,4,1,1]])
inputs = inputs[np.newaxis, np.newaxis, :] # 3D or 4D

dtype = torch.FloatTensor
inputs = torch.from_numpy(inputs)
inputs = Variable(inputs.type(dtype)) # dtype 

#F.max_pool2d(inputs,2)
F.max_pool2d( inputs,(2,2) ) # 

编辑时间记录

2017年07月29日22:50:22

2017年08月05日23:06:49

2017年08月06日00:41:14

2017年08月13日15:27:44

2017年10月05日17:04:16

2017年10月07日14:24:28