forked from rasbt/python-machine-learning-book-3rd-edition
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ch13_part3.py
202 lines (126 loc) · 4.54 KB
/
ch13_part3.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# coding: utf-8
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST
from torchvision import transforms
from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator
from ignite.metrics import Accuracy, Loss
from ignite.handlers import Checkpoint, DiskSaver
from ignite.contrib.handlers import TensorboardLogger, global_step_from_engine
# # Chapter 13: Going Deeper -- the Mechanics of PyTorch (Part 3/3)
# ## Higher-level PyTorch APIs: a short introduction to PyTorch-Ignite
# ### Setting up the PyTorch model
image_path = './'
torch.manual_seed(1)
transform = transforms.Compose([
transforms.ToTensor()
])
mnist_train_dataset = MNIST(
root=image_path,
train=True,
transform=transform,
download=True
)
mnist_val_dataset = MNIST(
root=image_path,
train=False,
transform=transform,
download=False
)
batch_size = 64
train_loader = DataLoader(
mnist_train_dataset, batch_size, shuffle=True
)
val_loader = DataLoader(
mnist_val_dataset, batch_size, shuffle=False
)
def get_model(image_shape=(1, 28, 28), hidden_units=(32, 16)):
input_size = image_shape[0] * image_shape[1] * image_shape[2]
all_layers = [nn.Flatten()]
for hidden_unit in hidden_units:
layer = nn.Linear(input_size, hidden_unit)
all_layers.append(layer)
all_layers.append(nn.ReLU())
input_size = hidden_unit
all_layers.append(nn.Linear(hidden_units[-1], 10))
all_layers.append(nn.Softmax(dim=1))
model = nn.Sequential(*all_layers)
return model
device = "cuda" if torch.cuda.is_available() else "cpu"
model = get_model().to(device)
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
# ### Setting up training and validation engines with PyTorch-Ignite
trainer = create_supervised_trainer(
model, optimizer, loss_fn, device=device
)
val_metrics = {
"accuracy": Accuracy(),
"loss": Loss(loss_fn)
}
evaluator = create_supervised_evaluator(
model, metrics=val_metrics, device=device
)
# ### Creating event handlers for logging and validation
# How many batches to wait before logging training status
log_interval = 100
@trainer.on(Events.ITERATION_COMPLETED(every=log_interval))
def log_training_loss():
e = trainer.state.epoch
max_e = trainer.state.max_epochs
i = trainer.state.iteration
batch_loss = trainer.state.output
print(f"Epoch[{e}/{max_e}], Iter[{i}] Loss: {batch_loss:.2f}")
@trainer.on(Events.EPOCH_COMPLETED)
def log_validation_results():
eval_state = evaluator.run(val_loader)
metrics = eval_state.metrics
e = trainer.state.epoch
max_e = trainer.state.max_epochs
acc = metrics['accuracy']
avg_loss = metrics['loss']
print(f"Validation Results - Epoch[{e}/{max_e}] Avg Accuracy: {acc:.2f} Avg Loss: {avg_loss:.2f}")
# ### Setting up training checkpoints and saving the best model
# We will save in the checkpoint the following:
to_save = {"model": model, "optimizer": optimizer, "trainer": trainer}
# We will save checkpoints to the local disk
output_path = "./output"
save_handler = DiskSaver(dirname=output_path, require_empty=False)
# Set up the handler:
checkpoint_handler = Checkpoint(
to_save, save_handler, filename_prefix="training")
# Attach the handler to the trainer
trainer.add_event_handler(Events.EPOCH_COMPLETED, checkpoint_handler)
# Store best model by validation accuracy
best_model_handler = Checkpoint(
{"model": model},
save_handler,
filename_prefix="best",
n_saved=1,
score_name="accuracy",
score_function=Checkpoint.get_default_score_fn("accuracy"),
)
evaluator.add_event_handler(Events.COMPLETED, best_model_handler)
# ### Setting up TensorBoard as an experiment tracking system
tb_logger = TensorboardLogger(log_dir=output_path)
# Attach handler to plot trainer's loss every 100 iterations
tb_logger.attach_output_handler(
trainer,
event_name=Events.ITERATION_COMPLETED(every=100),
tag="training",
output_transform=lambda loss: {"batch_loss": loss},
)
# Attach handler for plotting both evaluators' metrics after every epoch completes
tb_logger.attach_output_handler(
evaluator,
event_name=Events.EPOCH_COMPLETED,
tag="validation",
metric_names="all",
global_step_transform=global_step_from_engine(trainer),
)
# ### Executing the PyTorch-Ignite model training code
trainer.run(train_loader, max_epochs=5)
# ---
#
# Readers may ignore the next cell.