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

Calculating the losses

We now need the losses between the contents and styles of the two images. We will be using the mean squared loss as follows. Notice here that the subtraction in image1 - image2 is element-wise between the two image arrays. This subtraction works because the images have been resized to the same size in load_image:

def rms_loss(image1,image2):
loss = tf.reduce_mean(input_tensor=tf.square(image1 - image2))
return loss

So next, we define our content_loss function. This is just the mean squared difference between what is named content and target in the function signature:

def content_loss(content, target):
return rms_loss(content, target)

The style loss is defined in terms of a quantity called a Gram matrix. A Gram matrix, also known as the metric, is the dot product of the style matrix with its own transpose. Since this means that each column of the image...