(一)神经网络的骨架 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. react 二级路由嵌套

    嵌套路由之后,静态文静路径错误, 更改webpack  打包output 输出根目录,publicPath:'/',二级路由刷新之后白屏,在首页模板文件中路径前加   /,

  2. Libusb测试USB device(2)

    因为测试的比较混乱,我就按照我的问题导向来描述我的过程吧 一. 提示libusb_context对象为NULL的错误: 在简化的过程中,我没有加入dev_handler判断为空就进行了具体的工作: 1 ...

  3. 记录multipartFile表单类型转化为file

    导入依赖 <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</a ...

  4. 10,java双向链表基础代码复现

    双向链表总体来说和单链表差别不大,最大的区别就是node结构中多了一个pre指针(变量)指向前一个节点,因为有了之前的单链表基础,双向链表的复现问题少了很多,基本可以不参考老师的代码自主写下来. 1. ...

  5. Ubuntu 复制粘贴快捷键

    打开命令行terminal ctrl+alt+t 复制粘贴 ctrl+shift+c ctrl+shift+v /usr/share/applications/ 这个文件夹可以创建桌面快捷方式 换源 ...

  6. 推荐优秀国产蓝牙芯片-HS6621CxC系列

    HS6621CxC是一个优化功耗真正芯片系统(SOC)解决方案,适用于蓝牙低功耗和私有的2.4GHz应用场景.它集成了一个高性能.小功率的射频收发器,具有蓝牙基带和丰富的外围IO扩展. HS6621C ...

  7. 14-K8S之helm入门到逃跑

    目录 helm入门 1.helm介绍 2.helm核心术语 3.helm下载和安装 3.1以helm3.6为测试实例 3.2以helm3.7.2为例 helm v2版本在集群上部署Tiller 1.创 ...

  8. MySQL时区的问题

    我这里是在application.properties文件中配置的MySQL连接信息. 开始时间显示不征程是因为没有配置时区,后来加上了setTimeZone=Asia/Shanghai,时间显示正常 ...

  9. GrADS 读取NetCDF和HDF的ctl文件 SDF文件的描述文件

    翻译自http://cola.gmu.edu/grads/gadoc/SDFdescriptorfile.html 使用GrADS阅读NetCDF和HDF文件 NetCDF和HDF格式的文件被称作自描 ...

  10. (K8s学习笔记五)Pod的使用详解

    1.Pod用法 K8s里使用的容器不能使用启动命令是后台执行程序,如:nohup ./start.sh &,该脚本运行完成后kubelet会认为该Pod执行结束,将立刻销毁该Pod,如果该Po ...