-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathopts.py
139 lines (126 loc) · 6.56 KB
/
opts.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
"""Argument parser for genral and model / data specific options."""
import os
import argparse
import torch
import utils
class Opts():
"""All configurations and hyperparameters."""
def __init__(self):
"""Process calling arguments."""
self.parse()
self.args.data = os.path.join(self.args.rootDir, 'data')
self.args.model = os.path.join(self.args.rootDir, 'exp')
# Set torch default tensor type and random seed
torch.set_default_tensor_type('torch.FloatTensor')
torch.manual_seed(self.args.manualSeed)
# Customized parameters for dataset
if 'onebest' in self.args.dataset:
self.args.inputSize = 52
self.args.onebest = True
elif self.args.dataset.startswith('lattice'):
self.args.inputSize = 54
elif self.args.dataset.startswith('confnet'):
self.args.inputSize = 52
else:
raise ValueError
# Customized parameters for the network
arch = self.args.arch.split('-')
assert len(arch) == 4, 'bad architecture input argument'
self.args.nLSTMLayers = int(arch[0])
self.args.hiddenSize = int(arch[1])
self.args.nFCLayers = int(arch[2])
self.args.linearSize = int(arch[3])
self.args.bidirectional = True
if self.args.method == 'attention':
self.args.attentionLayers = 1
self.args.attentionSize = 64
# Settings for debug mode
if self.args.debug:
self.args.nEpochs = 2
self.args.nThreads = 1
# Setup model directory
self.args.hashKey = self.args.dataset \
+ '_' + self.args.arch \
+ '_' + self.args.method \
+ '_' + 'L='+str(self.args.LR) \
+ '_' + 'M='+str(self.args.momentum) \
+ '_' + 'S='+str(self.args.batchSize) \
+ '_' + 'O='+str(self.args.optimizer) \
+ '_' + 'D='+self.args.LRDecay \
+ '-' + str(self.args.LRDParam) \
+ '_' + self.args.suffix
if self.args.debug:
self.args.hashKey += '_debug'
self.args.resume = os.path.join(self.args.model, self.args.hashKey)
utils.mkdir(self.args.resume)
# Display all options
utils.print_options(self.args)
def parse(self):
"""Parsing calling arguments."""
parser = argparse.ArgumentParser(description='parser for latticeRNN')
# General options
parser.add_argument('--debug', default=False, action="store_true",
help='Debug mode, only run 2 epochs and 1 thread')
parser.add_argument('--manualSeed', default=1, type=int,
help='Manual seed')
# Path options
parser.add_argument('--rootDir', type=str, required=True,
help='path to experiment root directory')
# Data options
parser.add_argument('--dataset', default='lattice_mapped_0.1_prec', type=str,
help='Name of dataset',
choices=['lattice_mapped_0.1_prec'])
parser.add_argument('--nThreads', default=10, type=int,
help='Number of data loading threads')
parser.add_argument('--trainPctg', default=1.0, type=float,
help='Percentage of taining data to use')
parser.add_argument('--shuffle', default=False, action="store_true",
help='Flag to shuffle the dataset before training')
# Training/testing options
parser.add_argument('--nEpochs', default=15, type=int,
help='Number of total epochs to run')
parser.add_argument('--epochNum', default=0, type=int,
help='0=retrain|-1=latest|-2=best',
choices=[0, -1, -2])
parser.add_argument('--batchSize', default=1, type=int,
help='Mini-batch size')
parser.add_argument('--saveOne', default=False, action="store_true",
help='Only preserve one saved model')
parser.add_argument('--valOnly', default=False, action="store_true",
help='Run on validation set only')
parser.add_argument('--testOnly', default=False, action="store_true",
help='Run the test to see the performance')
parser.add_argument('--onebest', default=False, action="store_true",
help='Train on one-best path only')
# Optimization options
parser.add_argument('--LR', default=0.05, type=float,
help='Initial learning rate')
parser.add_argument('--LRDecay', default='none', type=str,
help='Learning rate decay method',
choices=['anneal', 'stepwise', 'newbob', 'none'])
parser.add_argument('--LRDParam', default=0.5, type=float,
help='Param for learning rate decay')
parser.add_argument('--momentum', default=0.5, type=float,
help='Momentum')
parser.add_argument('--weightDecay', default=1e-3, type=float,
help='Weight decay')
parser.add_argument('--clip', default=1.0, type=float,
help='Gradient clipping')
parser.add_argument('--optimizer', default='SGD', type=str,
help='Optimizer type',
choices=['SGD', 'Adam'])
# Model options
parser.add_argument('--init', default='kaiming_normal', type=str,
help='Initialisation method for linear layers',
choices=['uniform', 'normal',
'xavier_uniform', 'xavier_normal',
'kaiming_uniform', 'kaiming_normal'])
parser.add_argument('--arch', default='1-128-1-128', type=str,
help='Model architecture: '\
'nLSTMLayer-LSTMSize-nFCLayer-nFCSize')
parser.add_argument('--method', default='mean', type=str,
help='method for combining edges',
choices=['mean', 'max', 'posterior', 'attention'])
parser.add_argument('--suffix', default='LatticeRNN', type=str,
help='Suffix for saving the model')
self.args = parser.parse_args()