-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path243-cnn_classification.py
More file actions
76 lines (58 loc) · 1.63 KB
/
243-cnn_classification.py
File metadata and controls
76 lines (58 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Pytorch深度学习实现分类算法(Classification)
# 生成随机数据集
from matplotlib import pyplot as plt
from sklearn.datasets._samples_generator import make_blobs
x, y = make_blobs(200, 2, centers=[[2, 3], [6, 8]])
# plt.scatter(x[:, 0], x[:, 1])
# plt.show()
# 建立神经网络
import torch
import torch.nn as nn
class CModule(nn.Module):
def __init__(self):
super().__init__()
self.linear1 = nn.Linear(2, 32)
self.relu = nn.ReLU()
self.linear2 = nn.Linear(32, 4)
def forward(self, x):
x = self.linear1(x)
x = self.relu(x)
x = self.linear2(x)
return x
cmodule = CModule()
# print(cmodule)
# 模型训练
x, y = make_blobs(200, 2, centers=4)
x = torch.FloatTensor(x)
y = torch.tensor(y)
for i in range(10):
x = torch.tensor(x).type(torch.FloatTensor)
p_y = cmodule(x)
print(p_y)
exit()
# # 优化器
optimizer = torch.optim.Adam(cmodule.parameters(), lr=0.02)
# 损失函数
loss_fn = torch.nn.CrossEntropyLoss()
# for i in range(1000):
# x = torch.tensor(x).type(torch.FloatTensor)
# p_y = cmodule(x)
# y = torch.tensor(y).long()
# loss = loss_fn(p_y, y)
# 可视化预测结果
ax = plt.axes()
plt.ion()
plt.show()
for i in range(1000):
p_y = cmodule(x)
loss = loss_fn(p_y, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 准确率
p_y = torch.argmax(p_y, 1).data.numpy()
accuracy = (p_y == y.data.numpy()).sum() / y.size()[0]
plt.cla()
plt.scatter(x[:, 0], x[:, 1], c=p_y)
plt.text(0.75, 0.05 , 'accuracy:' + str(accuracy), transform=ax.transAxes)
plt.pause(0.3)