-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·138 lines (104 loc) · 4.24 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
#!/usr/bin/env python3
import os, sys
sys.path.append("/Users/KerimErekmen/Desktop/chesstwitter/lib/")
sys.path.append("/Users/KerimErekmen/Desktop/chesstwitter/lib/python3.9/site-packages/sklearn/")
import numpy as np
import torch
from torch.utils.data import Dataset
import torch.nn.functional as function_
from torch import optim
import torch.nn as nn
from sklearn.model_selection import train_test_split
class CustomImageDataset(Dataset):
def __init__(self, file_dir):
self.con = True # for every object disjunct
if str(file_dir).endswith(".npz"):
self.con = False
file_data = np.load(file_dir)
self.x = file_data['arr_0']
self.y = file_data['arr_1']
else:
self.x = file_dir # is transformed
self.y = file_dir
print("loaded file")
def __len__(self):
"""number of samples in our dataset
Returns:
int: number of samples
"""
return self.x.shape[0]
def __getitem__(self, idx):
"""loads and returns a sample from the dataset at the given index idx
Args:
idx (int): index of sample
Returns:
sample, label = x, y
"""
if self.con:
return (self.x[idx][0], self.y[idx][1])
else:
return (self.x[idx], self.y[idx])
return None
class NN(nn.Module):
def __init__(self):
super(NN, self).__init__()
self.a1 = nn.Conv2d(16, 32, kernel_size=3, padding=1)
self.a2 = nn.Conv2d(32, 32, kernel_size=3, padding=1)
self.a3 = nn.Conv2d(32, 64, kernel_size=3, stride=2)
self.b1 = nn.Conv2d(64, 64, kernel_size=3, padding=1)
self.b2 = nn.Conv2d(64, 64, kernel_size=3, padding=1)
self.b3 = nn.Conv2d(64, 128, kernel_size=3, stride=2)
self.d1 = nn.Conv2d(128, 128, kernel_size=3, padding=1)
self.d2 = nn.Conv2d(128, 128, kernel_size=3, padding=1)
self.d3 = nn.Conv2d(128, 128, kernel_size=1)
self.last = nn.Linear(128, 1)
def forward(self, x):
x = function_.relu(self.a1(x))
x = function_.relu(self.a2(x))
x = function_.relu(self.a3(x))
x = function_.relu(self.b1(x))
x = function_.relu(self.b2(x))
x = function_.relu(self.b3(x))
x = function_.relu(self.d1(x))
x = function_.relu(self.d2(x))
x = function_.relu(self.d3(x))
x = x.view(-1, 128)
x = function_.tanh(self.last(x))
return x
def train(self, model, optim, loader, criterion, epochs):
batches = len(loader)
for epoch in range(1, epochs+1):
running_loss = 0
correct_acc = 0 # TP+FP/TP+FP+TN+FN
total_loss = 0
print(f"Epoch {epoch} \n")
for i, (data, labels) in enumerate(loader):
data = data.to(dtype=torch.float)
labels = labels.to(dtype=torch.float)
optim.zero_grad()
output = model(data)
loss = criterion(output, labels.unsqueeze(-1))
loss.backward()
optim.step()
running_loss += loss.item()
total_loss += 1
if i % 1000 == 0:
print(f"Batch [{i}/{batches}], Loss [{loss.item():.4f}], outputmin {output.min()}, outputmax {output.max()}")
print(f"Total Loss of {running_loss/total_loss:.4f} for epoch {epoch}")
if __name__ == "__main__":
chess_dataset = CustomImageDataset("chess_nn_data.npz")
dataset = np.array(list(zip(list(chess_dataset.x), list(chess_dataset.y))))
#print(dataset.shape)
train_set, test_set = train_test_split(dataset, test_size=0.25)
train_set = CustomImageDataset(train_set)
test_set = CustomImageDataset(test_set)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=32, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_set, batch_size=32, shuffle=True)
model = NN()
optimizer = optim.Adam(model.parameters(), lr=1e-3)
criterion = nn.MSELoss()
model.train(model=model, optim=optimizer, loader=train_loader, criterion=criterion, epochs=10)
"""
for data, target in train_loader:
print(data.shape, target.shape)
"""