# Load the saved meta graph and restore variables saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file)) saver.restore(sess, checkpoint_file) # Get the placeholders from the graph by name inputs = graph.get_operation_by_name("inputs").outputs[0] labels = graph.get_operation_by_name("labels").outputs[0] vocab_embedding = graph.get_operation_by_name("vocab_embedding").outputs[0] discard_last_prediction = graph.get_operation_by_name("discard_last_prediction").outputs[0] # Tensors we want to evaluate predictions = graph.get_operation_by_name("accuracy/predictions").outputs[0] perplexity = graph.get_operation_by_name("perplexity/perplexity").outputs[0] # Generate batches for one epoch batches = preprocess_helper.batch_iter(list(zip(x_test, y_test)), FLAGS.batch_size, 1, shuffle=False) # Construct the embedding matrix vocab_emb = np.zeros(shape=(FLAGS.vocabulary_size, FLAGS.embedding_dimension)) w2v_model = gensim.models.KeyedVectors.load_word2vec_format(FLAGS.path_to_word2vec, binary=False) for tok, idx in vocab.items(): if FLAGS.use_word2vec_emb and tok in w2v_model.vocab: vocab_emb[idx] = w2v_model[tok] else: vocab_emb[idx] = generated_embeddings[tok] # Collect the predictions here all_perplexity = [] if(FLAGS.verbose_for_experiments): print("verbose_for_experiments: Only the perplexity will be shown for each sentence")
val_sample_index = -1 * int(FLAGS.val_sample_percentage * float(len(labels))) x_train, x_val = data[:val_sample_index], data[val_sample_index:] y_train, y_val = labels[:val_sample_index], labels[val_sample_index:] # Summary of the loaded data print('Loaded: ', len(x_train), ' samples for training') print('Loaded: ', len(x_val), ' samples for validation') print('Training input has shape: ', np.shape(x_train)) print('Validation input has shape: ', np.shape(x_val)) print('Training labels has shape: ', np.shape(y_train)) print('Validation labels has shape: ', np.shape(y_val)) # Generate training batches batches = preprocess_helper.batch_iter(list(zip(x_train, y_train)), FLAGS.batch_size, FLAGS.num_epochs) print("Loading and preprocessing done \n") # Define the model and start training with tf.Graph().as_default(): session_conf = tf.ConfigProto( allow_soft_placement=FLAGS.allow_soft_placement, log_device_placement=FLAGS.log_device_placement, inter_op_parallelism_threads=FLAGS.inter_op_parallelism_threads, intra_op_parallelism_threads=FLAGS.intra_op_parallelism_threads) sess = tf.Session(config=session_conf) with sess.as_default(): # Initialize model lstm_language_model = LSTMLanguageModel( vocabulary_size=FLAGS.vocabulary_size,