2017-08-04 95 views
0

我已經創造了Keras一個模型,然後我初始化通過調用節能模式和初始化中Keras

session=tf.Session() 
session.run(tf.global_variables_initializer()) 

訓練結束後,我試圖通過運行

saver = tf.train.Saver() 
saver.save(session, "action_inference_cart_pole_plan16_5000episode.ckpt") 

但是保存模型,它不斷返回這個錯誤

FailedPreconditionError: Attempting to use uninitialized value dense_241/kernel 
[[Node: dense_241/kernel/_21554 = _Send[T=DT_FLOAT, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_1854_dense_241/kernel", _device="/job:localhost/replica:0/task:0/gpu:0"](dense_241/kernel)]] 
[[Node: dense_284/bias/_21741 = _Recv[_start_time=0, client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_1947_dense_284/bias", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](^_arg_save_15/Const_0_0, ^save_15/SaveV2/tensor_names, ^save_15/SaveV2/shape_and_slices)]] 

我試圖手動初始化失敗的變量,曾經工作過一次。但是,現在有不同的變量,我甚至找不到它們。我想了解爲什麼會發生這種情況。

下面是完整的代碼

https://gist.github.com/guillefix/4c20284ca1e0d87dfab1dfde11ef9ea6

+0

您可以添加代碼,請一個例子(從keras documentation複製)? – Nain

+0

我用代碼添加了一個要點鏈接 – guillefix

回答

0

Keras通常有它自己的內置模型保存和載入的方法。在訓練keras模型時,您應該使用它們而不是TF保護程序,因爲keras有它自己的元計算圖,應該在加載模型時初始化它。

這裏是如何保存和載入keras模型

from keras.models import load_model 

model.save('my_model.h5') # creates a HDF5 file 'my_model.h5' 
del model # deletes the existing model 

# returns a compiled model 
# identical to the previous one 
model = load_model('my_model.h5')