Book Image

TensorFlow 2.0 Quick Start Guide

By : Tony Holdroyd
Book Image

TensorFlow 2.0 Quick Start Guide

By: Tony Holdroyd

Overview of this book

TensorFlow is one of the most popular machine learning frameworks in Python. With this book, you will improve your knowledge of some of the latest TensorFlow features and will be able to perform supervised and unsupervised machine learning and also train neural networks. After giving you an overview of what's new in TensorFlow 2.0 Alpha, the book moves on to setting up your machine learning environment using the TensorFlow library. You will perform popular supervised machine learning tasks using techniques such as linear regression, logistic regression, and clustering. You will get familiar with unsupervised learning for autoencoder applications. The book will also show you how to train effective neural networks using straightforward examples in a variety of different domains. By the end of the book, you will have been exposed to a large variety of machine learning and neural network TensorFlow techniques.
Table of Contents (15 chapters)
Free Chapter
1
Section 1: Introduction to TensorFlow 2.00 Alpha
5
Section 2: Supervised and Unsupervised Learning in TensorFlow 2.00 Alpha
7
Unsupervised Learning Using TensorFlow 2
8
Section 3: Neural Network Applications of TensorFlow 2.00 Alpha
13
Converting from tf1.12 to tf2

Using our model to get predictions

To get the predictions from our model, we need to take a sample from the output distribution. This sampling will get us the characters we need from that output distribution (sampling the output distribution is important because taking the argmax of it, as we would normally do, can easily get the model stuck in a loop).

tf.random.categorical does this sampling and tf.squeeze with axis=-1 removes the last dimension of the tensor, prior to displaying the indices.

The signature of tf.random.categorical is as follows:

tf.random.categorical(logits, num_samples, seed=None, name=None, output_dtype=None)

Comparing this with the call, we see that we are taking one sample (of length sequence_length = 100) from the predictions (example_batch_predictions[0]). The extra dimension is then removed, so we can look up the characters corresponding to the sample...