1.感知器
pytorch中实现一个最基本的感知器
import torch
import torch.nn as nn
class Perceptron(nn.Module):
""" A Perceptron is one Linear layer """
def __init__(self, input_dim):
"""
Args:
input_dim (int): size of the input features
"""
super(Perceptron, self).__init__()
self.fc1 = nn.Linear(input_dim, 1)
def forward(self, x_in):
"""The forward pass of the Perceptron
Args:
x_in (torch.Tensor): an input data tensor.
x_in.shape should be (batch, num_features)
Returns:
the resulting tensor. tensor.shape should be (batch,)
"""
return torch.sigmoid(self.fc1(x_in)).squeeze()
2.激活函数
(1)sigmoid激活函数
import torch
import matplotlib.pyplot as plt
x = torch.range(-5., 5., 0.1)
y = torch.sigmoid(x)
plt.plot(x.numpy(), y.numpy())
plt.show()
sigmoid 函数可能导致消失梯度问题和爆炸梯度问题。因此,在神经网络中,除了在输出端使用 sigmoid 单元外,很少看到其他使用 sigmoid 单元的情况。
(2)tanh激活函数
import torch
import matplotlib.pyplot as plt
x = torch.range(-5., 5., 0.1)
y = torch.tanh(x)
plt.plot(x.numpy(), y.numpy())
plt.show()
(3)ReLU激活函数
import torch
import matplotlib.pyplot as plt
relu = torch.nn.ReLU()
x = torch.range(-5., 5., 0.1)
y = relu(x)
plt.plot(x.numpy(), y.numpy())
plt.show()
PReLU(ReLU)函数的一个变体
import torch
import matplotlib.pyplot as plt
prelu = torch.nn.PReLU(num_parameters=1)
x = torch.range(-5., 5., 0.1)
y = prelu(x)
plt.plot(x.numpy(), y.numpy())
plt.show()
(4)Softmax函数
Input[0]
import torch.nn as nn
import torch
softmax = nn.Softmax(dim=1)
x_input = torch.randn(1, 3)
y_output = softmax(x_input)
print(x_input)
print(y_output)
print(torch.sum(y_output, dim=1))
Output[0]
tensor([[ 0.5836, -1.3749, -1.1229]])
tensor([[ 0.7561, 0.1067, 0.1372]])
tensor([ 1.])
Softmax函数结果取值为1
3. 损失函数
(1)均方误差损失
回归问题中常用均方误差(MSE)
Input[0]
import torch
import torch.nn as nn
mse_loss = nn.MSELoss()
outputs = torch.randn(3, 5, requires_grad=True)
targets = torch.randn(3, 5)
loss = mse_loss(outputs, targets)
print(loss)
Output[0]
tensor(3.8618)
(2)分类交叉熵损失
多分类问题中常用分类交叉熵
Input[0]
import torch
import torch.nn as nn
ce_loss = nn.CrossEntropyLoss()
outputs = torch.randn(3, 5, requires_grad=True)
targets = torch.tensor([1, 0, 3], dtype=torch.int64)
loss = ce_loss(outputs, targets)
print(loss)
Output[0]
tensor(2.7256)
(3)二元交叉熵损失
二分类问题损失函数常用二元交叉熵
Input[0]
bce_loss = nn.BCELoss()
sigmoid = nn.Sigmoid()
probabilities = sigmoid(torch.randn(4, 1, requires_grad=True))
targets = torch.tensor([1, 0, 1, 0], dtype=torch.float32).view(4, 1)
loss = bce_loss(probabilities, targets)
print(probabilities)
print(loss)
Output[0]
tensor([[ 0.1625],
[ 0.5546],
[ 0.6596],
[ 0.4284]])
tensor(0.9003)
4.优化器
PyTorch 库为优化器提供了几种选择。随机梯度下降法(SGD)是一种经典的选择算法,但对于复杂的优化问题,SGD 存在收敛性问题,往往导致模型较差。当前首选的替代方案是自适应优化器,例如 Adagrad 或 Adam,它们使用关于更新的信息。
(1)Adam优化器
Input[0]
import torch.nn as nn
import torch.optim as optim
input_dim = 2
lr = 0.001
perceptron = Perceptron(input_dim=input_dim)
bce_loss = nn.BCELoss()
optimizer = optim.Adam(params=perceptron.parameters(), lr=lr)