在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中實現各種激活函數。