(一)Liner Layers线性层

b 是偏移量bias

代码输入:

import torch
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader 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.linear1 = Linear(196608, 10) def forward(self, input):
output = self.linear1(input)
return output tudui = Tudui() for data in dataloader:
imgs, target = data
print(imgs.shape)
output = torch.reshape(imgs, (1, 1, 1, -1))
print(output.shape)
output = tudui(output)
print(output.shape)

输出:

torch.Size([64, 3, 32, 32])
torch.Size([1, 1, 1, 196608])
torch.Size([1, 1, 1, 10])

改为 flatten 类似“平铺”:

import torch
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoader 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.linear1 = Linear(196608, 10) def forward(self, input):
output = self.linear1(input)
return output tudui = Tudui() for data in dataloader:
imgs, target = data
print(imgs.shape)
# flatten
output = torch.flatten(imgs)
print(output.shape)

输出:

torch.Size([64, 3, 32, 32])
torch.Size([196608])

图形图像方面Module:

pytorch学习笔记(7)--线性层的更多相关文章

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

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

  2. [PyTorch 学习笔记] 3.2 卷积层

    本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson3/nn_layers_convolution.py 这篇文 ...

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

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

  4. [PyTorch 学习笔记] 4.1 权值初始化

    本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson4/grad_vanish_explod.py 在搭建好网络 ...

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

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

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

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

  7. TensorFlow 深度学习笔记 从线性分类器到深度神经网络

    转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Issue区讨论 官方教程地址 视频/字幕下载 L ...

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

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

  9. Pytorch学习笔记(一)---- 基础语法

    书上内容太多太杂,看完容易忘记,特此记录方便日后查看,所有基础语法以代码形式呈现,代码和注释均来源与书本和案例的整理. # -*- coding: utf-8 -*- # All codes and ...

  10. 学习笔记TF014:卷积层、激活函数、池化层、归一化层、高级层

    CNN神经网络架构至少包含一个卷积层 (tf.nn.conv2d).单层CNN检测边缘.图像识别分类,使用不同层类型支持卷积层,减少过拟合,加速训练过程,降低内存占用率. TensorFlow加速所有 ...

随机推荐

  1. mysqldump备份命令使用参数

    参数 参数说明 导出全部数据库. mysqldump -uroot -p --all-databases 导出全部表空间. mysqldump -uroot -p --all-databases -- ...

  2. C#-out和ref 参数修饰符

    参数修饰符: 无参数修饰符:如果一个参数没有任何参数修饰符修饰,那么认为它是值传递,意味着方法内部收到的是实参数据的副本 out:输出参数由方法内部进行赋值,(引用传递),如果方法内部没有给被out修 ...

  3. 微积分 I 笔记

    1.1 集合 这一节复习了高中关于集合的基础知识 介绍了一些新的概念 笛卡尔积 (Cartesian Product) 集合 \(X\) 与 \(Y\) 的笛卡尔积 (直积) \(X \times Y ...

  4. Linux下aria2详细配置,以及接管浏览器下载项

    本篇是在Deepin的环境下操作,大概可适用与Ubuntu和Debian,其他系的Linux不确定, 首先,我们安装aria2,使用命令行(在桌面右键可以打开)输入sudo apt install a ...

  5. 2023-03-03 js map 双重嵌套

    恩..其实也没啥要记录的,记住关键一点就是必须要有return,不管是几重,比如: arr.map((item, index) => {     return (         item.ar ...

  6. Object.assign() 方法浅析

    Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. const target = { a: 1, b: 2 ,c:3,e:6}; cons ...

  7. Python学习笔记(五)if分支语句

    一.if语法 示例: 1 money = int(input('请输入余额:')) 2 if money >= 5: 3 print('买得起!') 4 5 if True: 6 print(' ...

  8. linux下进程通信总结

    信号: 信号是通知发生了某种事件的机制,内核和进程都可能会向进程发送各种信号,进程也可以向自己发送信号.系统定义了一组标准信号类型,每种信号都拥有唯一的数值和用途.典型的信号递送是异步的,意味着进程可 ...

  9. linux下influx客户端使用

    influxdb-client 通过 sudo apt-cache search influx 找到了一个客户端工具 influxdb-client - command line interface ...

  10. iOS 过滤字符串

    //表示去掉字符串中的/符号 sysdate:[[self Gettime:strbegindate] stringByReplacingOccurrencesOfString:@"/&qu ...