-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathutil.py
More file actions
executable file
·214 lines (187 loc) · 9.05 KB
/
Copy pathutil.py
File metadata and controls
executable file
·214 lines (187 loc) · 9.05 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import os
import math
import reactions
import tensorflow as tf
import rnn
from model import Optimizer
from shutil import copyfile
def run_epoch(sess, cost_op, ops, reset, num_unrolls):
"""Runs one optimization epoch."""
sess.run(reset)
for _ in range(num_unrolls):
results = sess.run([cost_op] + ops)
return results[0], results[1:]
def create_model(sess, config, logger):
if not config.save_path == None:
if not os.path.exists(config.save_path):
os.mkdir(config.save_path)
copyfile('config.json', os.path.join(config.save_path, 'config.json'))
if config.opt_direction == 'max':
problem_type = 'concave'
else:
problem_type = 'convex'
if config.reaction_type == 'quad' and config.constraints == False:
rxn_yeild = reactions.Quadratic(
batch_size=config.batch_size,
num_dims=config.num_params,
ptype=problem_type,
random=config.instrument_error)
elif config.reaction_type == 'quad' and config.constraints == True:
rxn_yeild = reactions.ConstraintQuadratic(
batch_size=config.batch_size,
num_dims=config.num_params,
ptype=problem_type,
random=config.instrument_error)
elif config.reaction_type == 'gmm':
rxn_yeild = reactions.GMM(
batch_size=config.batch_size,
num_dims=config.num_params,
random=config.instrument_error,
cov=config.norm_cov)
if config.policy == 'srnn':
cell = rnn.StochasticRNNCell(cell=rnn.LSTM,
kwargs=
{'hidden_size':config.hidden_size,
'use_batch_norm_h':config.batch_norm,
'use_batch_norm_x':config.batch_norm,
'use_batch_norm_c':config.batch_norm,},
nlayers=config.num_layers,
reuse=config.reuse)
if config.policy == 'rnn':
cell = rnn.MultiInputRNNCell(cell=rnn.LSTM,
kwargs=
{'hidden_size':config.hidden_size,
'use_batch_norm_h':config.batch_norm,
'use_batch_norm_x':config.batch_norm,
'use_batch_norm_c':config.batch_norm,},
nlayers=config.num_layers,
reuse=config.reuse)
model = Optimizer(cell=cell, logger=logger, func=rxn_yeild,
ndim=config.num_params, batch_size=config.batch_size,
unroll_len=config.unroll_length, lr=config.learning_rate,
loss_type=config.loss_type, optimizer=config.optimizer,
trainable_init=config.trainable_init,
direction=config.opt_direction, constraints=config.constraints,
discount_factor=config.discount_factor)
ckpt = tf.train.get_checkpoint_state(config.save_path)
if ckpt and ckpt.model_checkpoint_path:
logger.info('Reading model parameters from {}.'.format(
ckpt.model_checkpoint_path))
model.saver.restore(sess, ckpt.model_checkpoint_path)
else:
logger.info('Creating Model with fresh parameters.')
sess.run(tf.global_variables_initializer())
return model
def load_model(sess, config, logger):
assert(os.path.exists(config.save_path))
if config.opt_direction == 'max':
problem_type = 'concave'
else:
problem_type = 'convex'
if config.reaction_type == 'quad' and config.constraints == False:
rxn_yeild = reactions.Quadratic(
batch_size=config.batch_size,
num_dims=config.num_params,
ptype=problem_type,
random=config.instrument_error)
elif config.reaction_type == 'quad' and config.constraints == True:
rxn_yeild = reactions.ConstraintQuadratic(
batch_size=config.batch_size,
num_dims=config.num_params,
ptype=problem_type,
random=config.instrument_error)
elif config.reaction_type == 'gmm':
rxn_yeild = reactions.GMM(
batch_size=config.batch_size,
num_dims=config.num_params,
random=config.instrument_error,
cov=config.norm_cov)
if config.policy == 'srnn':
cell = rnn.StochasticRNNCell(cell=rnn.LSTM,
kwargs=
{'hidden_size':config.hidden_size,
'use_batch_norm_h':config.batch_norm,
'use_batch_norm_x':config.batch_norm,
'use_batch_norm_c':config.batch_norm,},
nlayers=config.num_layers,
reuse=config.reuse)
if config.policy == 'rnn':
cell = rnn.MultiInputRNNCell(cell=rnn.LSTM,
kwargs=
{'hidden_size':config.hidden_size,
'use_batch_norm_h':config.batch_norm,
'use_batch_norm_x':config.batch_norm,
'use_batch_norm_c':config.batch_norm,},
nlayers=config.num_layers,
reuse=config.reuse)
model = Optimizer(cell=cell, logger=logger, func=rxn_yeild,
ndim=config.num_params, batch_size=config.batch_size,
unroll_len=config.unroll_length, lr=config.learning_rate,
loss_type=config.loss_type, optimizer=config.optimizer,
trainable_init=config.trainable_init,
direction=config.opt_direction, constraints=config.constraints,
discount_factor=config.discount_factor)
ckpt = tf.train.get_checkpoint_state(config.save_path)
if ckpt and ckpt.model_checkpoint_path:
logger.info('Reading model parameters from {}.'.format(
ckpt.model_checkpoint_path))
model.saver.restore(sess, ckpt.model_checkpoint_path)
return model
def check_initializers(initializers, keys):
if initializers is None:
return {}
keys = set(keys)
if not issubclass(type(initializers), dict):
raise TypeError("A dict of initializers was expected, but not "
"given. You should double-check that you've nested the "
"initializers for any sub-modules correctly.")
if not set(initializers) <= keys:
extra_keys = set(initializers) - keys
raise KeyError(
"Invalid initializer keys {}, initializers can only "
"be provided for {}".format(
", ".join("'{}'".format(key) for key in extra_keys),
", ".join("'{}'".format(key) for key in keys)))
def check_nested_callables(dictionary):
for key, entry in dictionary.items():
if isinstance(entry, dict):
check_nested_callables(entry)
elif not callable(entry):
raise TypeError(
"Initializer for '{}' is not a callable function "
"or dictionary".format(key))
check_nested_callables(initializers)
return dict(initializers)
def create_linear_initializer(input_size):
"""Returns a default initializer for weights or bias of a linear module."""
stddev = 1 / math.sqrt(input_size)
return tf.truncated_normal_initializer(stddev=stddev)
def trainable_initial_state(batch_size, state_size, dtype, initializers=None):
flat_state_size = nest.flatten(state_size)
if not initializers:
flat_initializer = tuple(tf.zeros_initializer for _ in flat_state_size)
else:
nest.assert_same_structure(initializers, state_size)
flat_initializer = nest.flatten(initializers)
if not all([callable(init) for init in flat_initializer]):
raise ValueError("Not all the passed initializers are callable objects.")
# Produce names for the variables. In the case of a tuple or nested tuple,
# this is just a sequence of numbers, but for a flat `namedtuple`, we use
# the field names. NOTE: this could be extended to nested `namedtuple`s,
# but for now that's extra complexity that's not used anywhere.
try:
names = ["init_{}".format(state_size._fields[i])
for i in range(len(flat_state_size))]
except (AttributeError, IndexError):
names = ["init_state_{}".format(i) for i in range(len(flat_state_size))]
flat_initial_state = []
for name, size, init in zip(names, flat_state_size, flat_initializer):
shape_with_batch_dim = [1] + tensor_shape.as_shape(size).as_list()
initial_state_variable = tf.get_variable(
name, shape=shape_with_batch_dim, dtype=dtype, initializer=init)
initial_state_variable_dims = initial_state_variable.get_shape().ndims
tile_dims = [batch_size] + [1] * (initial_state_variable_dims - 1)
flat_initial_state.append(
tf.tile(initial_state_variable, tile_dims, name=(name + "_tiled")))
return nest.pack_sequence_as(structure=state_size,
flat_sequence=flat_initial_state)