我在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)]]
我试图手动初始化失败的变量,并且之前曾经工作过一次。但是,现在有不同的变量,我甚至找不到它们。我想了解为什么会这样。
以下是full code
答案 0 :(得分:0)
Keras通常拥有自己的内置模型保存和加载方法。在训练keras模型时,你应该 使用它们而不是TF保护程序,因为keras有自己的元计算图,应该在加载模型时初始化。
以下是有关如何保存和加载keras模型的示例(从keras documentation复制而来)
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')