使用pytorch实现SVM
时间: 2023-12-09 11:04:10 浏览: 158
实现SVM的步骤如下:
1. 导入必要的库
```python
import torch
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.preprocessing StandardScaler
from sklearn.model_selection import train_test
```
2. 生成数据集
```python
X, y = make_blobs(n_samples=100, centers=2, random_state=0)
X = StandardScaler().fit_transform(X)
y = np.where(y == 0, -1, 1)
```
3. 划分数据集
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
```
4. 定义SVM模型
```python
class SVM(torch.nn.Module):
def __init__(self, input_dim):
super(SVM, self).__init__()
self.linear = torch.nn.Linear(input_dim, 1)
def forward(self, x):
return self.linear(x)
```
5. 初始化模型和优化器
```python
model = SVM(X.shape[1])
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
```
6. 训练模型
```python
epochs = 200
for epoch in range(epochs):
epoch_loss = 0
for i in range(X_train.shape[0]):
optimizer.zero_grad()
xi = torch.FloatTensor(X_train[i])
yi = torch.FloatTensor([y_train[i]])
output = yi * model(xi)
loss = torch.max(torch.zeros(1), 1 - output)
epoch_loss += loss
loss.backward()
optimizer.step()
print('Epoch {}, Loss {}'.format(epoch, epoch_loss))
```
7. 测试模型
```python
correct = 0
for i in range(X_test.shape[0]):
xi = torch.FloatTensor(X_test[i])
yi = torch.FloatTensor([y_test[i]])
output = yi * model(xi)
prediction = torch.sign(output).item()
if prediction == y_test[i]:
correct += 1
accuracy = correct / X_test.shape[0]
print('Accuracy:', accuracy)
```
完整代码如下:
```python
import torch
import numpy as np
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
X, y = make_blobs(n_samples=100, centers=2, random_state=0)
X = StandardScaler().fit_transform(X)
y = np.where(y == 0, -1, 1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
class SVM(torch.nn.Module):
def __init__(self, input_dim):
super(SVM, self).__init__()
self.linear = torch.nn.Linear(input_dim, 1)
def forward(self, x):
return self.linear(x)
model = SVM(X.shape[1])
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
epochs = 200
for epoch in range(epochs):
epoch_loss = 0
for i in range(X_train.shape[0]):
optimizer.zero_grad()
xi = torch.FloatTensor(X_train[i])
yi = torch.FloatTensor([y_train[i]])
output = yi * model(xi)
loss = torch.max(torch.zeros(1), 1 - output)
epoch_loss += loss
loss.backward()
optimizer.step()
print('Epoch {}, Loss {}'.format(epoch, epoch_loss))
correct = 0
for i in range(X_test.shape[0]):
xi = torch.FloatTensor(X_test[i])
yi = torch.FloatTensor([y_test[i]])
output = yi * model(xi)
prediction = torch.sign(output).item()
if prediction == y_test[i]:
correct += 1
accuracy = correct / X_test.shape[0]
print('Accuracy:', accuracy)
```
阅读全文