支持向量机(SVM, Support Vector Machines)是一种广泛应用于分类、回归、甚至是异常检测的监督学习算法。自从Vapnik和Chervonenkis在1995年首次提出,SVM算法就在机器学习领域赢得了巨大的声誉。这部分因为其基于几何和统计理论的坚实数学基础,也因为其在实际应用中展示出的出色性能。
在这一部分中,我们将使用Python和PyTorch库来实现一个基础的支持向量机(SVM)。我们会遵循以下几个主要步骤:
- import torch
-
- # 创建训练数据和标签
- X_train = torch.FloatTensor([[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3]])
- y_train = torch.FloatTensor([1, 1, 1, -1, -1, -1])
-
- # 创建测试数据
- X_test = torch.FloatTensor([[1, 0.5], [2, 0.5]])
模型定义
- class LinearSVM(torch.nn.Module):
- def __init__(self):
- super(LinearSVM, self).__init__()
- self.weight = torch.nn.Parameter(torch.rand(2), requires_grad=True)
- self.bias = torch.nn.Parameter(torch.rand(1), requires_grad=True)
-
- def forward(self, x):
- return torch.matmul(x, self.weight) + self.bias
- # 实例化模型和优化器
- model = LinearSVM()
- optimizer = torch.optim.SGD([model.weight, model.bias], lr=0.01)
- # 设置训练轮次和正则化参数C
- epochs = 100
- C = 0.1
-
- for epoch in range(epochs):
- for i, x in enumerate(X_train):
- y = y_train[i]
- optimizer.zero_grad()
-
- # 计算间隔损失 hinge loss: max(0, 1 - y*(wx + b))
- loss = torch.max(torch.tensor(0), 1 - y * model(x))
-
- # 添加正则化项: C * ||w||^2
- loss += C * torch.norm(model.weight)**2
-
- loss.backward()
- optimizer.step()
- with torch.no_grad():
- for x in X_test:
- prediction = model(x)
- print(f"Prediction for {x} is: {prediction}")