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

torch.nn.functional的用法

文章目录

  • 介绍
  • 激活函数
    • 示例
  • 损失函数
    • 示例
  • 卷积操作
    • 示例
  • 池化
    • 示例
  • 归一化操作
    • 示例
  • Dropout
    • 示例
  • torch.nn.functional 与 torch.nn 的区别

介绍

torch.nn.functional 是 PyTorch 中的一个模块,提供了许多函数式的神经网络操作,包括激活函数、损失函数、卷积操作等。这些函数是无状态的(stateless),与 torch.nn 中的模块化层(如 nn.ReLU、nn.Conv2d 等)不同,torch.nn.functional 提供的是直接的函数调用方式。

激活函数

torch.nn.functional 提供了许多常用的激活函数,例如 ReLU、Sigmoid、Tanh 等。

import torch.nn.functional as F

在这里插入图片描述

示例

import torch  
import torch.nn.functional as F  x = torch.tensor([-1.0, 0.0, 1.0])  
relu_output = F.relu(x)  # ReLU 激活  
softmax_output = F.softmax(x, dim=0)  # Softmax 激活  
print(relu_output)  # tensor([0., 0., 1.])  
print(softmax_output)  # tensor([0.0900, 0.2447, 0.6652])

损失函数

torch.nn.functional 提供了许多损失函数,与 torch.nn 中的模块化损失函数(如 nn.CrossEntropyLoss)功能相同,但需要显式传入参数。
在这里插入图片描述

示例

input = torch.tensor([[0.5, 1.5], [2.0, 1.0]], requires_grad=True)  
target = torch.tensor([1, 0])  
loss = F.cross_entropy(input, target)  # 交叉熵损失  
print(loss)  # tensor(1.2412, grad_fn=<NllLossBackward>)

卷积操作

torch.nn.functional 提供了卷积操作的函数式实现,例如 F.conv1d、F.conv2d、F.conv3d。
在这里插入图片描述

示例

input = torch.randn(1, 1, 5)  # 输入:batch_size=1, channels=1, width=5  
weight = torch.randn(1, 1, 3)  # 卷积核:out_channels=1, in_channels=1, kernel_size=3  
output = F.conv1d(input, weight)  
print(output.shape)  # torch.Size([1, 1, 3])

池化

torch.nn.functional 提供了池化操作的函数式实现,例如最大池化和平均池化。
在这里插入图片描述

示例

input = torch.tensor([[[[1.0, 2.0], [3.0, 4.0]]]])  # 输入:batch_size=1, channels=1, height=2, width=2  
output = F.max_pool2d(input, kernel_size=2)  
print(output)  # tensor([[[[4.]]]])

归一化操作

torch.nn.functional 提供了归一化操作的函数式实现,例如 BatchNorm、LayerNorm 等。
在这里插入图片描述

示例

input = torch.randn(2, 3)  # 输入:batch_size=2, features=3  
output = F.layer_norm(input, normalized_shape=(3,))  
print(output)

Dropout

torch.nn.functional 提供了 Dropout 的函数式实现。
在这里插入图片描述

示例

input = torch.tensor([1.0, 2.0, 3.0])  
output = F.dropout(input, p=0.5, training=True)  # 50% 概率随机置零  
print(output)

torch.nn.functional 与 torch.nn 的区别

在这里插入图片描述

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

相关文章:

  • 最新常见的图数据库对比,选型,架构,性能对比
  • UE5材质节点Camera Vector/Reflection Vector
  • NextCloud服务安装与配置教程
  • 详解GPT-信息抽取任务 (GPT-3 FAMILY LARGE LANGUAGE MODELS)
  • 华为数通考试模拟真题(附带答案解析)题库领取
  • 微信小程序:正确输出<小于,大于>符号
  • Flink源码解析之:如何根据算法生成StreamGraph过程
  • 矩阵简单问题(Java)
  • Elasticsearch DSL版
  • 2024-12-29-sklearn学习(26)模型选择与评估-交叉验证:评估估算器的表现 今夜偏知春气暖,虫声新透绿窗纱。
  • STM32CUBEIDE FreeRTOS操作教程(十二):std dynamic memory 标准动态内存
  • 异步爬虫之aiohttp的使用
  • 【Rust自学】9.1. 不可恢复的错误以及panic!
  • 【老张的程序人生】一天时间,我成软考高级系统分析师
  • vue使用el-select下拉框自定义复选框
  • k8s基础(2)—Kubernetes-Namespace
  • APM for Large Language Models
  • Spark Runtime Filter
  • AI大模型系列之七:Transformer架构讲解
  • 基于51单片机(STC12C5A60S2)和8X8彩色点阵屏(WS2812B驱动)的小游戏《贪吃蛇》(普中开发板矩阵按键控制)
  • 遇到复杂的 递归查询sql 需要oracle 转pgsql 可以把数据表结构给ai
  • Zynq PS端外设之GPIO
  • Spring Boot项目开发常见问题及解决方案(上)
  • Elasticsearch: 高级搜索
  • STM32 拓展 电源控制
  • SpringBootWeb案例-1
  • HTML——57. type和name属性
  • 应用架构模式-总体思路
  • vue 虚拟滚动 vue-virtual-scroller RecycleScroller
  • DC-DC 降压转换器设计提示和技巧