encoding_embedding_size = 64 decoding_embedding_size = 64 # RNN Size rnn_size = 64 # Number of Layers num_layers = 2 # Learning Rate learning_rate = 0.0001 # Dropout Keep Probability keep_probability = 0.8 display_step = 10 save_path = 'checkpoints/sup/actor/dev' save_path_critic = 'checkpoints/sup/critic/dev' (source_int_text, target_int_text), (source_vocab_to_int, target_vocab_to_int), (source_int_to_vocab, target_int_to_vocab) = util.load_preprocess('preprocess.p') load_path_actor = util.load_params('params_actor_sup.p') source_train = util.read_list('source_train.npy') target_train = util.read_list('target_train.npy') valid_size = batch_size * 10 train_source = source_train[valid_size:] train_target = target_train[valid_size:] valid_source = source_train[:valid_size] valid_target = target_train[:valid_size] test_acc_list = [] train_graph = tf.Graph() critic_graph = tf.Graph() actor_graph = tf.Graph()
import tensorflow as tf import numpy as np import util _, (source_vocab_to_int, target_vocab_to_int), (source_int_to_vocab, target_int_to_vocab) = util.load_preprocess() load_path = util.load_params() # To feed a sentence into the model for translation, you first need to preprocess it def sentence_to_seq(sentence, vocab_to_int): """ Convert a sentence to a sequence of ids :param sentence: String :param vocab_to_int: Dictionary to go from the words to an id :return: List of word ids """ sentence = sentence.lower() sentence_list = sentence.split() word_ids = [] for val in sentence_list: word_ids.append(vocab_to_int.get(val, vocab_to_int['<UNK>'])) return word_ids # Translate translate_sentence = 'he saw a old yellow truck .' translate_sentence = sentence_to_seq(translate_sentence, source_vocab_to_int)
import tensorflow as tf import numpy as np import util _, vocab_to_int, int_to_vocab, token_dict = util.load_preprocess() seq_length, load_dir = util.load_params() def get_tensors(loaded_graph): """ Get input, initial state, final state, and probabilities tensor from <loaded_graph> :param loaded_graph: TensorFlow graph loaded from file :return: Tuple (InputTensor, InitialStateTensor, FinalStateTensor, ProbsTensor) """ with loaded_graph.as_default(): inputs = tf.get_default_graph().get_tensor_by_name('input:0') initial_state = tf.get_default_graph().get_tensor_by_name( 'initial_state:0') fianl_state = tf.get_default_graph().get_tensor_by_name( 'final_state:0') prob = tf.get_default_graph().get_tensor_by_name('probs:0') return inputs, initial_state, fianl_state, prob def pick_word(probabilities, int_to_vocab): """ Pick the next word in the generated text :param probabilities: Probabilites of the next word :param int_to_vocab: Dictionary of word ids as the keys and words as the values :return: String of the predicted word """
# Number of Layers num_layers = args.num_layers # Embedding Size encoding_embedding_size = args.encoding_embedding_size decoding_embedding_size = args.decoding_embedding_size # Learning Rate learning_rate = args.learning_rate # Dropout Keep Probability keep_probability = args.keep_prob display_step = args.display_step # build the graph save_path = args.save_path (source_int_text, target_int_text), (source_vocab_to_int, target_vocab_to_int), _ = util.load_preprocess() max_target_sentence_length = max( [len(sentence) for sentence in source_int_text]) model = models.SeqToSeq(rnn_size, num_layers, len(target_vocab_to_int), len(source_vocab_to_int), batch_size, encoding_embedding_size, decoding_embedding_size, keep_prob) train_graph = tf.Graph() with train_graph.as_default(): input_data, targets, lr, keep_prob, target_sequence_length, max_target_sequence_length, source_sequence_length = model.model_inputs( ) #sequence_length = tf.placeholder_with_default(max_target_sentence_length, None, name='sequence_length') input_shape = tf.shape(input_data)