tf.SessionRunHook
enables you to add your custom code during each session run command you execute in your code. To understand it, I have created a simple example below:
- We want to print loss values after each update of the parameters.
- We will use
SessionRunHook
to achieve this.
Create a tensorflow Graph
import tensorflow as tf
import numpy as np
x = tf.placeholder(shape=(10, 2), dtype=tf.float32)
w = tf.Variable(initial_value=[[10.], [10.]])
w0 = [[1], [1.]]
y = tf.matmul(x, w0)
loss = tf.reduce_mean((tf.matmul(x, w) - y) ** 2)
optimizer = tf.train.AdamOptimizer(0.001).minimize(loss)
Creating the Hook
class _Hook(tf.train.SessionRunHook):
def __init__(self, loss):
self.loss = loss
def begin(self):
pass
def before_run(self, run_context):
return tf.train.SessionRunArgs(self.loss)
def after_run(self, run_context, run_values):
loss_value = run_values.results
print("loss value:", loss_value)
Creating a monitored Session with a hook
sess = tf.train.MonitoredSession(hooks=[_Hook(loss)])
train
for _ in range(10):
x_ = np.random.random((10, 2))
sess.run(optimizer, {x: x_})
# Output
loss value: 21.244701
loss value: 19.39169
loss value: 16.02665
loss value: 16.717144
loss value: 15.389178
loss value: 16.23935
loss value: 14.299083
loss value: 9.624525
loss value: 5.654896
loss value: 10.689494