(一)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. 在pycharm中安装虚拟环境

    1.安装python解释器 2.安装pytorch (1)查看当前虚拟环境 conda env list (2)进入自己的虚拟环境 source activate SCI(自己的虚拟环境名字) (3) ...

  2. locust自定义负载策略。

    1.时间峰值策略 每运行一分钟启动100个用户,总运行时间为10分钟 class CustomShape(LoadTestShape): # 设置时限 time_limit = 600 # 设置产生率 ...

  3. sql执行多条语句

    问题背景: 今天想在xml里面写一个sql,执行批量修改, update question_rules set score=${rule.score}, data_describe=#{rule.da ...

  4. 图片上传插件WebUploader的坑

    需要上传图片的一个操作选择了WebUploader这个插件来实现,具体的实现过程如下. js代码: 1.引入js.css文件: <script type="text/javascrip ...

  5. Windows10 WSL开启SSH登录

    1.wsl 安装ssh服务(使用的是ubuntu)sudo apt install openssh-server 2.修改配置文件sudo vim /etc/ssh/sshd_config关键的几处修 ...

  6. ubuntu16.04编译LAPACK3.7.1出错

    NEP: Testing Nonsymmetric Eigenvalue Problem routines ./EIG/xeigtstz < nep.in > znep.out 2> ...

  7. 复杂SQL语句及其优化

    一,复杂SQL语句类型 1 ,笛卡尔连接 题目1:找出工资超过各自经理的员工姓名 表:employee(id , name , depid , salary, manager_id ) SELECT ...

  8. Python学习笔记(一)环境确认

    1.安装环境 1)python解释器 版本3.10.2 安装完毕后在命令提示符窗口中输入python显示版本信息 2)开发工具 pycharm 2021.1.3 2.新建项目 创建项目后修改解释器配置 ...

  9. python 嵌套对象转为dict

    as_dict(self, keys=None, exclude_keys=None): """ 将ORM对象序列化为字典 :param keys: :return: & ...

  10. python peewee

    from peewee import MySQLDatabase, Model from peewee import CharField, IntegerField, BooleanField # 引 ...