def single_train(self): self.x, self.y = get_next_batch() self.global_step = tf.get_variable('global_step', initializer=0, dtype=tf.int32, trainable=False) self.lr = tf.train.exponential_decay(learning_rate=hp.LR, global_step=self.global_step, decay_rate=hp.DECAY_RATE, decay_steps=hp.DECAY_STEPS) self.optimizer = tf.train.AdamOptimizer(self.lr) with tf.variable_scope(self.scope_name, reuse=self.reuse): self.y_hat = lstm_3_layers(self.x, num_units=hp.UNITS, bidirection=False, scope='lstm_3_layers') # [N, U] self.y_hat = tf.layers.dense(self.y_hat, units=hp.LABEL_SIZE * 2, activation=tf.nn.tanh, name='dense_1') # [N, L*2] self.y_hat = tf.layers.dense(self.y_hat, units=hp.LABEL_SIZE, activation=tf.nn.sigmoid, name='output_1') # [N, L] self.loss = tf.reduce_sum( tf.reduce_mean(tf.square(self.y - self.y_hat))) self.grads = self.optimizer.compute_gradients(self.loss) clipped = [] for grad, var in self.grads: grad = tf.clip_by_norm(grad, 5.) clipped.append((grad, var)) self.train_op = self.optimizer.apply_gradients( clipped, global_step=self.global_step)
def train(self): self.text, self.refer_mel, self.mel, self.linear = get_next_batch() self.encoder_inputs = embedding(self.text, scope='embedding', reuse=self.reuse) self.decoder_inputs = tf.concat((tf.zeros_like(self.mel[:, :1, :]), self.mel[:, :-1, :]), 1) self.decoder_inputs = self.decoder_inputs[:, :, -hp.N_MELS:] with tf.variable_scope(self.scope_name): self.text_outputs = encoder(self.encoder_inputs, is_training=self.is_training) self.vae_outputs, self.mu, self.log_var = vae(self.refer_mel, is_training=self.is_training) self.encoder_outputs = self.text_outputs + self.vae_outputs self.mel_hat, self.alignments = decoder(self.decoder_inputs, self.encoder_outputs, is_training=self.is_training) self.linear_hat = postnet(self.mel_hat, is_training=self.is_training) if self.mode in ['train', 'eval']: self.global_step = tf.get_variable('global_step', initializer=0, dtype=tf.int32, trainable=False) self.lr = tf.train.exponential_decay(learning_rate=hp.LR, global_step=self.global_step, decay_steps=hp.DECAY_STEPS, decay_rate=hp.DECAY_RATE) self.optimizer = tf.train.AdamOptimizer(self.lr) self.mel_loss = tf.reduce_mean(tf.abs(self.mel_hat - self.mel)) self.linear_loss = tf.reduce_mean(tf.abs(self.linear_hat - self.linear)) self.kl_loss = - 0.5 * tf.reduce_sum(1 + self.log_var - tf.pow(self.mu, 2) - tf.exp(self.log_var)) self.vae_loss_weight = control_weight(self.global_step) self.loss = self.mel_loss + self.linear_loss + self.vae_loss_weight * self.kl_loss self.
def test(self): with tf.device('/cpu:0'): self.x, self.y = get_next_batch() with tf.variable_scope(self.scope_name, reuse=self.reuse): self.y_hat = lstm_3_layers(self.x, num_units=hp.UNITS, bidirection=False, scope='lstm_3_layers') # [N, U] self.y_hat = tf.layers.dense(self.y_hat, units=hp.LABEL_SIZE * 2, activation=tf.nn.tanh, name='dense_1') # [N, L*2] self.y_hat = tf.layers.dense(self.y_hat, units=hp.LABEL_SIZE, activation=tf.nn.sigmoid, name='output_1') # [N, L]
def multi_train(self): def _assign_to_device(device, ps_device='/cpu:0'): PS_OPS = ['Variable', 'VariableV2', 'AutoReloadVariable'] def _assign(op): node_def = op if isinstance(op, tf.NodeDef) else op.node_def if node_def.op in PS_OPS: return '/' + ps_device else: return device return _assign def _average_gradients(tower_grads): average_grads = [] for grad_and_vars in zip(*tower_grads): grads = [] for g, _ in grad_and_vars: expanded_g = tf.expand_dims(g, 0) grads.append(expanded_g) grad = tf.concat(grads, 0) grad = tf.reduce_mean(grad, 0) v = grad_and_vars[0][1] grad_and_var = (grad, v) average_grads.append(grad_and_var) return average_grads with tf.device('/cpu:0'): self.x, self.y, self.mask = get_next_batch() self.tower_grads = [] self.global_step = tf.get_variable('global_step', initializer=0, dtype=tf.int32, trainable=False) self.lr = tf.train.exponential_decay(hp.LR, global_step=self.global_step, decay_steps=hp.DECAY_STEPS, decay_rate=hp.DECAY_RATE) self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr) gpu_nums = len(hp.GPU_IDS) per_batch = hp.BATCH_SIZE // gpu_nums with tf.variable_scope(self.scope_name, reuse=self.reuse): for i in range(gpu_nums): with tf.device( _assign_to_device('/gpu:{}'.format(hp.GPU_IDS[i]), ps_device='/cpu:0')): self._x = self.x[i * per_batch:(i + 1) * per_batch] self._y = self.y[i * per_batch:(i + 1) * per_batch] self._mask = self.mask[i * per_batch:(i + 1) * per_batch] self.y_hat = lstm_3_layers( self.x, num_units=hp.UNITS, bidirection=False, scope='lstm_3_layers') # [N, U] self.y_hat = tf.layers.dense( self.y_hat, units=hp.LABEL_SIZE * 2, activation=tf.nn.tanh, name='dense_1') # [N, L*2] self.y_hat = tf.layers.dense(self.y_hat, units=hp.LABEL_SIZE, activation=tf.nn.sigmoid, name='output_1') # [N, L] tf.get_variable_scope().reuse_variables() # loss self.loss = tf.reduce_sum( tf.reduce_mean(tf.square(self.y - self.y_hat))) self.grads = self.optimizer.compute_gradients( self.loss) self.tower_grads.append(self.grads) self.tower_grads = _average_gradients(self.tower_grads) clipped = [] for grad, var in self.tower_grads: grad = tf.clip_by_norm(grad, 5.) clipped.append((grad, var)) self.train_op = self.optimizer.apply_gradients( clipped, global_step=self.global_step)
def train(self): self.ori_spk, self.ori_mel, self.aim_spk, self.aim_mel = get_next_batch() self.flow() self.update()
def train(self): self.ori_spk, self.ori_feat, self.aim_spk, self.aim_feat, \ self.t_G, self.t_D_fake, self.t_D_real = get_next_batch() self.flow() self.update()