-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_test.py
40 lines (26 loc) · 1.09 KB
/
convert_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
This script is used to test how to implement
a string -> list of ids using tensorflow,
instead of using python code to be able to
embedd the decoding into the model
"""
import tensorflow as tf
def test():
# First build the model
# An input to take the entire sentence
input_sentence = tf.placeholder(tf.string, name='user_input')
input_sentence_array = tf.reshape(input_sentence, [1])
# Split the input into tokens
input_tokens = tf.string_split(input_sentence_array, delimiter=' ')
mapping_strings = tf.constant(['UNK', 'a','b','c','d','e'])
table = tf.contrib.lookup.index_table_from_tensor(mapping=mapping_strings, num_oov_buckets=0, default_value=0)
# map the tokens into IDs
input_ids = table.lookup(input_tokens.values)
# Now test the model:
with tf.Session() as session:
text_input = 'a c d e b a hejsan vad händer idag? a'
tf.tables_initializer().run(session=session)
result = session.run(input_ids, feed_dict={input_sentence: text_input})
print("Result:", result)
if __name__ == '__main__':
test()