-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontinue_experiment.py
127 lines (111 loc) · 4.15 KB
/
continue_experiment.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Created by Brian B. Moser.
# Contact: Brian.Moser@DFKI.de
import argparse
import torch
import sys
import os
sys.path.insert(0, os.path.abspath('..')) # noqa
from defusernn_models.bidi_lstm import BidiLSTM
from src.models.n_renet import N_ReNet
from defusernn_models.conv_lstm import ConvLSTM
from defusernn_models.md_lstm import MDMDLSTMModel
from lucky_trainer.utils import get_dataset
from lucky_trainer.trainer import Trainer
# Device configuration (DO NOT EDIT)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
parser = argparse.ArgumentParser()
parser.add_argument("-d",
"--directory",
type=str,
required=True,
help="file path of the state without last /")
parser.add_argument("-f",
"--filename",
type=str,
required=True,
help="file name of the state")
args = parser.parse_args()
args_directory = args.directory
args_filename = args.filename
def get_model(model_params, train_loader):
"""
Loads the model given by the entry <model> in the dictionary model_params.
:param model_params: Dictionary with the parameters and name of the model
:param train_loader: Train dataset, which is needed to get important
dimensions like Time-Steps
:return: A Model (BidiLSTM, ReNet, ConvLSTM, MD-LSTM, pyraMiD-LSTM)
"""
if model_params['model'] == 'bidi_lstm':
return BidiLSTM(next(iter(train_loader))[0][0].shape[1], model_params)
elif model_params['model'] == 'renet':
assert next(iter(train_loader))[0][0].shape[0] == \
model_params['input_dim'][0] and \
next(iter(train_loader))[0][0].shape[1] == \
model_params['input_dim'][1] and \
next(iter(train_loader))[0][0].shape[2] == \
model_params['input_dim'][2], \
"<input_dim> does not match dataset"
return N_ReNet(model_params['input_dim'], model_params)
elif model_params['model'] == 'conv_lstm':
return ConvLSTM(next(iter(train_loader))[0][0].shape, model_params)
elif model_params['model'] == 'md_lstm':
return MDMDLSTMModel(
next(iter(train_loader))[0][0].shape[0],
model_params
)
elif model_params['model'] == 'pyramid_lstm':
pass # todo
else:
raise NameError(
model_params['model'] + ' is not defined.'
+ ' Choose one of the following: bidi_lstm, renet, '
+ 'conv_net, md_lstm or pyramid_lstm.'
)
def main(_args_directory, _args_filename):
state = torch.load(_args_directory + "/" + _args_filename)
model_params = state['model_params']
train_params = state['train_params']
dataset_params = state['dataset_params']
model_state = state['model_state']
optimizer_state = state['optimizer_state']
epoch = state['epoch']
# Load iterable datasets
train_loader = get_dataset(
dataset_params['train_filename'],
train_params['batch_size']
)
validation_loader = get_dataset(
dataset_params['validation_filename'],
train_params['batch_size'],
shuffle=False
)
test_loader = get_dataset(
dataset_params['test_filename'],
train_params['batch_size'],
shuffle=False
)
model = get_model(model_params, train_loader).to(device)
model.load_state_dict(model_state)
# Train the model
trainer = Trainer(
model,
model_params,
train_params,
dataset_params,
train_loader,
validation_loader,
test_loader,
_args_directory,
_args_filename,
current_epoch=epoch
)
trainer.optimizer.load_state_dict(optimizer_state)
print("Welcome back. Continuing Training...")
try:
trainer.train()
except KeyboardInterrupt:
trainer.save_progress(_args_directory, _args_filename)
if __name__ == "__main__":
main(args_directory, args_filename)