0
我嘗試通過import_graph_def()
讀取RNN網絡並進行推理。 但我不能使用tf.trainable_variables()
來獲取任何變量。Tensorflow,使用import_graph_def()加載模型錯誤
在下面的代碼,tf.trainable_variables()
返回[](帶有沒有列表) 此外,當我使用saver = tf.train.Saver()
,tensorflow報告「沒有變量保存」
def eval_on_test(graph_path):
batch_size = 80
train_begin = 0
train_end = 3000
with tf.Graph().as_default() as graph:
with open(graph_path, 'rb') as f:
tf_graph = tf.GraphDef()
print("Loading graph_def from {}".format(graph_path))
tf_graph.ParseFromString(f.read())
return_elements = tf.import_graph_def(tf_graph, name="", return_elements=['input_x:0', 'output_y:0', 'pred:0', 'loss:0'])
X = return_elements[0]
Y = return_elements[1]
pred = return_elements[2]
loss = return_elements[3]
tf_config = tf.ConfigProto()
tf_config.gpu_options.allow_growth = True
print("graph loaded, start testing")
with tf.Session(config=tf_config) as sess:
init_op = sess.graph.get_operation_by_name('init')
sess.run(init_op)
print(tf.trainable_variables())
batch_index,train_x,train_y=get_train_data(batch_size,time_step,train_begin,train_end)
for batch in range(len(batch_index)-1):
loss_ = sess.run(loss, feed_dict={X:train_x[batch_index[batch]:batch_index[batch+1]],Y:train_y[batch_index[batch]:batch_index[batch+1]]})
print(batch, loss_)
任何幫助,將不勝感激。
感謝您的信息。這解決了我的問題。我試圖用graphdef保存和恢復圖形,該後解決問題,https://www.tensorflow.org/extend/tool_developers/ – zhangc