update leras

This commit is contained in:
Colombo 2020-03-04 15:08:10 +04:00
commit cd76425db3
2 changed files with 7 additions and 13 deletions

View file

@ -64,12 +64,12 @@ def initialize_layers(nn):
sub_w_name = "/".join(w_name_split[1:]) sub_w_name = "/".join(w_name_split[1:])
w_val = d.get(sub_w_name, None) w_val = d.get(sub_w_name, None)
w_val = np.reshape( w_val, w.shape.as_list() )
if w_val is None: if w_val is None:
io.log_err(f"Weight {w.name} was not loaded from file {filename}") #io.log_err(f"Weight {w.name} was not loaded from file {filename}")
tuples.append ( (w, w.initializer) ) tuples.append ( (w, w.initializer) )
else: else:
w_val = np.reshape( w_val, w.shape.as_list() )
tuples.append ( (w, w_val) ) tuples.append ( (w, w_val) )
nn.tf_batch_set_value(tuples) nn.tf_batch_set_value(tuples)

View file

@ -60,25 +60,19 @@ def initialize_optimizers(nn):
self.epsilon = tf.Variable (epsilon, name="epsilon") self.epsilon = tf.Variable (epsilon, name="epsilon")
self.iterations = tf.Variable(0, dtype=tf.int64, name='iters') self.iterations = tf.Variable(0, dtype=tf.int64, name='iters')
self.accumulators = []
self.accumulator_counter = 0
self.accumulators_dict = {} self.accumulators_dict = {}
self.lr_rnds_dict = {} self.lr_rnds_dict = {}
def get_weights(self): def get_weights(self):
return [self.lr, self.rho, self.epsilon, self.iterations] + self.accumulators return [self.lr, self.rho, self.epsilon, self.iterations] + list(self.accumulators_dict.values())
def initialize_variables(self, trainable_weights, vars_on_cpu=True): def initialize_variables(self, trainable_weights, vars_on_cpu=True):
# Initialize here all trainable variables used in training # Initialize here all trainable variables used in training
e = tf.device('/CPU:0') if vars_on_cpu else None e = tf.device('/CPU:0') if vars_on_cpu else None
if e: e.__enter__() if e: e.__enter__()
with tf.variable_scope(self.name): with tf.variable_scope(self.name):
accumulators = [ tf.get_variable ( f'acc_{i+self.accumulator_counter}', v.shape, dtype=v.dtype, initializer=tf.initializers.constant(0.0), trainable=False) accumulators = { v.name : tf.get_variable ( f'acc_{v.name}'.replace(':','_'), v.shape, dtype=v.dtype, initializer=tf.initializers.constant(0.0), trainable=False) for v in trainable_weights }
for (i, v ) in enumerate(trainable_weights) ] self.accumulators_dict.update ( accumulators)
self.accumulators_dict.update ( { v.name : acc for v,acc in zip(trainable_weights,accumulators) } )
self.accumulators += accumulators
self.accumulator_counter += len(trainable_weights)
if self.lr_dropout != 1.0: if self.lr_dropout != 1.0:
lr_rnds = [ nn.tf_random_binomial( v.shape, p=self.lr_dropout, dtype=v.dtype) for v in trainable_weights ] lr_rnds = [ nn.tf_random_binomial( v.shape, p=self.lr_dropout, dtype=v.dtype) for v in trainable_weights ]