本文整理汇总了Python中tensorflow.python.training.monitored_session._HookedSession方法的典型用法代码示例。如果您正苦于以下问题:Python monitored_session._HookedSession方法的具体用法?Python monitored_session._HookedSession怎么用?Python monitored_session._HookedSession使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.python.training.monitored_session
的用法示例。
在下文中一共展示了monitored_session._HookedSession方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_print_at_end_only
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def test_print_at_end_only(self):
with tf.Graph().as_default(), tf.Session() as sess:
tf.train.get_or_create_global_step()
t = tf.constant(42.0, name='foo')
train_op = tf.constant(3)
hook = metric_hook.LoggingMetricHook(
tensors=[t.name], at_end=True, metric_logger=self._logger)
hook.begin()
mon_sess = monitored_session._HookedSession(sess, [hook])
sess.run(tf.global_variables_initializer())
for _ in range(3):
mon_sess.run(train_op)
self.assertEqual(self._logger.logged_metric, [])
hook.end(sess)
self.assertEqual(len(self._logger.logged_metric), 1)
metric = self._logger.logged_metric[0]
self.assertRegexpMatches(metric["name"], "foo")
self.assertEqual(metric["value"], 42.0)
self.assertEqual(metric["unit"], None)
self.assertEqual(metric["global_step"], 0)
示例2: testRunPassesAllArguments
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def testRunPassesAllArguments(self):
with tf.Graph().as_default(), tf.Session() as sess:
mock_run = FakeSession(sess)
mon_sess = monitored_session._HookedSession(sess=mock_run, hooks=[])
a_tensor = tf.constant([0], name='a_tensor')
sess.run(tf.global_variables_initializer())
output = mon_sess.run(fetches=a_tensor,
feed_dict='a_feed',
options='an_option',
run_metadata='a_metadata')
self.assertEqual(output, [0])
self.assertEqual(mock_run.args_called, {
'feed_dict': 'a_feed',
'options': 'an_option',
'run_metadata': 'a_metadata'
})
示例3: testCallsHooksBeginEnd
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def testCallsHooksBeginEnd(self):
with tf.Graph().as_default(), tf.Session() as sess:
mock_hook = FakeHook()
mock_hook2 = FakeHook()
mon_sess = monitored_session._HookedSession(
sess=sess, hooks=[mock_hook, mock_hook2])
a_tensor = tf.constant([0], name='a_tensor')
sess.run(tf.global_variables_initializer())
mon_sess.run(a_tensor)
for hook in [mock_hook, mock_hook2]:
self.assertEqual(
hook.last_run_values, tf.train.SessionRunValues(results=None))
self.assertEqual(hook.last_run_context.original_args,
tf.train.SessionRunArgs(a_tensor))
self.assertEqual(hook.last_run_context.session, sess)
self.assertEqual(hook.call_counter['before_run'], 1)
self.assertEqual(hook.call_counter['after_run'], 1)
示例4: testFetchesHookRequests
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def testFetchesHookRequests(self):
with tf.Graph().as_default(), tf.Session() as sess:
mock_hook = FakeHook()
mock_hook2 = FakeHook()
mon_sess = monitored_session._HookedSession(
sess=sess, hooks=[mock_hook, mock_hook2])
a_tensor = tf.constant([0], name='a_tensor')
another_tensor = tf.constant([5], name='another_tensor')
third_tensor = tf.constant([10], name='third_tensor')
mock_hook.request = tf.train.SessionRunArgs([another_tensor])
mock_hook2.request = tf.train.SessionRunArgs([third_tensor])
sess.run(tf.global_variables_initializer())
output = mon_sess.run(fetches=a_tensor)
self.assertEqual(output, [0])
self.assertEqual(mock_hook.last_run_values.results, [5])
self.assertEqual(mock_hook2.last_run_values.results, [10])
示例5: testOnlyHooksHaveFeeds
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def testOnlyHooksHaveFeeds(self):
with tf.Graph().as_default(), tf.Session() as sess:
mock_hook = FakeHook()
mock_hook2 = FakeHook()
mon_sess = monitored_session._HookedSession(
sess=sess, hooks=[mock_hook, mock_hook2])
a_tensor = tf.constant([0], name='a_tensor')
b_tensor = tf.constant([0], name='b_tensor')
add_tensor = a_tensor + b_tensor
mock_hook.request = tf.train.SessionRunArgs(
None, feed_dict={
a_tensor: [5]
})
mock_hook2.request = tf.train.SessionRunArgs(
None, feed_dict={
b_tensor: [10]
})
sess.run(tf.global_variables_initializer())
self.assertEqual(mon_sess.run(fetches=add_tensor), [15])
示例6: testHooksFeedConflicts
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def testHooksFeedConflicts(self):
with tf.Graph().as_default(), tf.Session() as sess:
mock_hook = FakeHook()
mock_hook2 = FakeHook()
mon_sess = monitored_session._HookedSession(
sess=sess, hooks=[mock_hook, mock_hook2])
a_tensor = tf.constant([0], name='a_tensor')
b_tensor = tf.constant([0], name='b_tensor')
add_tensor = a_tensor + b_tensor
mock_hook.request = tf.train.SessionRunArgs(
None, feed_dict={
a_tensor: [5]
})
mock_hook2.request = tf.train.SessionRunArgs(
None, feed_dict={
a_tensor: [10]
})
sess.run(tf.global_variables_initializer())
with self.assertRaisesRegexp(RuntimeError, 'Same tensor is fed'):
mon_sess.run(fetches=add_tensor)
示例7: testHooksAndUserFeedConflicts
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def testHooksAndUserFeedConflicts(self):
with tf.Graph().as_default(), tf.Session() as sess:
mock_hook = FakeHook()
mock_hook2 = FakeHook()
mon_sess = monitored_session._HookedSession(
sess=sess, hooks=[mock_hook, mock_hook2])
a_tensor = tf.constant([0], name='a_tensor')
b_tensor = tf.constant([0], name='b_tensor')
add_tensor = a_tensor + b_tensor
mock_hook.request = tf.train.SessionRunArgs(
None, feed_dict={
a_tensor: [5]
})
mock_hook2.request = tf.train.SessionRunArgs(
None, feed_dict={
b_tensor: [10]
})
sess.run(tf.global_variables_initializer())
with self.assertRaisesRegexp(RuntimeError, 'Same tensor is fed'):
mon_sess.run(fetches=add_tensor, feed_dict={b_tensor: [10]})
示例8: test_stop_based_on_last_step
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def test_stop_based_on_last_step(self):
h = tf.train.StopAtStepHook(last_step=10)
with tf.Graph().as_default():
global_step = tf.contrib.framework.get_or_create_global_step()
no_op = tf.no_op()
h.begin()
with tf.Session() as sess:
mon_sess = monitored_session._HookedSession(sess, [h])
sess.run(tf.assign(global_step, 5))
mon_sess.run(no_op)
self.assertFalse(mon_sess.should_stop())
sess.run(tf.assign(global_step, 9))
mon_sess.run(no_op)
self.assertFalse(mon_sess.should_stop())
sess.run(tf.assign(global_step, 10))
mon_sess.run(no_op)
self.assertTrue(mon_sess.should_stop())
sess.run(tf.assign(global_step, 11))
mon_sess._should_stop = False
mon_sess.run(no_op)
self.assertTrue(mon_sess.should_stop())
示例9: test_stop_based_on_num_step
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def test_stop_based_on_num_step(self):
h = tf.train.StopAtStepHook(num_steps=10)
with tf.Graph().as_default():
global_step = tf.contrib.framework.get_or_create_global_step()
no_op = tf.no_op()
h.begin()
with tf.Session() as sess:
mon_sess = monitored_session._HookedSession(sess, [h])
sess.run(tf.assign(global_step, 5))
mon_sess.run(no_op)
self.assertFalse(mon_sess.should_stop())
sess.run(tf.assign(global_step, 13))
mon_sess.run(no_op)
self.assertFalse(mon_sess.should_stop())
sess.run(tf.assign(global_step, 14))
mon_sess.run(no_op)
self.assertTrue(mon_sess.should_stop())
sess.run(tf.assign(global_step, 15))
mon_sess._should_stop = False
mon_sess.run(no_op)
self.assertTrue(mon_sess.should_stop())
示例10: test_print_every_n_steps
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def test_print_every_n_steps(self):
with tf.Graph().as_default(), tf.Session() as sess:
t = tf.constant(42.0, name='foo')
train_op = tf.constant(3)
hook = tf.train.LoggingTensorHook(tensors=[t.name], every_n_iter=10)
hook.begin()
mon_sess = monitored_session._HookedSession(sess, [hook])
sess.run(tf.global_variables_initializer())
mon_sess.run(train_op)
self.assertRegexpMatches(str(self.logged_message), t.name)
for j in range(3):
_ = j
self.logged_message = ''
for i in range(9):
_ = i
mon_sess.run(train_op)
# assertNotRegexpMatches is not supported by python 3.1 and later
self.assertEqual(str(self.logged_message).find(t.name), -1)
mon_sess.run(train_op)
self.assertRegexpMatches(str(self.logged_message), t.name)
示例11: test_save_steps
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def test_save_steps(self):
hook = tf.train.SummarySaverHook(
save_steps=8,
summary_writer=self.summary_writer,
summary_op=self.summary_op)
with self.test_session() as sess:
hook.begin()
sess.run(tf.global_variables_initializer())
mon_sess = monitored_session._HookedSession(sess, [hook])
for _ in range(30):
mon_sess.run(self.train_op)
hook.end(sess)
self.summary_writer.assert_summaries(
test_case=self,
expected_logdir=self.log_dir,
expected_summaries={
1: {'my_summary': 1.0},
9: {'my_summary': 2.0},
17: {'my_summary': 3.0},
25: {'my_summary': 4.0},
})
示例12: test_save_secs_saving_once_every_step
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def test_save_secs_saving_once_every_step(self):
hook = tf.train.SummarySaverHook(
save_secs=0.5,
summary_writer=self.summary_writer,
summary_op=self.summary_op)
with self.test_session() as sess:
hook.begin()
sess.run(tf.global_variables_initializer())
mon_sess = monitored_session._HookedSession(sess, [hook])
for _ in range(4):
mon_sess.run(self.train_op)
time.sleep(0.5)
hook.end(sess)
self.summary_writer.assert_summaries(
test_case=self,
expected_logdir=self.log_dir,
expected_summaries={
1: {'my_summary': 1.0},
2: {'my_summary': 2.0},
3: {'my_summary': 3.0},
4: {'my_summary': 4.0},
})
示例13: test_save_secs_saving_once_every_three_steps
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def test_save_secs_saving_once_every_three_steps(self):
hook = tf.train.SummarySaverHook(
save_secs=0.9,
summary_writer=self.summary_writer,
summary_op=self.summary_op)
with self.test_session() as sess:
hook.begin()
sess.run(tf.global_variables_initializer())
mon_sess = monitored_session._HookedSession(sess, [hook])
for _ in range(8):
mon_sess.run(self.train_op)
time.sleep(0.3)
hook.end(sess)
self.summary_writer.assert_summaries(
test_case=self,
expected_logdir=self.log_dir,
expected_summaries={
1: {'my_summary': 1.0},
4: {'my_summary': 2.0},
7: {'my_summary': 3.0},
})
示例14: test_stop_based_on_last_step
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def test_stop_based_on_last_step(self):
h = basic_session_run_hooks.StopAtStepHook(last_step=10)
with tf.Graph().as_default():
global_step = tf.compat.v1.train.get_or_create_global_step()
no_op = tf.no_op()
h.begin()
with tf.compat.v1.Session() as sess:
mon_sess = monitored_session._HookedSession(sess, [h])
sess.run(tf.compat.v1.assign(global_step, 5))
h.after_create_session(sess, None)
mon_sess.run(no_op)
self.assertFalse(mon_sess.should_stop())
sess.run(tf.compat.v1.assign(global_step, 9))
mon_sess.run(no_op)
self.assertFalse(mon_sess.should_stop())
sess.run(tf.compat.v1.assign(global_step, 10))
mon_sess.run(no_op)
self.assertTrue(mon_sess.should_stop())
sess.run(tf.compat.v1.assign(global_step, 11))
mon_sess._should_stop = False
mon_sess.run(no_op)
self.assertTrue(mon_sess.should_stop())
示例15: test_stop_based_with_multiple_steps
# 需要导入模块: from tensorflow.python.training import monitored_session [as 别名]
# 或者: from tensorflow.python.training.monitored_session import _HookedSession [as 别名]
def test_stop_based_with_multiple_steps(self):
h = basic_session_run_hooks.StopAtStepHook(num_steps=10)
with tf.Graph().as_default():
global_step = tf.compat.v1.train.get_or_create_global_step()
no_op = tf.no_op()
h.begin()
with tf.compat.v1.Session() as sess:
mon_sess = monitored_session._HookedSession(sess, [h])
sess.run(tf.compat.v1.assign(global_step, 5))
h.after_create_session(sess, None)
mon_sess.run(no_op)
self.assertFalse(mon_sess.should_stop())
sess.run(tf.compat.v1.assign(global_step, 15))
mon_sess.run(no_op)
self.assertTrue(mon_sess.should_stop())