-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
165 lines (140 loc) · 4.9 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
159
160
161
162
163
164
165
class TrainConfig:
max_depth = 76 # God's Number
batch_size_per_depth = 1000
num_steps = 100
learning_rate = 1e-4
ENABLE_FP16 = True # Set this to True if you want to train the model faster
import os
import numpy as np
from contextlib import nullcontext
from tqdm import trange
from IPython.display import clear_output
import matplotlib.pyplot as plt
import torch
from torch import nn
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f'device: {device}')
print(f'os.cpu_count(): {os.cpu_count()}')
env = Balto()
class LinearBlock(nn.Module):
"""
Linear layer with ReLU and BatchNorm
"""
def __init__(self, input_prev, embed_dim):
super(LinearBlock, self).__init__()
self.fc = nn.Linear(input_prev, embed_dim)
self.relu = nn.ReLU()
self.bn = nn.BatchNorm1d(embed_dim)
def forward(self, inputs):
x = inputs
x = self.fc(x)
x = self.relu(x)
x = self.bn(x)
return x
class ResidualBlock(nn.Module):
"""
Residual block with two linear layers
"""
def __init__(self, embed_dim):
super(ResidualBlock, self).__init__()
self.layers = nn.ModuleList([
LinearBlock(embed_dim, embed_dim),
LinearBlock(embed_dim, embed_dim)
])
def forward(self, inputs):
x = inputs
for layer in self.layers:
x = layer(x)
x += inputs # skip-connection
return x
class Model(nn.Module):
"""
Fixed architecture following DeepCubeA.
"""
def __init__(self, input_dim=SIZE**2, output_dim=len(env.moves)):
super(Model, self).__init__()
self.input_dim = input_dim
self.embedding = LinearBlock(input_dim, 5000)
self.layers = nn.ModuleList([
LinearBlock(5000,1000),
ResidualBlock(1000),
ResidualBlock(1000),
ResidualBlock(1000),
ResidualBlock(1000)
])
self.output = nn.Linear(1000, output_dim)
def forward(self, inputs):
# int indices => float one-hot vectors
x = nn.functional.one_hot(inputs, num_classes=SIZE).to(torch.float)
x = x.reshape(-1, self.input_dim)
x = self.embedding(x)
for layer in self.layers:
x = layer(x)
logits = self.output(x)
return logits
class ScrambleGenerator(torch.utils.data.Dataset):
def __init__(
self,
num_workers=os.cpu_count(),
max_depth=TrainConfig.max_depth,
total_samples=TrainConfig.num_steps*TrainConfig.batch_size_per_depth
):
self.num_workers = num_workers
self.max_depth = max_depth
self.envs = [Balto() for _ in range(num_workers)]
self.generators = [env.scrambler(self.max_depth) for env in self.envs]
self.total_samples = total_samples
def __len__(self):
return self.total_samples
def __getitem__(self, i):
'generate one scramble, consisting of `self.max_depth` data points'
worker_idx = i % self.num_workers
X = np.zeros((self.max_depth, SIZE), dtype=int)
y = np.zeros((self.max_depth,), dtype=int)
for j in range(self.max_depth):
state, last_move = next(self.generators[worker_idx])
X[j, :] = state
y[j] = last_move
return X, y
def plot_loss_curve(h):
fig, ax = plt.subplots(1, 1)
ax.plot(h)
ax.set_xlabel("Steps")
ax.set_ylabel("Cross-entropy loss")
plt.show()
def train(model, dataloader):
model.train()
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=TrainConfig.learning_rate)
g = iter(dataloader)
ctx = torch.autocast(str(device), dtype=torch.float16) if TrainConfig.ENABLE_FP16 else nullcontext()
h = []
for i in trange(1, TrainConfig.num_steps + 1, smoothing=0):
batch_x, batch_y = next(g)
batch_x, batch_y = batch_x.reshape(-1, SIZE).to(device), batch_y.reshape(-1).to(device)
with ctx:
pred_y = model(batch_x)
loss = loss_fn(pred_y, batch_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
h.append(loss.item())
if i % 25 == 0:
clear_output()
plot_loss_curve(h)
print(f"Trained on data equivalent to {TrainConfig.batch_size_per_depth * TrainConfig.num_steps} solves.")
print(h)
return model
if __name__ == '__main__':
model = Model().to(device)
model.load_state_dict(torch.load('model.pth', weights_only=True))
dataloader = torch.utils.data.DataLoader(
ScrambleGenerator(),
num_workers=os.cpu_count(),
batch_size=TrainConfig.batch_size_per_depth
)
# import cProfile
# import re
# cProfile.run('model = train(model, dataloader)', 'out')
model = train(model, dataloader)
torch.save(model.state_dict(), "model.pth")