def build_language_training_rnn(sess, hyper_params, prog_params, train_set, test_set): model = LanguageModel(hyper_params["num_layers"], hyper_params["hidden_size"], hyper_params["batch_size"], hyper_params["max_input_seq_length"], hyper_params["max_target_seq_length"], hyper_params["char_map_length"]) # Create a Dataset from the train_set and the test_set train_dataset = model.build_dataset(train_set, hyper_params["batch_size"], hyper_params["max_input_seq_length"], hyper_params["char_map"]) v_iterator = None if test_set is []: t_iterator = model.add_dataset_input(train_dataset) sess.run(t_iterator.initializer) else: test_dataset = model.build_dataset(test_set, hyper_params["batch_size"], hyper_params["max_input_seq_length"], hyper_params["char_map"]) # Build the input stream from the different datasets t_iterator, v_iterator = model.add_datasets_input(train_dataset, test_dataset) sess.run(t_iterator.initializer) sess.run(v_iterator.initializer) # Create the model model.create_training_rnn(hyper_params["dropout_input_keep_prob"], hyper_params["dropout_output_keep_prob"], hyper_params["grad_clip"], hyper_params["learning_rate"], hyper_params["lr_decay_factor"], use_iterator=True) model.add_tensorboard(sess, hyper_params["tensorboard_dir"], prog_params["tb_name"], prog_params["timeline"]) model.initialize(sess) model.restore(sess, hyper_params["checkpoint_dir"] + "/language/") # Override the learning rate if given on the command line if prog_params["learn_rate"] is not None: model.set_learning_rate(sess, prog_params["learn_rate"]) return model, t_iterator, v_iterator
def generate_text(hyper_params): with tf.Session() as sess: # Create model model = LanguageModel(hyper_params["num_layers"], hyper_params["hidden_size"], 1, 1, hyper_params["max_target_seq_length"], hyper_params["char_map_length"]) model.create_forward_rnn() model.initialize(sess) model.restore(sess, hyper_params["checkpoint_dir"] + "/language/") # Start with a letter text = "O" for _ in range(10): print(text, end="") # Convert to an one-hot encoded vector input_vec = dataprocessor.DataProcessor.get_str_to_one_hot_encoded( hyper_params["char_map"], text, add_eos=False) feat_vec = np.array(input_vec) (a, b) = feat_vec.shape feat_vec = feat_vec.reshape((a, 1, b)) prediction = model.process_input(sess, feat_vec, [1]) text = dataprocessor.DataProcessor.get_labels_str( hyper_params["char_map"], prediction[0]) print(text) return
def generate_text(hyper_params): with tf.Session() as sess: # Create model model = LanguageModel(hyper_params["num_layers"], hyper_params["hidden_size"], 1, 1, hyper_params["max_target_seq_length"], hyper_params["char_map_length"]) model.create_forward_rnn() model.initialize(sess) model.restore(sess, hyper_params["checkpoint_dir"] + "/language/") # Start with a letter text = "O" for _ in range(10): print(text, end="") # Convert to an one-hot encoded vector input_vec = dataprocessor.DataProcessor.get_str_to_one_hot_encoded(hyper_params["char_map"], text, add_eos=False) feat_vec = np.array(input_vec) (a, b) = feat_vec.shape feat_vec = feat_vec.reshape((a, 1, b)) prediction = model.process_input(sess, feat_vec, [1]) text = dataprocessor.DataProcessor.get_labels_str(hyper_params["char_map"], prediction[0]) print(text) return