Sessions

It is time to learn a little bit more about sessions. As we saw earlier, TensorFlow only executes the operations inside a session.

The simplest usage of a session is the following:

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run([mean], feed_dict={})

This will initialize the variables by calling their initializer inside the session. In a neural network, we cannot have all variables starting with a zero value, especially the weights in the different layers (some, such as bias, can be initialized to 0). The initializer is then of prime importance and is either set directly for explicit variables, or is implicit when calling mid-level functions (see the different initializers available with  TensorFlow and play with them in all our examples).

TensorFlow uses also an external way to get reports, called summaries. They live in the tf.summary module, and can track tensors, and preprocess them:

tf.summary.histogram(var)
tf.summary.scalar('mean', tf.reduce_mean(var))

All these summary reports can then be written and retrieved during a session run and saved by a special object:

merged = tf.summary.merge_all()
writer = tf.summary.FileWriter(path/to/log-directory)
with tf.Session() as sess:
summary, _ = sess.run([merged, train_step], feed_dict={})
writer.add_summary(summary, i)
Tensorboard is a tool provided with TensorFlow that allows us to display these summaries. It can be launched with tensorboard --logdir=path/to/log-directory.

If we don't want to use a session directly, the Estimator class can be used.

From an estimator, we can call its method train, which takes as an argument a data generator and the number of steps that we want to run. For instance, this could be:

def input_fn():
features = {'SepalLength': np.array([6.4, 5.0]),
'SepalWidth': np.array([2.8, 2.3]),
'PetalLength': np.array([5.6, 3.3]),
'PetalWidth': np.array([2.2, 1.0])}
labels = np.array([2, 1])
return features, labels
estimator.train(input_fn=input_fn , steps=STEPS)

Similarly, we can use test to get results from the model:

estimator.test(input_fn=input_train)

If you want to use more than the simple Estimators already provided with TensorFlow, we suggest to follow the tutorial on the TensorFlow website: https://www.tensorflow.org/get_started/custom_estimators.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset