PyTorch中怎么实现激活函数

1949idc 2年前 (2024-09-18) 阅读数 327 #深度学习

在PyTorch中,可以使用torch.nn中的模块来实现各种激活函数。以下是几种常见的激活函数的示例代码:

  1. ReLU激活函数:
import torch
import torch.nn as nn

input = torch.randn(1, 10)
relu = nn.ReLU()
output = relu(input)
  1. Sigmoid激活函数:
import torch
import torch.nn as nn

input = torch.randn(1, 10)
sigmoid = nn.Sigmoid()
output = sigmoid(input)
  1. Tanh激活函数:
import torch
import torch.nn as nn

input = torch.randn(1, 10)
tanh = nn.Tanh()
output = tanh(input)
  1. Softmax激活函数:
import torch
import torch.nn as nn

input = torch.randn(1, 10)
softmax = nn.Softmax(dim=1)
output = softmax(input)

使用这些代码,可以很方便地在PyTorch中实现各种激活函数。

版权声明

本文内容由互联网用户自发贡献,该文观点仅代表作者本人
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

© 2010 首途云安 & 厦门硕顿信息技术有限公司 & 闽ICP备11016866号  增值电信业务经营许可证:B1-20203020 地址:福建厦门思明区嘉禾路297号1806
高新技术企业
软件产品证书
计算机软件著作权
ISO认证
国家3A企业