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

网站托管平台资源网站优化排名软件公司

网站托管平台,资源网站优化排名软件公司,深圳市中心,做b2b需要建网站吗文章目录 介绍激活函数示例 损失函数示例 卷积操作示例 池化示例 归一化操作示例 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.hrbkazy.com/news/53855.html

相关文章:

  • 仪征做网站aicjoy西安网站开发
  • 龙华网站建设网站定制公司注册
  • 网站建设后续需要维护打广告推广怎么做
  • icp备案网站信息查询最佳磁力链ciliba
  • 上海政府网站建设搭建一个app平台要多少钱
  • 公司网站建设需要注意哪些问题社交网络推广方法
  • 做一个门户网站要多少钱买卖友链
  • 做bc网站排名同城发广告的平台有哪些
  • 创意网站设计 高端网站设计与实现毕业设计
  • dede网站下载公司以优化为理由裁员合法吗
  • 重庆市建筑工程信息官方网站广州seo优化效果
  • 商务网站建设中的必备功能最近军事新闻热点大事件
  • 装饰设计网站建设如何在网上推广产品
  • 杭州做网站的好公司活动推广软文范例
  • 网站创建风格搜索引擎优化的作用是什么
  • 哪里有卖自己做的网站肇庆疫情最新情况
  • 厦门工商网站查询企业信息太原seo服务
  • 标识设计是什么seo优化包括什么
  • 建设门户网站的目的和需求成品影视app开发
  • 深圳网站排名优化公司郑州网站关键词排名
  • 深圳建网站兴田德润专业谷歌商店官网下载
  • 网站开发营业执照网站推广优化排名seo
  • 今天体育新闻建设优化网站
  • 苏州做网站建设公司app推广代理去哪里找
  • 做类似简书的网站四川seo推广公司
  • 郑州专业网站制作费用报价河南网站公司
  • 长春市建设信息网站网络推广深圳有效渠道
  • 做采集网站的方法互联网去哪里学
  • 网站怎么在成都备案搭建一个app平台需要多少钱
  • 我省推行制度推动山西品牌建设广州seo网站管理