-
Notifications
You must be signed in to change notification settings - Fork 21
/
loadmodel.py
52 lines (40 loc) · 1.93 KB
/
loadmodel.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
41
42
43
44
45
46
47
48
49
50
51
52
import tensorflow as tf
from tensorflow.python.platform import gfile
# only for bugfix
tf.contrib.rnn
output_graph_path = './model.pb'
graph = tf.Graph()
with gfile.FastGFile(output_graph_path, 'rb') as f:
output_graph_def = tf.GraphDef()
output_graph_def.ParseFromString(f.read())
with graph.as_default():
tf.import_graph_def(output_graph_def, name="")
with tf.Session(graph=graph) as sess:
init_all_tables = graph.get_operation_by_name('init_all_tables')
sess.run(init_all_tables)
# sess.run(tf.global_variables_initializer())
# sess.run(tf.local_variables_initializer())
# 得到当前图有几个操作节点
print("%d ops in the final graph." % len(output_graph_def.node))
tensor_name = [tensor.name for tensor in output_graph_def.node]
print(tensor_name)
print('---------------------------')
Placeholder = sess.graph.get_tensor_by_name('Placeholder:0')
Placeholder_1 = sess.graph.get_tensor_by_name('Placeholder_1:0')
# embedding层的输出
embedding_out = sess.graph.get_tensor_by_name('embedding_lookup:0')
enbedding_transpose = sess.graph.get_tensor_by_name('transpose:0')
# BiLSTM层的输出
BiLSTM_out = sess.graph.get_tensor_by_name('concat:0')
BiLSTM_transpose_1 = sess.graph.get_tensor_by_name('transpose_1:0')
a = sess.graph.get_tensor_by_name('Variable_1:0')
a_array = a.eval(session=sess)
for i in a_array[:1]:
print(i)
print('#####################')
input_words = [['唱', '一', '首', '不', '消', '失', '的', '回', '忆']]
input_words_len = [9]
b = sess.graph.get_tensor_by_name('hash_table_Lookup/hash_table_Lookup/LookupTableFindV2:0')
b = sess.run(b, {Placeholder: input_words, Placeholder_1: input_words_len})
for i in b:
print(i)