def predict(main_config, model_config, model, experiment_name): model = MODELS[model] model_dir = str(main_config['DATA']['model_dir']) vectorizer = DatasetVectorizer(model_dir) max_doc_len = vectorizer.max_sentence_len vocabulary_size = vectorizer.vocabulary_size model = model(max_doc_len, vocabulary_size, main_config, model_config) with tf.Session() as session: saver = tf.train.Saver() last_checkpoint = tf.train.latest_checkpoint('{}/{}'.format( model_dir, experiment_name)) saver.restore(session, last_checkpoint) while True: x1 = input('First sentence:') x2 = input('Second sentence:') x1_sen = vectorizer.vectorize(x1) x2_sen = vectorizer.vectorize(x2) feed_dict = { model.x1: x1_sen, model.x2: x2_sen, model.is_training: False } prediction = session.run([model.temp_sim], feed_dict=feed_dict) print(prediction)
def predict(main_config, model_config, model): model_name = '{}_{}'.format(model, main_config['PARAMS']['embedding_size']) model = MODELS[model_name] model_dir = str(main_config['DATA']['model_dir']) vectorizer = DatasetVectorizer(model_dir) max_doc_len = vectorizer.max_sentence_len vocabulary_size = vectorizer.vocabulary_size model = model(max_doc_len, vocabulary_size, main_config, model_config) with tf.Session() as session: saver = tf.train.Saver() last_checkpoint = tf.train.latest_checkpoint('{}/{}/model'.format( model_dir, model_name)) saver.restore(session, last_checkpoint) while True: x = input('Text:') x_sen = vectorizer.vectorize(x) feed_dict = {model.x: x_sen} prediction = session.run([model.temp_sim], feed_dict=feed_dict) print(prediction)
class MultiheadSiameseNetGuiDemo: def __init__(self, master): self.frame = master self.frame.title('Multihead Siamese Nets') sample1 = StringVar(master, value=SAMPLE_SENTENCE1) sample2 = StringVar(master, value=SAMPLE_SENTENCE2) self.first_sentence_entry = Entry( self.frame, width=50, font="Helvetica {}".format(GUI_FONT_SIZE), textvariable=sample1) self.second_sentence_entry = Entry( self.frame, width=50, font="Helvetica {}".format(GUI_FONT_SIZE), textvariable=sample2) self.predictButton = Button(self.frame, text='Predict', font="Helvetica {}".format(GUI_FONT_SIZE), command=self.predict) self.clearButton = Button(self.frame, text='Clear', command=self.clear, font="Helvetica {}".format(GUI_FONT_SIZE)) self.resultLabel = Label(self.frame, text='Result', font="Helvetica {}".format(GUI_FONT_SIZE)) self.first_sentence_label = Label( self.frame, text='Sentence 1', font="Helvetica {}".format(GUI_FONT_SIZE)) self.second_sentence_label = Label( self.frame, text='Sentence 2', font="Helvetica {}".format(GUI_FONT_SIZE)) self.main_config = init_config() self.model_dir = str(self.main_config['DATA']['model_dir']) model_dirs = [os.path.basename(x[0]) for x in os.walk(self.model_dir)] self.visualize_attentions = IntVar() self.visualize_attentions_checkbox = Checkbutton( master, text="Visualize attention weights", font="Helvetica {}".format(int(GUI_FONT_SIZE / 2)), variable=self.visualize_attentions, onvalue=1, offvalue=0) variable = StringVar(master) variable.set('Choose a model...') self.model_type = OptionMenu(master, variable, *model_dirs, command=self.load_model) self.model_type.configure(font=('Helvetica', GUI_FONT_SIZE)) self.first_sentence_entry.grid(row=0, column=1, columnspan=4) self.first_sentence_label.grid(row=0, column=0, sticky=E) self.second_sentence_entry.grid(row=1, column=1, columnspan=4) self.second_sentence_label.grid(row=1, column=0, sticky=E) self.model_type.grid(row=2, column=1, sticky=W + E, ipady=1) self.predictButton.grid(row=2, column=2, sticky=W + E, ipady=1) self.clearButton.grid(row=2, column=3, sticky=W + E, ipady=1) self.resultLabel.grid(row=2, column=4, sticky=W + E, ipady=1) self.vectorizer = DatasetVectorizer(self.model_dir) self.max_doc_len = self.vectorizer.max_sentence_len self.vocabulary_size = self.vectorizer.vocabulary_size self.session = tf.Session() self.model = None def predict(self): if self.model: sentence1 = self.first_sentence_entry.get() sentence2 = self.second_sentence_entry.get() x1_sen = self.vectorizer.vectorize(sentence1) x2_sen = self.vectorizer.vectorize(sentence2) feed_dict = { self.model.x1: x1_sen, self.model.x2: x2_sen, self.model.is_training: False } if self.visualize_attentions.get(): prediction, at1, at2 = np.squeeze( self.session.run([ self.model.predictions, self.model.debug_vars['attentions_x1'], self.model.debug_vars['attentions_x2'] ], feed_dict=feed_dict)) visualization.visualize_attention_weights(at1, sentence1) visualization.visualize_attention_weights(at2, sentence2) else: prediction = np.squeeze( self.session.run(self.model.predictions, feed_dict=feed_dict)) prediction = np.round(prediction, 2) self.resultLabel['text'] = prediction if prediction < 0.5: self.resultLabel.configure(foreground="red") else: self.resultLabel.configure(foreground="green") else: messagebox.showerror("Error", "Choose a model to make a prediction.") def clear(self): self.first_sentence_entry.delete(0, 'end') self.second_sentence_entry.delete(0, 'end') self.resultLabel['text'] = '' def load_model(self, model_name): if 'multihead' in model_name: self.visualize_attentions_checkbox.grid(row=2, column=0, sticky=W + E, ipady=1) else: self.visualize_attentions_checkbox.grid_forget() tf.reset_default_graph() self.session = tf.Session() logger.info('Loading model: %s', model_name) model = MODELS[model_name.split('_')[0]] model_config = init_config(model_name.split('_')[0]) self.model = model(self.max_doc_len, self.vocabulary_size, self.main_config, model_config) saver = tf.train.Saver() last_checkpoint = tf.train.latest_checkpoint('{}/{}'.format( self.model_dir, model_name)) saver.restore(self.session, last_checkpoint) logger.info('Loaded model from: %s', last_checkpoint)