Last active
October 7, 2019 10:22
-
-
Save VictorSaenger/86be4c452371daf45b6d45daf86ef500 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Load the encoder: | |
| g = tf.Graph() | |
| with g.as_default(): | |
| text_input = tf.placeholder(dtype=tf.string, shape=[None]) | |
| embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder-large/3") | |
| embedded_text = embed(text_input) | |
| init_op = tf.group([tf.global_variables_initializer(), tf.tables_initializer()]) | |
| g.finalize() | |
| # Initialize session: | |
| session = tf.Session(graph=g) | |
| session.run(init_op) | |
| #Function to compute all embeddings for each sentence: | |
| #Be patient, takes a little while: | |
| def similarity_matrix(merge_list): | |
| #initialize distance array: | |
| #initialize embeddings array: | |
| emb_all = np.zeros([len(merge_list),512]) | |
| #Outer for loop: | |
| for i in range(0,len(merge_list)): | |
| #Here is where we run the previously started session, so it is important to run previous step succesfully: | |
| i_emb = session.run(embedded_text, feed_dict={text_input: [merge_list[i]]}) | |
| emb_all[i,:] = i_emb | |
| return emb_all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment