Reader small image

You're reading from  TensorFlow 2.0 Quick Start Guide

Product typeBook
Published inMar 2019
Reading LevelBeginner
PublisherPackt
ISBN-139781789530759
Edition1st Edition
Languages
Right arrow
Author (1)
Tony Holdroyd
Tony Holdroyd
author image
Tony Holdroyd

Tony Holdroyd's first degree, from Durham University, was in maths and physics. He also has technical qualifications, including MCSD, MCSD.net, and SCJP. He holds an MSc in computer science from London University. He was a senior lecturer in computer science and maths in further education, designing and delivering programming courses in many languages, including C, C+, Java, C#, and SQL. His passion for neural networks stems from research he did for his MSc thesis. He has developed numerous machine learning, neural network, and deep learning applications, and has advised in the media industry on deep learning as applied to image and music processing. Tony lives in Gravesend, Kent, UK, with his wife, Sue McCreeth, who is a renowned musician.
Read more about Tony Holdroyd

Right arrow

Introducing TensorFlow 2

TensorFlow began its life in 2011 as DisBelief, an internal, closed source project at Google. DisBelief was a machine learning system that employed deep learning neural networks. This system morphed into TensorFlow, which was released to the developer community under an Apache 2.0 open source license, on November 9, 2015. Version 1.0.0 made its appearance on February 11, 2017. There have been a number of point releases since then that have incorporated a wealth of new features.

At the time of writing this book, the most recent version is TensorFlow 2.0.0 alpha release, which was announced at the TensorFlow Dev Summit on March 6, 2019.

TensorFlow takes its name from, well, tensors. A tensor is a generalization of vectors and matrices to possibly higher dimensions. The rank of a tensor is the number of indices it takes to uniquely specify each element of...

Looking at the modern TensorFlow ecosystem

Let's discuss eager execution. The first incarnation of TensorFlow involved constructing a computational graph made up of operations and tensors, which had to be subsequently evaluated in what Google termed as session (this is known as declarative programming). This is still a common way to write TensorFlow programs. However, eager execution, available from release 1.5 onward in research form and baked into TensorFlow proper from release 1.7, involves the immediate evaluation of operations, with the consequence that tensors can be treated like NumPy arrays (this is known as imperative programming).

Google says that eager execution is the preferred method for research and development but that computational graphs are to be preferred for serving TensorFlow production applications.

tf.data is an API that allows you to build complicated...

Installing TensorFlow

The best programming support for TensorFlow is provided for Python (although libraries do exist for Java, C, and Go, while those for other languages are under active development).

There is a wealth of information on the web for installing TensorFlow for Python.

It is standard practice, also recommended by Google, to install TensorFlow in a virtual environment, that is, an environment that isolates a set of APIs and code from other APIs and code and from the system-wide environment.

There are two distinct versions of TensorFlow—one for execution on a CPU and another for execution on a GPU. This last requires that the numerical libraries CUDA and CuDNN are installed. Tensorflow will default to GPU execution where possible. See https://www.tensorflow.org/alpha/guide/using_gpu.

Rather than attempt to reinvent the wheel here, there follow resources for...

Housekeeping and eager operations

We will first look at how to import TensorFlow, then TensorFlow coding style, and how to do some basic housekeeping. After this, we will look at some basic TensorFlow operations. You can either create a Jupyter Notebook for these snippets or use your favorite IDE to create your source code. The code is all available in the GitHub repository.

Importing TensorFlow

Importing TensorFlow is straightforward. Note a couple of system checks:

import tensorflow as tf
print("TensorFlow version: {}".format(tf.__version__))
print("Eager execution is: {}".format(tf.executing_eagerly()))
print("Keras version: {}".format(tf.keras.__version__))
...

Providing useful TensorFlow operations

Finding the squared difference between two tensors

Later in this book, we will need to find the square of the difference between two tensors. The method is as follows:

tf.math.squared.difference( x,  y, name=None)

Take the following example:

x = [1,3,5,7,11]
y = 5
s = tf.math.squared_difference(x,y)
s

The output will be as follows:

<tf...

Summary

In this chapter, we started to become familiar with TensorFlow by looking at a number of snippets of code illustrating some basic operations. We had a look at an overview of the modern TensorFlow ecosystem and how to install TensorFlow. We also examined some housekeeping operations, some eager operations, and a variety of TensorFlow operations that will be useful in the rest of this book. There is an excellent introduction to TensorFlow 2 at www.youtube.com/watch?v=k5c-vg4rjBw.

Also check out Appendix A for details of a tf1.12 to tf2 conversion tool. In the next chapter, we will take a look at Keras, which is a high-level API for TensorFlow 2.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
TensorFlow 2.0 Quick Start Guide
Published in: Mar 2019Publisher: PacktISBN-13: 9781789530759
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Tony Holdroyd

Tony Holdroyd's first degree, from Durham University, was in maths and physics. He also has technical qualifications, including MCSD, MCSD.net, and SCJP. He holds an MSc in computer science from London University. He was a senior lecturer in computer science and maths in further education, designing and delivering programming courses in many languages, including C, C+, Java, C#, and SQL. His passion for neural networks stems from research he did for his MSc thesis. He has developed numerous machine learning, neural network, and deep learning applications, and has advised in the media industry on deep learning as applied to image and music processing. Tony lives in Gravesend, Kent, UK, with his wife, Sue McCreeth, who is a renowned musician.
Read more about Tony Holdroyd