def __init__(self): # Input shape #self.img_shape = (512,) self.latent_dim = 87 self.input_dim = 512 self.voc_size = 0 optimizer = Adam(0.0002, 0.5) self.parser = data_utils.DataParser(FLAGS.source_dir, use_tokens=False, verbose=FLAGS.verbose) # Build and compile the discriminator self.discriminator = self.build_discriminator() self.discriminator.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy']) print("+++++++++++++++") # Build the generator self.generator = self.build_generator() # The generator takes noise as input and generates imgs z = Input(shape=(self.latent_dim,)) img = self.generator(z) # For the combined model we will only train the generator self.discriminator.trainable = False # The discriminator takes generated images as input and determines validity valid = self.discriminator(img) # The combined model (stacked generator and discriminator) # Trains the generator to fool the discriminator self.combined = Model(z, valid) self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)
def main(_): logging.basicConfig(level=logging.DEBUG) if not os.path.exists(FLAGS.logdir): os.makedirs(FLAGS.logdir) if FLAGS.tokenization == 'token': use_tokens = True elif FLAGS.tokenization == 'char': use_tokens = False else: raise ValueError('Unknown tokenization mode:', FLAGS.tokenization) # Parse the training and validation data. parser = data_utils.DataParser(FLAGS.source_dir, use_tokens=use_tokens, verbose=FLAGS.verbose) # Print useful stats about the parsed data. if FLAGS.verbose: logging.info('Training data stats:') parser.display_stats(parser.train_conjectures) logging.info('---') logging.info('Validation data stats:') parser.display_stats(parser.val_conjectures) voc_size = len(parser.vocabulary) + 1 if FLAGS.task_name == 'conditioned_classification': # Get the function for building the model, and the encoding to use. make_model, encoding = conditioned_classification_models.MODELS.get( FLAGS.model_name, None) if not make_model: raise ValueError('Unknown model:', FLAGS.model_name) # Instantiate a generator that will yield batches of training data. train_generator = parser.training_steps_and_conjectures_generator( encoding=encoding, max_len=FLAGS.max_len, batch_size=FLAGS.batch_size) # Instantiate a generator that will yield batches of validation data. val_generator = parser.validation_steps_and_conjectures_generator( encoding=encoding, max_len=FLAGS.max_len, batch_size=FLAGS.batch_size) elif FLAGS.task_name == 'unconditioned_classification': make_model, encoding = unconditioned_classification_models.MODELS.get( FLAGS.model_name, None) if not make_model: raise ValueError('Unknown model:', FLAGS.model_name) train_generator = parser.training_steps_generator( encoding=encoding, max_len=FLAGS.max_len, batch_size=FLAGS.batch_size) val_generator = parser.validation_steps_generator( encoding=encoding, max_len=FLAGS.max_len, batch_size=FLAGS.batch_size) else: raise ValueError('Unknown task_name:', FLAGS.task_name) if FLAGS.checkpoint_path: # Optionally load an existing saved model. model = keras.models.load_model(FLAGS.checkpoint_path) else: # Instantiate a fresh model. model = make_model(voc_size, FLAGS.max_len) model.summary() model.compile(optimizer=keras.optimizers.RMSprop(lr=0.0001), loss='binary_crossentropy', metrics=['acc']) # Define a callback for saving the model to the log directory. checkpoint_path = os.path.join(FLAGS.logdir, FLAGS.model_name + '.h5') checkpointer = keras.callbacks.ModelCheckpoint( checkpoint_path, save_best_only=True) # Define a callback for writing TensorBoard logs to the log directory. tensorboard_vis = keras.callbacks.TensorBoard(log_dir=FLAGS.logdir) logging.info('Fit model...') history = model.fit_generator(train_generator, samples_per_epoch=FLAGS.samples_per_epoch, validation_data=val_generator, nb_epoch=FLAGS.epochs, nb_val_samples=FLAGS.val_samples, pickle_safe=True, nb_worker=FLAGS.data_parsing_workers, verbose=FLAGS.verbose, callbacks=[checkpointer, tensorboard_vis]) # Save training history to a JSON file. f = open(os.path.join(FLAGS.logdir, 'history.json'), 'w') f.write(json.dumps(history.history)) f.close()
tf.app.flags.DEFINE_integer( 'samples_per_epoch', 12800, 'Number of random step statements to draw for ' 'training at each epoch.') tf.app.flags.DEFINE_integer( 'val_samples', 246912, 'Number of (ordered) step statements to draw for ' 'validation.') tf.app.flags.DEFINE_integer('epochs', 40, 'Number of epochs to train.') tf.app.flags.DEFINE_integer('verbose', 1, 'Verbosity mode (0, 1 or 2).') tf.app.flags.DEFINE_string('checkpoint_path', '', 'Path to checkpoint to (re)start from.') tf.app.flags.DEFINE_integer( 'data_parsing_workers', 4, 'Number of threads to use to generate input data.') parser = data_utils.DataParser(FLAGS.source_dir, use_tokens=False, verbose=FLAGS.verbose) conj_index, step_index, total = 0, 0, 0 epoch = 100000 num = 0 model = load_model('myDiscriminator_model.h5') model.compile(optimizer=keras.optimizers.RMSprop(lr=0.0001), loss='binary_crossentropy', metrics=['acc']) for i in range(epoch): (X_val, _), (conj_index, step_index) = parser.draw_batch_of_steps_in_order( conj_index, step_index, 'val', 'integer', 512, 128) each = sum(model.predict_on_batch(X_val) / 128)