def train( self, x_labelled, y, x_unlabelled, epochs, x_valid, y_valid, print_every = 1, learning_rate = 3e-4, beta1 = 0.9, beta2 = 0.999, seed = 31415, stop_iter = 100, save_path = None, load_path = None ): ''' Session and Summary ''' if save_path is None: self.save_path = 'checkpoints/model_GC_{}-{}-{}_{}.cpkt'.format( self.num_lab,learning_rate,self.batch_size,time.time()) else: self.save_path = save_path np.random.seed(seed) tf.set_random_seed(seed) with self.G.as_default(): self.optimiser = tf.train.AdamOptimizer( learning_rate = learning_rate, beta1 = beta1, beta2 = beta2 ) self.train_op = self.optimiser.minimize( self.cost ) init = tf.initialize_all_variables() self._test_vars = None _data_labelled = np.hstack( [x_labelled, y] ) _data_unlabelled = x_unlabelled x_valid_mu, x_valid_lsgms = x_valid[ :, :int(self.dim_x) ], x_valid[ :, int(self.dim_x): int(2*self.dim_x) ] with self.session as sess: sess.run(init) if load_path == 'default': self.saver.restore( sess, self.save_path ) elif load_path is not None: self.saver.restore( sess, load_path ) best_eval_accuracy = 0. best_train_accuracy = 0. stop_counter = 0 print("****lab_clf", self.weights['lab_clf']) print("****lab_recon_clf", self.weights['lab_recon_clf']) print("****ulab_clf", self.weights['ulab_clf']) print("****ulab_recon_clf", self.weights['ulab_recon_clf']) print("****L_loss", self.weights['L_loss']) print("****U_loss", self.weights['U_loss']) for epoch in range(epochs): ''' Shuffle Data ''' np.random.shuffle( _data_labelled ) np.random.shuffle( _data_unlabelled ) ''' Training ''' for x_l_mu, x_l_lsgms, y, x_u_mu, x_u_lsgms in utils.feed_numpy_semisupervised( self.num_lab_batch, self.num_ulab_batch, _data_labelled[:,:2*self.dim_x], _data_labelled[:,2*self.dim_x:],_data_unlabelled ): training_result = sess.run( [self.train_op, self.cost], feed_dict = { self.x_labelled_mu: x_l_mu, self.x_labelled_lsgms: x_l_lsgms, self.y_lab: y, self.x_unlabelled_mu: x_u_mu, self.x_unlabelled_lsgms: x_u_lsgms} ) training_cost = training_result[1] ''' Evaluation ''' stop_counter += 1 if epoch % print_every == 0: test_vars = tf.get_collection(bookkeeper.GraphKeys.TEST_VARIABLES) if test_vars: if test_vars != self._test_vars: self._test_vars = list(test_vars) self._test_var_init_op = tf.initialize_variables(test_vars) self._test_var_init_op.run() eval_accuracy, eval_cross_entropy = \ sess.run( [self.eval_accuracy, self.eval_cross_entropy], feed_dict = { self.x_labelled_mu: x_valid_mu, self.x_labelled_lsgms: x_valid_lsgms, self.y_lab: y_valid } ) if eval_accuracy > best_eval_accuracy: best_eval_accuracy = eval_accuracy self.saver.save( sess, self.save_path ) stop_counter = 0 print("accuracy update: ", best_eval_accuracy) utils.print_metrics( epoch+1, ['Training', 'cost', training_cost], ['Validation', 'accuracy', eval_accuracy], ['Validation', 'cross-entropy', eval_cross_entropy] ) if stop_counter >= stop_iter: print('Stopping GC training') print('No change in validation accuracy for {} iterations'.format(stop_iter)) print('Best validation accuracy: {}'.format(best_eval_accuracy)) print('Model saved in {}'.format(self.save_path)) break
def train( self, x_labelled, y, x_unlabelled, epochs, x_valid, y_valid, print_every = 1, learning_rate = 3e-4, beta1 = 0.9, beta2 = 0.999, seed = 31415, stop_iter = 100, save_path = None, load_path = None ): ''' Session and Summary ''' if save_path is None: self.save_path = 'checkpoints/model_GC_{}-{}-{}_{}.cpkt'.format( self.num_lab,learning_rate,self.batch_size,time.time()) else: self.save_path = save_path np.random.seed(seed) tf.set_random_seed(seed) with self.G.as_default(): self.optimiser = tf.train.AdamOptimizer( learning_rate = learning_rate, beta1 = beta1, beta2 = beta2 ) self.train_op = self.optimiser.minimize( self.cost ) init = tf.initialize_all_variables() self._test_vars = None _data_labelled = np.hstack( [x_labelled, y] ) _data_unlabelled = x_unlabelled x_valid_mu, x_valid_lsgms = x_valid[ :, :self.dim_x ], x_valid[ :, self.dim_x:2*self.dim_x ] with self.session as sess: sess.run(init) if load_path == 'default': self.saver.restore( sess, self.save_path ) elif load_path is not None: self.saver.restore( sess, load_path ) best_eval_accuracy = 0. stop_counter = 0 for epoch in range(epochs): ''' Shuffle Data ''' np.random.shuffle( _data_labelled ) np.random.shuffle( _data_unlabelled ) ''' Training ''' for x_l_mu, x_l_lsgms, y, x_u_mu, x_u_lsgms in utils.feed_numpy_semisupervised( self.num_lab_batch, self.num_ulab_batch, _data_labelled[:,:2*self.dim_x], _data_labelled[:,2*self.dim_x:],_data_unlabelled ): training_result = sess.run( [self.train_op, self.cost], feed_dict = { self.x_labelled_mu: x_l_mu, self.x_labelled_lsgms: x_l_lsgms, self.y_lab: y, self.x_unlabelled_mu: x_u_mu, self.x_unlabelled_lsgms: x_u_lsgms } ) training_cost = training_result[1] ''' Evaluation ''' stop_counter += 1 if epoch % print_every == 0: test_vars = tf.get_collection(bookkeeper.GraphKeys.TEST_VARIABLES) if test_vars: if test_vars != self._test_vars: self._test_vars = list(test_vars) self._test_var_init_op = tf.initialize_variables(test_vars) self._test_var_init_op.run() eval_accuracy, eval_cross_entropy = \ sess.run( [self.eval_accuracy, self.eval_cross_entropy], feed_dict = { self.x_labelled_mu: x_valid_mu, self.x_labelled_lsgms: x_valid_lsgms, self.y_lab: y_valid } ) if eval_accuracy > best_eval_accuracy: best_eval_accuracy = eval_accuracy self.saver.save( sess, self.save_path ) stop_counter = 0 utils.print_metrics( epoch+1, ['Training', 'cost', training_cost], ['Validation', 'accuracy', eval_accuracy], ['Validation', 'cross-entropy', eval_cross_entropy] ) if stop_counter >= stop_iter: print('Stopping GC training') print('No change in validation accuracy for {} iterations'.format(stop_iter)) print('Best validation accuracy: {}'.format(best_eval_accuracy)) print('Model saved in {}'.format(self.save_path)) break