(一)神经网络的骨架 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. CentOS 7.4使用yum源安装MySQL 5.7.20

    CentOS 7.4使用yum源安装MySQL 5.7.20 小牛教程 InnoDB存储引擎 2022年11月25日 从CentOS 7.0发布以来,yum源中开始使用Mariadb来代替MySQL的 ...

  2. 堆QAQ

    L2-012 关于堆的判断 将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: x is the root:x是根结点: x and y are si ...

  3. 针对Vmware打开BIOS中VT虚拟化相关后仍然报错

    安装虚拟机时报错问题现象:  通过VMware虚拟机安装Centos时提示弹出提示框,"已将该虚拟机配置为使用64位客户机操作系统,但是,无法执行64位操作."具体提示如下图所示: ...

  4. Android集成mupdf,实现手写笔签字,手指翻页的java代码

    import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.RectF; import ...

  5. Ubuntu子系统shell脚本自动连接xfce4界面

    脚本功能 命令行参数指定ip连接/获取ifconfig中的本地ip连接 修改.bashrc #!/bin/bash net_dev="wifi0" #默认的设备名 FALSE=&q ...

  6. react native 音频播放 react-native-sound

    先放一个效果图:该图实现的效果,点击播放按钮,进度条随着时间移动,点击暂停按钮,进度条停止移动 第一步,用到什么库 1.zmxv/react-native-sound 播放声音的库   2.calls ...

  7. react native 布局问题

    1. Text组件里面的 文字垂直居中 <Text style={styles.confirmButtonStyle}>确认</Text> confirmButtonStyle ...

  8. 无锡哲讯谈食品行业如何利用SAP信息化方案实现数字化转型?

    随着人们对生活品质的提高,大家对食品安全问题越来越重视.食品企业如果缺乏相应的监管和追溯,很容易陷入困难的被动局面.SAP系统可以对食品加工企业供应链.生产销售.食品质量控制等环节的信息化管控,降低食 ...

  9. Gitblit的windows安装(java编写)

    准备工作: 1.jdk(大于等于1.8版本)2.GitBlit压缩包:jdk下载地址:https://www.java.com/zh-CN/Gitblit下载地址:http://www.gitblit ...

  10. 自动化:web网页理解

    一.网页成分 网页由以下三部分组成 HTML: HTML 教程 (w3school.com.cn) 1.标记语言,网页的主体,不会变化 2.只会提示,不会报错 CSS: 1.标记语言,用来修饰HTML ...