在PyTorch中,可以使用torch.nn中的模块来实现各种激活函数。以下是几种常见的激活函数的示例代码:
import torch
import torch.nn as nn
input = torch.randn(1, 10)
relu = nn.ReLU()
output = relu(input)
import torch
import torch.nn as nn
input = torch.randn(1, 10)
sigmoid = nn.Sigmoid()
output = sigmoid(input)
import torch
import torch.nn as nn
input = torch.randn(1, 10)
tanh = nn.Tanh()
output = tanh(input)
import torch
import torch.nn as nn
input = torch.randn(1, 10)
softmax = nn.Softmax(dim=1)
output = softmax(input)
使用这些代码,可以很方便地在PyTorch中实现各种激活函数。