Session-like 處理初始化、恢複和掛鉤的對象。
用法
tf.compat.v1.train.MonitoredSession(
session_creator=None, hooks=None, stop_grace_period_secs=120
)
參數
-
session_creator
用於創建會話的工廠對象。通常是ChiefSessionCreator
,這是默認值。 -
hooks
`SessionRunHook' 對象的可迭代對象。
返回
- 一個 MonitoredSession 對象。
屬性
-
graph
在此會話中啟動的圖表。
遷移到 TF2
警告:這個 API 是為 TensorFlow v1 設計的。繼續閱讀有關如何從該 API 遷移到本機 TensorFlow v2 等效項的詳細信息。見TensorFlow v1 到 TensorFlow v2 遷移指南有關如何遷移其餘代碼的說明。
此 API 與 Eager Execution 不兼容,並且tf.function.要遷移到 TF2,請重寫代碼以兼容 Eager Execution。檢查遷移指南更換Session.run
調用。在 Keras 中,會話掛鉤可以由回調代替,例如測井鉤筆記本更多詳情請閱讀使用 tf.function 獲得更好的性能.
示例用法:
saver_hook = CheckpointSaverHook(...)
summary_hook = SummarySaverHook(...)
with MonitoredSession(session_creator=ChiefSessionCreator(...),
hooks=[saver_hook, summary_hook]) as sess:
while not sess.should_stop():
sess.run(train_op)
初始化:在創建時,受監控的會話按給定順序執行以下操作:
- 為每個給定的鉤子調用
hook.begin()
- 通過
scaffold.finalize()
完成圖形 - 創建會話
- 通過
Scaffold
提供的初始化操作初始化模型 - 如果存在檢查點,則恢複變量
- 啟動隊列跑步者
- 調用
hook.after_create_session()
運行:當run()
被調用時,被監控的會話做以下事情:
- 調用
hook.before_run()
- 使用合並的提取和 feed_dict 調用 TensorFlow
session.run()
- 調用
hook.after_run()
- 返回用戶詢問的
session.run()
的結果 - 如果發生
AbortedError
或UnavailableError
,它會在再次執行run() 調用之前恢複或重新初始化會話
退出:在 close()
,受監控的會話按順序執行以下操作:
- 調用
hook.end()
- 關閉隊列運行器和會話
- 抑製
OutOfRange
錯誤,如果 monitored_session 用作上下文,則表明所有輸入都已處理
如何設置tf.compat.v1.Session
參數:
- 在大多數情況下,您可以按如下方式設置會話參數:
MonitoredSession(
session_creator=ChiefSessionCreator(master=..., config=...))
- 在非首席員工的分布式設置中,您可以使用以下內容:
MonitoredSession(
session_creator=WorkerSessionCreator(master=..., config=...))
請參閱 MonitoredTrainingSession
以獲取基於主管或工人的示例用法。
注意:這不是 tf.compat.v1.Session
。例如,它不能執行以下操作:
- 它不能設置為默認會話。
- 它無法發送到 saver.save。
- 它無法發送到 tf.train.start_queue_runners。
子類
相關用法
- Python tf.compat.v1.train.MonitoredSession.run_step_fn用法及代碼示例
- Python tf.compat.v1.train.MomentumOptimizer用法及代碼示例
- Python tf.compat.v1.train.MomentumOptimizer.compute_gradients用法及代碼示例
- Python tf.compat.v1.train.FtrlOptimizer.compute_gradients用法及代碼示例
- Python tf.compat.v1.train.get_or_create_global_step用法及代碼示例
- Python tf.compat.v1.train.cosine_decay_restarts用法及代碼示例
- Python tf.compat.v1.train.Optimizer用法及代碼示例
- Python tf.compat.v1.train.AdagradOptimizer.compute_gradients用法及代碼示例
- Python tf.compat.v1.train.init_from_checkpoint用法及代碼示例
- Python tf.compat.v1.train.Checkpoint用法及代碼示例
- Python tf.compat.v1.train.Supervisor.managed_session用法及代碼示例
- Python tf.compat.v1.train.Checkpoint.restore用法及代碼示例
- Python tf.compat.v1.train.global_step用法及代碼示例
- Python tf.compat.v1.train.RMSPropOptimizer.compute_gradients用法及代碼示例
- Python tf.compat.v1.train.exponential_decay用法及代碼示例
- Python tf.compat.v1.train.natural_exp_decay用法及代碼示例
- Python tf.compat.v1.train.RMSPropOptimizer用法及代碼示例
- Python tf.compat.v1.train.get_global_step用法及代碼示例
- Python tf.compat.v1.train.GradientDescentOptimizer.compute_gradients用法及代碼示例
- Python tf.compat.v1.train.linear_cosine_decay用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.compat.v1.train.MonitoredSession。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。