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

网站文案案例站长统计性宝app

网站文案案例,站长统计性宝app,黄页88可信吗,南京做网站公司地点文章目录 介绍激活函数示例 损失函数示例 卷积操作示例 池化示例 归一化操作示例 Dropout示例 torch.nn.functional 与 torch.nn 的区别 介绍 torch.nn.functional 是 PyTorch 中的一个模块,提供了许多函数式的神经网络操作,包括激活函数、损失函数、卷…

文章目录

  • 介绍
  • 激活函数
    • 示例
  • 损失函数
    • 示例
  • 卷积操作
    • 示例
  • 池化
    • 示例
  • 归一化操作
    • 示例
  • 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.khdw.cn/news/54855.html

相关文章:

  • 在国税网站更换购票员怎么做吉林seo基础
  • 网站建设网络推广图片成功的软文推广
  • b站网站开发者调试用具网络销售靠谱吗
  • 侯马市网站建设公司百度指数数据分析
  • 自己建网站百度到吗如何在百度上推广自己
  • ag娱乐建设网站搭建网站要多少钱
  • 衡水景县专业做淘宝网站公司如何优化关键词搜索
  • 那种漂亮的网站怎么做的一个品牌的策划方案
  • 做外单阿里的网站企业品牌推广网站
  • 网上电影网站怎么做的日本网络ip地址域名
  • 做网站需要会什么 知乎友情链接购买网站
  • 我想建立个网站数据怎么办百度客服24小时电话
  • 上海建网站工作室网络优化有前途吗
  • 在互易上做的网站如何修改商城推广
  • 上海青浦做网站公司请简述网络营销的特点
  • 做网站挂谷歌广告赚钱吗贵阳网站建设制作
  • 网页设计与网站建设期末考试试卷网络推广公司方案
  • 购物返利网站怎么做网站百度收录突然消失了
  • 绍兴cms建站模板代理怎么引流推广
  • 公司做网站的费用计什么科目优化电池充电什么意思
  • 泗洪网页设计seo实战密码
  • 套餐型网站建设合同事件营销的概念
  • 番禺做网站系统公司软文推广
  • 网站服务器选购近期重大新闻
  • wordpress关注插件网页优化包括什么
  • 沈阳网站建设发布网站怎么优化seo
  • 公安机关网站备案流程图项目推广计划书
  • 怎么样自己做网站营销策略有哪几种
  • 营销型网站建设实战公司怎么在百度上推广
  • 侦探公司做网站的资料的网站建设