Book Image

TensorFlow 1.x Deep Learning Cookbook

Book Image

TensorFlow 1.x Deep Learning Cookbook

Overview of this book

Deep neural networks (DNNs) have achieved a lot of success in the field of computer vision, speech recognition, and natural language processing. This exciting recipe-based guide will take you from the realm of DNN theory to implementing them practically to solve real-life problems in the artificial intelligence domain. In this book, you will learn how to efficiently use TensorFlow, Google’s open source framework for deep learning. You will implement different deep learning networks, such as Convolutional Neural Networks (CNNs), Recurrent Neural Networks (RNNs), Deep Q-learning Networks (DQNs), and Generative Adversarial Networks (GANs), with easy-to-follow standalone recipes. You will learn how to use TensorFlow with Keras as the backend. You will learn how different DNNs perform on some popularly used datasets, such as MNIST, CIFAR-10, and Youtube8m. You will not only learn about the different mobile and embedded platforms supported by TensorFlow, but also how to set up cloud platforms for deep learning applications. You will also get a sneak peek at TPU architecture and how it will affect the future of DNNs. By using crisp, no-nonsense recipes, you will become an expert in implementing deep learning techniques in growing real-world applications and research areas such as reinforcement learning, GANs, and autoencoders.
Table of Contents (15 chapters)
14
TensorFlow Processing Units

Migrating from 0.x to 1.x

TensorFlow 1.x does not offer backward compatibility. This means that the codes that worked on TensorFlow 0.x may not work on TensorFlow 1.0. So, if you have codes that worked on TensorFlow 0.x, you need to upgrade them (old GitHub repositories or your own codes). This recipe will point out major differences between TensorFlow 0.x and TensorFlow 1.0 and will show you how to use the script tf_upgrade.py to automatically upgrade the code for TensorFlow 1.0.

How to do it...

Here is how we proceed with the recipe:

  1. First, download tf_upgrade.py from https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/compatibility.
  2. If you want to convert one file from TensorFlow 0.x to TensorFlow 1.0, use the following command at the command line:
python tf_upgrade.py --infile old_file.py --outfile upgraded_file.py
  1. For example, if you have a TensorFlow program file named test.py, you will use the preceding command as follows:
python tf_upgrade.py --infile test.py --outfile test_1.0.py
  1. This will result in the creation of a new file named test_1.0.py.
  2. If you want to migrate all the files of a directory, then use the following at the command line:
python tf_upgrade.py --intree InputDIr --outtree OutputDir
# For example, if you have a directory located at /home/user/my_dir you can migrate all the python files in the directory located at /home/user/my-dir_1p0 using the above command as:
python tf_upgrade.py --intree /home/user/my_dir --outtree /home/user/my_dir_1p0

  1. In most cases, the directory also contains dataset files; you can ensure that non-Python files are copied as well in the new directory (my-dir_1p0 in the preceding example) using the following:
python tf_upgrade.py --intree /home/user/my_dir --outtree /home/user/my_dir_1p0 -copyotherfiles True
  1. In all these cases, a report.txt file is generated. This file contains the details of conversion and any errors in the process.
  2. Read the report.txt file and manually upgrade the part of the code that the script is unable to update.

There's more...

tf_upgrade.py has certain limitations:

  • It cannot change the arguments of tf.reverse(): you will have to manually fix it
  • For methods with argument list reordered, like tf.split() and tf.reverse_split(), it will try to introduce keyword arguments, but it cannot actually reorder the arguments
  • You will have to manually replace constructions like tf.get.variable_scope().reuse_variables() with the following:
with tf.variable_scope(tf.get_variable_scope(), resuse=True):