当前位置: 首页 > news >正文

神经网络 torch.nn---Linear Layers(nn.Linear)

torch.nn - PyTorch中文文档 (pytorch-cn.readthedocs.io)

torch.nn — PyTorch 2.3 documentation

nn.Linear

torch.nn.Linear(in_featuresout_featuresbias=Truedevice=Nonedtype=None)

参数:

  • in_features - 每个输入样本的大小
  • out_features - 每个输出样本的大小
  • bias - 若设置为False,这层不会学习偏置。默认值:True

形状:

  • 输入: (N,in_features)(N , in_features)
  • 输出: (N,out_features)(N , out_features)

变量:

  • weight -形状为(out_features x in_features)的模块中可学习的权值
  • bias -形状为(out_features)的模块中可学习的偏置

计算公式:

代码实例讲解

import torch
import torchvision
from torch import nn
from torch.nn import Linear
from torch.utils.data import DataLoaderdataset = torchvision.datasets.CIFAR10(root='./dataset', train=False, transform=torchvision.transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset, batch_size=64,drop_last=True)
# shuffle 是否打乱   False不打乱
# drop_last 最后一轮数据不够时,是否舍弃 true舍弃
class Tudui(nn.Module):def __init__(self):super(Tudui, self).__init__()self.linear1 = Linear(196608, 10)def forward(self, x):output = self.linear1(x)return outputtudui = Tudui()for data in dataloader:imgs, targets = dataprint(imgs.shape)  #torch.Size([16, 3, 32, 32])output= torch.flatten(imgs)# output = torch.reshape(imgs,(1, 1, 1, -1))print(output.shape) #torch.Size([1, 1, 1, 196608])output = tudui.forward(output)print(output.shape)

部分输出结果:

 

torch.flatten() 和torch.reshape() 

output= torch.flatten(imgs)
output = torch.reshape(imgs,(1, 1, 1, -1))

以上两行代码都是将图像展开成一行

  • torch.flatten() 和torch.reshape() 的区别:

    • torch.flatten更方便,可以直接把图像变成一行

    • torch.reshape功能更强大,可任意指定图像尺寸

http://www.lryc.cn/news/365258.html

相关文章:

  • PPT视频如何16倍速或者加速播放
  • 【ai】DeepStream 简介
  • 如何学习使用淘宝API?淘宝API运营场景
  • Java 面试题:Java 的动态代理是基于什么原理?
  • Python logging 模块详解
  • http://account.battlenet.com.cn
  • java第二十课 —— 面向对象习题
  • Flask的模块化实践
  • 锁存器(Latch)的产生与特点
  • 搜维尔科技:「案例」Faceware电影中面部动画的演变历程
  • 特征工程技巧—Bert
  • 更改 Docker 的默认存储位置
  • 搜索与图论:图中点的层次
  • NLP入门——数据预处理:编码规范化
  • 代码随想录算法训练营第四十八天| 70. 爬楼梯 (进阶)、322. 零钱兑换、279.完全平方数
  • c++11 constexpr关键字
  • ios 获取图片的一部分区域
  • 数据结构(3)栈、队列、数组
  • 数据库(入门)
  • ChatTTS+Python编程搞定语音报时小程序
  • 【Mac】Alfred 5 for Mac(苹果效率提升工具)v5.5软件介绍及安装教程
  • PDF文件处理不再复杂:9个Python库让一切变得简单
  • 安防视频融合汇聚平台EasyCVR如何实现视频画面自定义标签?
  • Liunx音频
  • 2024前端面试准备3-JS异步-进阶
  • lm studio 0.2.24国内下载模型
  • 卷积池化尺寸计算公式
  • 前端框架原理自测题:根据 JSX / Vue 模板写出 render 函数 / VNode
  • RabbitMQ启动报错:Error during startup: {error, {schema_integrity_check_failed,
  • 操作系统入门系列-MIT6.828(操作系统工程)学习笔记(三)---- xv6初探与实验一(Lab: Xv6 and Unix utilities)