温馨提示×

PyTorch中怎么实现激活函数

小亿
96
2024-05-10 19:09:58
栏目: 深度学习
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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中实现各种激活函数。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:pytorch调参激活函数怎么调

0