-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
158 lines (99 loc) · 2.83 KB
/
train.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
# coding: utf-8
# In[12]:
from torchsummary import summary
import numpy as np
import pandas as pd
import seaborn as sns
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import Network as Net
import matplotlib.pyplot as plt
PATH = "trained_model"
# In[13]:
data_raw = pd.read_csv('train.csv', sep=",")
labels = data_raw['label']
data_raw.drop('label', axis=1, inplace=True)
valid_data_raw = pd.read_csv('validation.csv', sep=",")
valid_labels = valid_data_raw['label']
valid_data_raw.drop('label', axis=1, inplace=True)
# In[15]:
data = data_raw.values
labels = labels.values
valid_data = valid_data_raw.values
valid_labels = valid_labels.values
# In[16]:
pixels = data[10].reshape(50, 50)
plt.subplot(221)
sns.heatmap(data=pixels)
pixels = data[12].reshape(50, 50)
plt.subplot(222)
sns.heatmap(data=pixels)
pixels = data[20].reshape(50, 50)
plt.subplot(223)
sns.heatmap(data=pixels)
pixels = data[32].reshape(50, 50)
plt.subplot(224)
sns.heatmap(data=pixels)
# In[17]:
reshaped = []
for i in data:
reshaped.append(i.reshape(1, 50, 50))
data = np.array(reshaped)
# In[18]:
x = torch.FloatTensor(data)
# print(x.shape)
y = torch.LongTensor(labels.tolist())
reshaped = []
for i in valid_data:
reshaped.append(i.reshape(1, 50, 50))
valid_data_formatted = np.array(reshaped)
# In[18]:
valid_x = torch.FloatTensor(valid_data_formatted)
# print(x.shape)
valid_y = torch.LongTensor(valid_labels.tolist())
# In[20]:
device = torch.device("cpu")
#
# In[21]:
model = Net.Network().to(device)
summary(model, (1, 50, 50))
# In[22]:
net = Net.Network()
# optimizer = optim.SGD(net.parameters(), 1e-3, momentum=0.7)
optimizer = torch.optim.Adam(net.parameters(), lr=1e-4)
loss_func = nn.CrossEntropyLoss()
# In[23]:
loss_log = []
acc_log = []
loss_log_valid = []
for e in range(20):
for i in range(0, x.shape[0], 32):
x_mini = x[i:i + 32]
y_mini = y[i:i + 32]
optimizer.zero_grad()
net_out = net(Variable(x_mini))
loss = loss_func(net_out, Variable(y_mini))
loss.backward()
optimizer.step()
if i % 1000 == 0:
# pred = net(Variable(valid_data))
loss_log.append(loss.item())
net_valid = net(Variable(valid_x))
loss_valid = loss_func(net_valid, Variable(valid_y))
loss_log_valid.append(loss_valid.item())
acc_log.append(net.evaluate(
torch.max(net(Variable(valid_x[:100])).data, 1)[1], valid_y))
print('Epoch: {} - Loss: {:.6f}'.format(e + 1, loss.item()))
# In[24]:
plt.figure(figsize=(10, 8))
plt.plot(loss_log[2:])
plt.plot(loss_log_valid)
plt.plot(acc_log)
plt.plot(np.ones(len(acc_log)), linestyle='dashed')
plt.show()
print(net.state_dict())
torch.save(net.state_dict(), PATH)