pytorch学习笔记(5)--神经网络
(一)神经网络的骨架 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)--神经网络的更多相关文章
- CNN学习笔记:神经网络表示
CNN学习笔记:神经网络表示 双层神经网络模型 在一个神经网络中,当你使用监督学习训练它的时候,训练集包含了输入x还有目标输出y.隐藏层的含义是,在训练集中,这些中间节点的真正数值,我们是不知道的,即 ...
- 【pytorch】pytorch学习笔记(一)
原文地址:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 什么是pytorch? pytorch是一个基于p ...
- TensorFlow学习笔记——深层神经网络的整理
维基百科对深度学习的精确定义为“一类通过多层非线性变换对高复杂性数据建模算法的合集”.因为深层神经网络是实现“多层非线性变换”最常用的一种方法,所以在实际中可以认为深度学习就是深度神经网络的代名词.从 ...
- Pytorch学习笔记(二)---- 神经网络搭建
记录如何用Pytorch搭建LeNet-5,大体步骤包括:网络的搭建->前向传播->定义Loss和Optimizer->训练 # -*- coding: utf-8 -*- # Al ...
- PyTorch学习笔记6--案例2:PyTorch神经网络(MNIST CNN)
上一节中,我们使用autograd的包来定义模型并求导.本节中,我们将使用torch.nn包来构建神经网络. 一个nn.Module包含各个层和一个forward(input)方法,该方法返回outp ...
- 莫烦pytorch学习笔记(八)——卷积神经网络(手写数字识别实现)
莫烦视频网址 这个代码实现了预测和可视化 import os # third-party library import torch import torch.nn as nn import torch ...
- 【深度学习】Pytorch 学习笔记
目录 Pytorch Leture 05: Linear Rregression in the Pytorch Way Logistic Regression 逻辑回归 - 二分类 Lecture07 ...
- Pytorch学习笔记(一)——简介
一.Tensor Tensor是Pytorch中重要的数据结构,可以认为是一个高维数组.Tensor可以是一个标量.一维数组(向量).二维数组(矩阵)或者高维数组等.Tensor和numpy的ndar ...
- [PyTorch 学习笔记] 3.1 模型创建步骤与 nn.Module
本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson3/module_containers.py 这篇文章来看下 ...
- [PyTorch 学习笔记] 3.3 池化层、线性层和激活函数层
本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson3/nn_layers_others.py 这篇文章主要介绍 ...
随机推荐
- 创建异步倒计时触发Task
https://www.cnblogs.com/shanfeng1000/p/13402152.html //Task关闭 CancellationTokenSource cancel = new C ...
- jmeter接口自动化-读取CSV文件执行测试用例
一.在csv文件中编写好用例 首先在csv文件首行填写相关参数并编写测试用例.脚本可通过优先级参数控制执行哪些接口,通过请求方式执行不同端口下的接口,再读取csv文件时进行参数化. 二.设计测试脚本并 ...
- 简单的关键词查找实验(基于C语言)
准备 书名数据库的阵列表示 关键字 书籍 B1 B2 B3 B4 B5 B6 B7 algebra 1 1 1 1 1 1 0 application 1 0 1 1 1 1 0 elementary ...
- [localhost-startStop-1]
第一次遇到Tomcat在Linux服务器启动卡住的情况,情况很简单,tomcat启动以后卡在INFO: Deploying web application directory ......这句话,具体 ...
- C#实现不用随机函数(Random)的洗牌算法
代码不多,先看效果: 类代码: 1 static class ShuffleCards 2 { 3 private static int lastHash = 0; 4 public static v ...
- System.Diagnostics.Process.Start(); 用法详解
来源:https://news.68idc.cn/buildlang/ask/20150104156981.html 实例代码:http://www.cppcns.com/ruanjian/cshar ...
- Linux上的I2C基础知识
Linux上的I2C基础知识 什么是I2C I2C(Inter-Integrated Circuit,eye-squared-C),也称为 I2C 或 IIC,是一种同步.多控制器/多目标(主/从). ...
- IMX6Ull驱动
mount -t nfs -o nolock,vers=3 192.168.1.117:/home/book/nfs_rootfs /mnt cat /proc/sys/kernel/printk e ...
- 【未完】【DDR系列文章收集】
资料来源 1.https://zhuanlan.zhihu.com/p/343262874 (1)主要讲DRAM刷新的内容: 为什么需要刷新(漏电流导致电容电荷的流失)? 刷新的本质(对存储数据的电容 ...
- 【git】7.5 git工具-搜索
资料来源: (1) https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E6%90%9C%E7%B4%A2 1.git grep 注1:使用g ...