-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_ignite_energy.py
167 lines (128 loc) · 5.91 KB
/
main_ignite_energy.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
import numpy as np
import torch
from torch import optim
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import TensorDataset, DataLoader
from torch.nn.parameter import Parameter
import net
import jem
from ignite.engine import Events, create_supervised_trainer, create_supervised_evaluator
from ignite.handlers import ModelCheckpoint, EarlyStopping
from ignite.metrics import Accuracy, Loss
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from tqdm import tqdm
def get_data_loaders(batch_size):
# 1. MNISTのデータセットを取得
mnist = fetch_openml('mnist_784', version=1,)
# 2. データの設定(入力データは閉区間[0, 1]に正規化する)
x = mnist.data / 255
y = mnist.target.astype(np.int32)
# 3. DataLoaderの作成
# 3-1. データを学習用とテスト用に分割
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=1/7, random_state=0)
# 3-2. データのフォーマットを変換:PyTorchでの形式 = [画像数,チャネル数,高さ,幅]
x_train = x_train.reshape(60000, 28* 28)
x_test = x_test.reshape(10000, 28*28)
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(-1, 1))
scaler.fit(x)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
print(x_train.max(), x_train.min())
print(x_test.max(), x_test.min())
# 3-3. PyTorchのテンソルに変換
x_train = Parameter(torch.Tensor(x_train))
x_test = torch.Tensor(x_test)
y_train = torch.LongTensor(y_train)
y_test = torch.LongTensor(y_test)
# 3-4. 入力(x)とラベル(y)を組み合わせて最終的なデータを作成
ds_train = TensorDataset(x_train, y_train)
ds_test = TensorDataset(x_test, y_test)
# 3-5. DataLoaderを作成
train_loader = DataLoader(ds_train, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(ds_test, batch_size=batch_size, shuffle=False)
return train_loader, test_loader
def main(batch_size, epochs):
# 1. GPUの設定(PyTorchでは明示的に指定する必要がある)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
train_loader, test_loader = get_data_loaders(batch_size)
# 2. モデル作成
# model = net.CNN(num_classes=num_classes).to(device)
model = net.Net(1000,10).to(device)
print(model) # ネットワークの詳細を確認用に表示
# 3. 損失関数を定義
criterion = nn.CrossEntropyLoss()
# 4. 最適化手法を定義(ここでは例としてAdamを選択)
# optimizer = optim.Adam(model.parameters(), lr=0.001)
optimizer = optim.Adam(model.parameters())
buffer_size = 10000
B = jem.ReplayBuffer(buffer_size)
m_uniform = torch.distributions.uniform.Uniform(torch.tensor([-1.0]), torch.tensor([1.0]))
B.add(m_uniform.sample((100, 784)).squeeze())
trainer = jem.create_supervised_trainer2(model, optimizer, criterion, device=device, replay_buffer=B)
train_evaluator = create_supervised_evaluator(model, metrics={'accuracy': Accuracy(),'loss':Loss(criterion)}, device=device)
test_evaluator = create_supervised_evaluator(model, metrics={'accuracy': Accuracy(),'loss':Loss(criterion)}, device=device)
desc = "ITERATION - loss: {:.2f}"
pbar = tqdm(
initial=0, leave=False, total=len(train_loader),
desc=desc.format(0)
)
log_interval = 10
# 5. ログ出力
@trainer.on(Events.ITERATION_COMPLETED)
def log_training_loss(engine):
i = (engine.state.iteration - 1) % len(train_loader) + 1
if i % log_interval == 0:
pbar.desc = desc.format(engine.state.output['loss'])
pbar.update(log_interval)
@trainer.on(Events.EPOCH_COMPLETED)
def log_training_results(engine):
pbar.refresh()
engine_metrics = engine.state.metrics
avg_loss = engine_metrics['loss']
avg_loss_elf = engine_metrics['loss_elf']
avg_loss_gen = engine_metrics['loss_gen']
tqdm.write(
"Engine Results - Epoch: {} Avg loss: {:.4f} Avg loss elf: {:.4f} Avg loss gen:{:.4f}"
.format(engine.state.epoch, avg_loss, avg_loss_elf, avg_loss_gen)
)
train_evaluator.run(train_loader)
metrics = train_evaluator.state.metrics
avg_accuracy = metrics['accuracy']
avg_loss = metrics['loss']
tqdm.write(
"Training Results - Epoch: {} Avg accuracy: {:.3f} Avg loss: {:.4f}"
.format(engine.state.epoch, avg_accuracy, avg_loss)
)
@trainer.on(Events.EPOCH_COMPLETED)
def log_validation_results(engine):
test_evaluator.run(test_loader)
metrics = test_evaluator.state.metrics
avg_accuracy = metrics['accuracy']
avg_loss = metrics['loss']
tqdm.write(
"Validation Results - Epoch: {} Avg accuracy: {:.3f} Avg loss: {:.4f}"
.format(engine.state.epoch, avg_accuracy, avg_loss))
pbar.n = pbar.last_print_n = 0
def score_function(engine):
val_loss = engine.state.metrics['loss']
return -val_loss
# 5. checkpoint setting
best_handler = ModelCheckpoint(dirname='./checkpoints', filename_prefix='best',
n_saved=3,score_name='loss',score_function=score_function,
create_dir=True,require_empty=False)
test_evaluator.add_event_handler(Events.EPOCH_COMPLETED, best_handler, {'mymodel': model})
early_handler = EarlyStopping(patience=5, score_function=score_function, trainer=trainer)
# Note: the handler is attached to an *Evaluator* (runs one epoch on validation dataset)
test_evaluator.add_event_handler(Events.COMPLETED, early_handler)
# 6. 実行
trainer.run(train_loader, max_epochs=epochs)
pbar.close()
if __name__ == '__main__':
batch_size = 100
# num_classes = 10
epochs = 20
main(batch_size, epochs)