(一)神经网络的骨架 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. linux下启动rabbitmq,redis,nginx

    这只是其中一种启动方法,也是我自己安装好后试过多次可以用的, 1,启动rabbitmq rabbitmqctl start_app rabbitmq程序端口是5672,可视化界面入口端口是15672, ...

  2. aqueduct “Uncaught error Bad state: No element" 或者 "NoSuchMethodError: The getter 'location' was called on null.”

    可以先将Pub\Cache目录下的内容清空(移除aqueduct命令),随后重新运行命令生成 pub global activate aqueduct  设置环境变量 创建用户变量 PUB_HOSTE ...

  3. ubuntu fastdds安装

    0,虚拟机安装: 推荐安装ubuntu 20.ubuntu 22与VMware不兼容,存在无法与主机之间复制粘贴文件的问题. 1.虚拟机镜像下载,下载tdesktop版本: 中科大站点:https:/ ...

  4. Go组件库总结之介入式链表

    本篇文章我们用Go封装一个介入式的双向链表,目的是将链表的实现和具体元素解耦.文章参考自:https://github.com/brewlin/net-protocol 1.元素的接口 type El ...

  5. IO学习笔记7

    2.4 多路复用javaAPI 在上面我们简单java代码实现了多路复用,是一个单线程版的.讲上面的epoll代码复制到linux服务器中,使用strace追踪系统调用. javaAPI会根据系统类型 ...

  6. SpringBoot为什么这么火?

    1.  总的设计原则是""默认大于配置"" 2. Starter机制,开箱即用,默认的配置和依赖都是默认加载的 3. SpringBoot是Spring的子类, ...

  7. selenium执行下载多个文件操作,谷歌浏览器弹出"xxx想要下载多个文件"的处理方法

    背景:   使用selenium框架,批量下载多个目录的不同文件,而下载多个文件时,浏览器会弹出如下窗口 解决方案有2个:1.代码定位到元素并点击[允许].2.修改浏览器的设置,使其能够拥有自动下载的 ...

  8. 戴尔n4110在win7下无法使用virtualbox的解决方法(应该对win7都有用)

    正文 因为已经学了一段时间的汇编了嘛,想着就拿单独一台机器出来学汇编好了,刚好趁着天气降温回学校拿被子的机会把笔记本也拿出来了,然后我装上了virtual box,把编译好的文件写到虚拟盘中,打开就直 ...

  9. UI动画练习 - CABasicAnimation:视图绕X/Y轴旋转(3D效果)

    视图 3D 旋转 1 - 代码示例:以绕 X 轴旋转为例 1 #import "ViewController.h" 2 @interface ViewController () 3 ...

  10. Intel oneAPI 环境变量设置

    因工作需要,需要在linux系统配置多个不同环境的库,需要使用environment-modules工具管理环境变量,为保持配置方法的一致性,也使用modulefile文件加载Intel oneAPI ...