Book Image

TensorFlow Machine Learning Cookbook

By : Nick McClure
Book Image

TensorFlow Machine Learning Cookbook

By: Nick McClure

Overview of this book

TensorFlow is an open source software library for Machine Intelligence. The independent recipes in this book will teach you how to use TensorFlow for complex data computations and will let you dig deeper and gain more insights into your data than ever before. You’ll work through recipes on training models, model evaluation, sentiment analysis, regression analysis, clustering analysis, artificial neural networks, and deep learning – each using Google’s machine learning library TensorFlow. This guide starts with the fundamentals of the TensorFlow library which includes variables, matrices, and various data sources. Moving ahead, you will get hands-on experience with Linear Regression techniques with TensorFlow. The next chapters cover important high-level concepts such as neural networks, CNN, RNN, and NLP. Once you are familiar and comfortable with the TensorFlow ecosystem, the last chapter will show you how to take it to production.
Table of Contents (19 chapters)
TensorFlow Machine Learning Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Index

Declaring Operations


Now we must learn about the other operations we can add to a TensorFlow graph.

Getting ready

Besides the standard arithmetic operations, TensorFlow provides us with more operations that we should be aware of. We need to know how to use them before proceeding. Again, we can create a graph session by running the following code:

import tensorflow as tf
sess = tf.Session()

How to do it…

TensorFlow has the standard operations on tensors: add(), sub(), mul(), and div(). Note that all of these operations in this section will evaluate the inputs element-wise unless specified otherwise:

  1. TensorFlow provides some variations of div() and relevant functions.

  2. It is worth mentioning that div() returns the same type as the inputs. This means it really returns the floor of the division (akin to Python 2) if the inputs are integers. To return the Python 3 version, which casts integers into floats before dividing and always returning a float, TensorFlow provides the function truediv() function, as shown as follows:

    print(sess.run(tf.div(3,4)))
    0
    print(sess.run(tf.truediv(3,4)))
    0.75
  3. If we have floats and want an integer division, we can use the function floordiv(). Note that this will still return a float, but rounded down to the nearest integer. The function is shown as follows:

    print(sess.run(tf.floordiv(3.0,4.0)))
    0.0
  4. Another important function is mod(). This function returns the remainder after the division. It is shown as follows:

    print(sess.run(tf.mod(22.0, 5.0)))
    2.0-
  5. The cross-product between two tensors is achieved by the cross() function. Remember that the cross-product is only defined for two three-dimensional vectors, so it only accepts two three-dimensional tensors. The function is shown as follows:

    print(sess.run(tf.cross([1., 0., 0.], [0., 1., 0.])))
    [ 0.  0.  1.0]
  6. Here is a compact list of the more common math functions. All of these functions operate elementwise.

    abs()

    Absolute value of one input tensor

    ceil()

    Ceiling function of one input tensor

    cos()

    Cosine function of one input tensor

    exp()

    Base e exponential of one input tensor

    floor()

    Floor function of one input tensor

    inv()

    Multiplicative inverse (1/x) of one input tensor

    log()

    Natural logarithm of one input tensor

    maximum()

    Element-wise max of two tensors

    minimum()

    Element-wise min of two tensors

    neg()

    Negative of one input tensor

    pow()

    The first tensor raised to the second tensor element-wise

    round()

    Rounds one input tensor

    rsqrt()

    One over the square root of one tensor

    sign()

    Returns -1, 0, or 1, depending on the sign of the tensor

    sin()

    Sine function of one input tensor

    sqrt()

    Square root of one input tensor

    square()

    Square of one input tensor

  7. Specialty mathematical functions: There are some special math functions that get used in machine learning that are worth mentioning and TensorFlow has built in functions for them. Again, these functions operate element-wise, unless specified otherwise:

    digamma()

    Psi function, the derivative of the lgamma() function

    erf()

    Gaussian error function, element-wise, of one tensor

    erfc()

    Complimentary error function of one tensor

    igamma()

    Lower regularized incomplete gamma function

    igammac()

    Upper regularized incomplete gamma function

    lbeta()

    Natural logarithm of the absolute value of the beta function

    lgamma()

    Natural logarithm of the absolute value of the gamma function

    squared_difference()

    Computes the square of the differences between two tensors

How it works…

It is important to know what functions are available to us to add to our computational graphs. Mostly, we will be concerned with the preceding functions. We can also generate many different custom functions as compositions of the preceding functions, as follows:

# Tangent function (tan(pi/4)=1)
print(sess.run(tf.div(tf.sin(3.1416/4.), tf.cos(3.1416/4.))))
1.0

There's more…

If we wish to add other operations to our graphs that are not listed here, we must create our own from the preceding functions. Here is an example of an operation not listed previously that we can add to our graph. We choose to add a custom polynomial function, :

def custom_polynomial(value):
    return(tf.sub(3 * tf.square(value), value) + 10)
print(sess.run(custom_polynomial(11)))
362