(一)神经网络的骨架 nn.Module

import torch
from torch import nn class Tudui(nn.Module):
def __init__(self):
super().__init__() def forward(self, input):
output = input+1
return output tudui = Tudui()
x = torch.tensor(1.0)
output = tudui(x)
print(output)

(二) 卷积操作conv2d:

stride:一次几步

padding:输入的边缘是否进行填充

import torch
import torch.nn.functional as F input = torch.tensor([[1, 2, 0, 3, 1],
[0, 1, 2, 3, 1],
[1, 2, 1, 0, 0],
[5, 2, 3, 1, 1],
[2, 1, 0, 1, 1]])
kernel = torch.tensor([[1, 2, 1],
[0, 1, 0],
[2, 1, 0]])
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3)) print(input.shape)
print(kernel.shape) output = F.conv2d(input, kernel, stride=1)
print(output) output2 = F.conv2d(input, kernel, stride=2)
print(output2) output3 = F.conv2d(input, kernel, stride=1, padding=1)
print(output3)

(三)MaxPool最大池化

kernel_size:

stride 不设置默认= kernel_size

import torch
from torch import nn
from torch.nn import MaxPool2d input = torch.tensor([[1, 2, 0, 3, 1],
[0, 1, 2, 3, 1],
[1, 2, 1, 0, 0],
[5, 2, 3, 1, 1],
[2, 1, 0, 1, 1]], dtype=torch.float32)
input = torch.reshape(input, (-1, 1, 5, 5))
print(input.shape) class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=False) def forward(self, input):
output = self.maxpool1(input)
return output tudui = Tudui()
output = tudui(input)
print(output)
import torch
import torchvision.datasets
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader # input = torch.tensor([[1, 2, 0, 3, 1],
# [0, 1, 2, 3, 1],
# [1, 2, 1, 0, 0],
# [5, 2, 3, 1, 1],
# [2, 1, 0, 1, 1]], dtype=torch.float32)
# input = torch.reshape(input, (-1, 1, 5, 5))
# print(input.shape)
from torch.utils.tensorboard import SummaryWriter dataset = torchvision.datasets.CIFAR10("../dataset", train=False, transform=torchvision.transforms.ToTensor(), download=False)
dataloader = DataLoader(dataset, batch_size=64) class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=False) def forward(self, input):
output = self.maxpool1(input)
return output tudui = Tudui()
step = 0
writer = SummaryWriter("../logs")
for data in dataloader:
imgs, target = data
writer.add_images("input_MaxPool", imgs, step)
output = tudui(imgs)
writer.add_images("output_MaxPool", output, step)
step = step+1 writer.close()

pytorch学习笔记(5)--神经网络的更多相关文章

  1. CNN学习笔记:神经网络表示

    CNN学习笔记:神经网络表示 双层神经网络模型 在一个神经网络中,当你使用监督学习训练它的时候,训练集包含了输入x还有目标输出y.隐藏层的含义是,在训练集中,这些中间节点的真正数值,我们是不知道的,即 ...

  2. 【pytorch】pytorch学习笔记(一)

    原文地址:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 什么是pytorch? pytorch是一个基于p ...

  3. TensorFlow学习笔记——深层神经网络的整理

    维基百科对深度学习的精确定义为“一类通过多层非线性变换对高复杂性数据建模算法的合集”.因为深层神经网络是实现“多层非线性变换”最常用的一种方法,所以在实际中可以认为深度学习就是深度神经网络的代名词.从 ...

  4. Pytorch学习笔记(二)---- 神经网络搭建

    记录如何用Pytorch搭建LeNet-5,大体步骤包括:网络的搭建->前向传播->定义Loss和Optimizer->训练 # -*- coding: utf-8 -*- # Al ...

  5. PyTorch学习笔记6--案例2:PyTorch神经网络(MNIST CNN)

    上一节中,我们使用autograd的包来定义模型并求导.本节中,我们将使用torch.nn包来构建神经网络. 一个nn.Module包含各个层和一个forward(input)方法,该方法返回outp ...

  6. 莫烦pytorch学习笔记(八)——卷积神经网络(手写数字识别实现)

    莫烦视频网址 这个代码实现了预测和可视化 import os # third-party library import torch import torch.nn as nn import torch ...

  7. 【深度学习】Pytorch 学习笔记

    目录 Pytorch Leture 05: Linear Rregression in the Pytorch Way Logistic Regression 逻辑回归 - 二分类 Lecture07 ...

  8. Pytorch学习笔记(一)——简介

    一.Tensor Tensor是Pytorch中重要的数据结构,可以认为是一个高维数组.Tensor可以是一个标量.一维数组(向量).二维数组(矩阵)或者高维数组等.Tensor和numpy的ndar ...

  9. [PyTorch 学习笔记] 3.1 模型创建步骤与 nn.Module

    本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson3/module_containers.py 这篇文章来看下 ...

  10. [PyTorch 学习笔记] 3.3 池化层、线性层和激活函数层

    本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson3/nn_layers_others.py 这篇文章主要介绍 ...

随机推荐

  1. JDK下载很慢

    方法1.直接百度jdk镜像,搜到华为镜像:https://repo.huaweicloud.com/java/jdk/ 方法2.到官网拷贝下载的url:https://download.oracle. ...

  2. 数位dp( tzoj6061:Bomb-求49个数;tzoj1427: 不要62)

    6061:http://www.tzcoder.cn/acmhome/problemdetail.do?method=showdetail&id=6061 dfs记忆化搜索 #include& ...

  3. python接口测试常见问题。

    一.入参问题 1.body字段类型 1.如果数据是从excel提取的那么中文数据会提示错误. 解决方法# media_value['body'] = media_value['body'].encod ...

  4. HJ77 火车进站

    描述 给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号,火车站只有一个方向进出,同时停靠在火车站的列车中,只有后进站的出站了,先 ...

  5. Unity中UGUI图片跟随文本自适应方法

    字体和图片层级如下 Text添加Content Size Fitter Image设置锚点

  6. 常用得cron表达式

    0 10 0 1 * ? //每月1号的0:10:00执行 0 01 00 28-31 * ? #月底最后一天早上凌晨1点执行 异常: 查询是否有特殊字符: cat -A ***.sh 解决方法: 1 ...

  7. ES5 绑定 this 的方法

    this的动态切换,固然为 JavaScript 创造了巨大的灵活性,但也使得编程变得困难和模糊.有时,需要把this固定下来,避免出现意想不到的情况.JavaScript 提供了call.apply ...

  8. C#封装FluentValidation

    FluentValidation是一个非常强大的用于构建强类型验证规则的 .NET 框架,帮程序员解决了繁琐的校验问题,用起来非常爽,但我还是遇到了一件非常不爽的事情,如下代码所示: public c ...

  9. sentinel监控数据持久化&本地测试

    官方文档  https://sentinelguard.io/zh-cn/ wiki: 在生产环境中使用-Sentinel 推荐方案:持久化到 时序数据库InfluxDB  : 结合Grafana 可 ...

  10. typora破解使用

    1.将example文件夹以及run.exe同时拷贝到typora安装目录下的\Typora\resources\ 里面. 2.然后运行run.exe,会出现一个cmd弹窗,然后会消失,此时,激活码k ...