def build_loss_nodes(self, model, parameters): with tf.name_scope('loss'): with tf.name_scope('distance'): distance = Standard_trainer.build_distance( self, model, parameters) if model.sparsity_coeff != 0: with tf.name_scope('sparse_output_constraint'): sparse_loss, sparse_loss_mean = self.build_sparsity_term( model, parameters) loss_val_ = distance + sparse_loss self.sparse_loss_mean = sparse_loss_mean else: loss_val_ = distance temp = tf.zeros_like(loss_val_) self.sparse_loss_mean = tf.reduce_mean(temp) with tf.name_scope("NADE_mask_input"): ################################################## ################################################## # Masked gradients are the values known in the input : so 1 - mask are used for gradient # loss_val_masked = tf.boolean_mask(self.mask_input==1, loss_val_, tf.zeros_like(loss_val_)) loss_val_masked_ = tf.stop_gradient( self.mask_input * loss_val_) + (1 - self.mask_input) * loss_val_ # LEQUEL ?? ################################################## ################################################## # Normalization norm_nade = model.orch_dim / (model.orch_dim - tf.reduce_sum( self.mask_input, axis=1) + 1) loss_val_masked = model.orch_dim # Note: don't reduce_mean the validation loss, we need to have the per-sample value if parameters['mask_orch']: with tf.name_scope('mask_orch'): loss_masked = tf.where(mask_orch_ph == 1, loss_val_, tf.zeros_like(loss_val_)) self.loss_val = loss_masked else: self.loss_val = loss_val_ mean_loss = tf.reduce_mean(self.loss_val) with tf.name_scope('weight_decay'): weight_decay = Standard_trainer.build_weight_decay(self, model) # Weight decay if model.weight_decay_coeff != 0: # Keras weight decay does not work... self.loss = mean_loss + weight_decay else: self.loss = mean_loss return
def build_loss_nodes(self, model, parameters): with tf.name_scope('loss'): with tf.name_scope('distance'): distance = self.build_distance(model, parameters) # self.aaa = distance if model.sparsity_coeff != 0: with tf.name_scope('sparse_output_constraint'): sparse_loss, sparse_loss_mean = self.build_sparsity_term( model, parameters) loss_val_ = distance + sparse_loss self.sparse_loss_mean = sparse_loss_mean else: loss_val_ = distance temp = tf.zeros_like(loss_val_) self.sparse_loss_mean = tf.reduce_mean(temp) with tf.name_scope("NADE_mask_input"): # Masked gradients are the values known in the input : so 1 - mask are used for gradient loss_val_masked_ = (1 - self.mask_input) * loss_val_ # Mean along pitch axis loss_val_masked_mean = tf.reduce_mean(loss_val_masked_, axis=1) # NADE Normalization nombre_unit_masked_in = tf.reduce_sum(self.mask_input, axis=1) norm_nade = model.orch_dim / (model.orch_dim - nombre_unit_masked_in + 1) loss_val_masked = norm_nade * loss_val_masked_mean # Note: don't reduce_mean the validation loss, we need to have the per-sample value # if parameters['mask_orch']: # with tf.name_scope('mask_orch'): # self.loss_val = tf.where(mask_orch_ph==1, loss_val_masked, tf.zeros_like(loss_val_masked)) # else: # self.loss_val = loss_val_masked self.loss_val = loss_val_masked mean_loss = tf.reduce_mean(self.loss_val) with tf.name_scope('weight_decay'): weight_decay = Standard_trainer.build_weight_decay(self, model) # Weight decay if model.weight_decay_coeff != 0: # Keras weight decay does not work... self.loss = mean_loss + weight_decay else: self.loss = mean_loss return
def build_loss_nodes(self, model, parameters): with tf.name_scope('loss'): with tf.name_scope('distance'): self.loss_val = self.build_distance(model, parameters) import pdb; pdb.set_trace() # Mean the loss mean_loss = tf.reduce_mean(self.loss_val) # Weight decay if model.weight_decay_coeff != 0: with tf.name_scope('weight_decay'): weight_decay = Standard_trainer.build_weight_decay(self, model) # Keras weight decay does not work... self.loss = mean_loss + weight_decay else: self.loss = mean_loss return