我正在训练一个简单的小型神经网络,以解决调节电动机速度的基本问题。我希望能够保存模型并退出程序,然后稍后加载并恢复训练。
以下是相关代码:
self.model = Sequential()
self.model.add(InputLayer(2))
self.model.add(Dense(6, activation='relu'))
self.model.add(Dense(9, activation='linear'))
self.model.compile(loss='mse', optimizer='adam', metrics=['mae'])
# ... Loop for training and Evaluation (Deep Q Learner) ...
learn(self.model)
self.model.save('motor_model', save_format='tf')
现在,在训练之后,我希望能够加载模型并继续训练
self.model = models.load_model('motor_model', compile=False)
# ... Loop for training and Evaluation (Deep Q Learner) ...
learn(self.model)
我第一次运行模型可以正常工作。但是,在保存和加载模型后,它不会。加载模型后,我可以调用预测函数:
prediction = self.model.predict(currentInput)
但是,当我调用预报函数时,它会失败:
self.model.fit(self.input, target_vec.reshape(-1, 9), epochs=1, verbose=0)
我得到的错误是:
2019-12-07 07:22:00.762174:W tensorflow / c / c_api.cc:326]操作 '{name:'sequential / dense / StatefulPartitionedCall'
id:33 op device:{} def:{{{node 顺序/密集/ StatefulPartitionedCall}} = StatefulPartitionedCall [Tin = [DT_FLOAT,DT
_RESOURCE,DT_RESOURCE],Tout = [DT_FLOAT,DT_FLOAT,DT_FLOAT,DT_FLOAT],_ gradient_op_type =“ PartitionedCall-298”,conf
ig =“”, config_proto =“ \ n \ 007 \ n \ 003CPU \ 020 \ 001 \ n \ 007 \ n \ 003GPU \ 020 \ 0002 \ 002J \ 0008 \ 001”, executor_type =“”,f = __ forward_re
stored_function_body_509 []](输入_1,密集/内核,密集/偏差)}}'是 运行后通过设置属性进行更改b
一个会话。此突变将无效,并会触发 未来的错误。要么不要修改节点af
运行它们或创建一个新会话。 2019-12-07 07:22:03.320478: W tensorflow / python / util / util.cc:299]集不是当前的 被认为是序列,但是
将来可能会更改,因此请考虑避免使用它们。 追溯(最近一次通话):文件 “ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, _do_call中的1363行 返回fn(* args)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, _run_fn中的第1346行 self._extend_graph()文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, _extend_graph中的第1386行 tf_session.ExtendSession(self._session)tensorflow.python.framework.errors_impl.InvalidArgumentError:节点 '训练/亚当/梯度/梯度/顺序/密度
e_1 / StatefulPartitionedCall_grad / PartitionedCall':连接到 源节点顺序/ dense_1 / S的无效输出1
tatefulPartitionedCall具有1个输出。在处理上述异常期间,发生了另一个异常:
回溯(最近一次通话最后一次):文件“ ct2.py”,第47行,在 leftController.to_position(target,overlayAction)文件“ /opt/mowzr/motor_controller.py”,第94行,位于to_position self.model.fit(self.prevInput,target_vec.reshape(-1,9),epochs = 1,verbose = 0)文件 “ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/training.py”, 766号线,适合 use_multiprocessing = use_multiprocessing)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/training_arrays.py”, 第680行,在
适合 steps_name ='steps_per_epoch')文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/training_arrays.py”, 第275行,在
model_iteration model.reset_metrics()文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/training.py”, 第953行,位于reset_m
度量 m.reset_states()文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/metrics.py”, 第209行,处于reset_states K.batch_set_value([[(v,0)for self.variables中的v])文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/backend.py”, 第3343行,在batch_set_valu
中 Ë get_session()。run(assign_ops,feed_dict = feed_dict)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/backend.py”, 第490行,在get_session中 _initialize_variables(session)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/backend.py”, _initialize_var
中的第905行 实物 [[候选人变量中的v的variables_module.is_variable_initialized(v)])文件 “ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, 956行,运行中 run_metadata_ptr)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, _run中的1179行 feed_dict_tensor,选项,run_metadata)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, _do_run中的第1357行 run_metadata)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, 1382行,_do_call 提高类型(e)(node_def,op,消息)tensorflow.python.framework.errors_impl.InvalidArgumentError:节点 '训练/亚当/梯度/梯度/顺序/密度
e_1 / StatefulPartitionedCall_grad / PartitionedCall':连接到 源节点顺序/ dense_1 / S的无效输出1
tatefulPartitionedCall具有1个输出。在处理上述异常期间,发生了另一个异常:
回溯(最近通话最近):文件 “ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, _do_call中的1363行 返回fn(* args)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, _run_fn中的第1346行 self._extend_graph()文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, _extend_graph中的第1386行 tf_session.ExtendSession(self._session)tensorflow.python.framework.errors_impl.InvalidArgumentError:节点 '训练/亚当/梯度/梯度/顺序/密度
e_1 / StatefulPartitionedCall_grad / PartitionedCall':连接到 源节点顺序/ dense_1 / S的无效输出1
tatefulPartitionedCall具有1个输出。在处理上述异常期间,发生了另一个异常:
回溯(最近一次通话最近):文件“ ct2.py”,第53行,在 leftController.saveModel()文件中的“ /opt/mowzr/motor_controller.py”行116,在saveModel中 self.model.save('motor_model',save_format ='tf')文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/engine/network.py”, 986行,保存中 签名,选项)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/saving/save.py”, 第115行,在save_model中 签名,选项)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/saving/saved_model/save.py”, 第74行,在
保存 save_lib.save(模型,文件路径,签名,选项)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/saved_model/save.py”, 保存中的第924行 object_saver.save(utils_impl.get_variables_path(export_dir))文件 “ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/training/tracking/util.py”, 1161行,保存 session = get_session()文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/training/tracking/util.py”, get_ses中的第71行
sion session = keras_backend.get_session()文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/backend.py”, 第490行,在get_session中 _initialize_variables(session)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/backend.py”, _initialize_var
中的第905行 实物 [[候选人变量中的v的variables_module.is_variable_initialized(v)])文件 “ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, 956行,运行中 run_metadata_ptr)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, _run中的1179行 feed_dict_tensor,选项,run_metadata)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, _do_run中的第1357行 run_metadata)文件“ /usr/local/lib/python3.7/dist-packages/tensorflow_core/python/client/session.py”, 1382行,_do_call 提高类型(e)(node_def,op,消息)tensorflow.python.framework.errors_impl.InvalidArgumentError:节点 '训练/亚当/梯度/梯度/顺序/密度
e_1 / StatefulPartitionedCall_grad / PartitionedCall':连接到 源节点顺序/ dense_1 / S的无效输出1
tatefulPartitionedCall具有1个输出。
答案 0 :(得分:0)
我遇到了同样的错误。
我不知道到底是什么导致了这个错误,但是有一种解决方法(虽然不是一个很漂亮的方法)。使用相同的架构创建模型,然后将权重设置为加载的模型权重:
self.model = self.create_model()
self.model.set_weights(load_model("sample.model").get_weights())