Book Image

Getting Started with TensorFlow

By : Giancarlo Zaccone
Book Image

Getting Started with TensorFlow

By: Giancarlo Zaccone

Overview of this book

<p>Google's TensorFlow engine, after much fanfare, has evolved in to a robust, user-friendly, and customizable, application-grade software library of machine learning (ML) code for numerical computation and neural networks.</p> <p>This book takes you through the practical software implementation of various machine learning techniques with TensorFlow. In the first few chapters, you'll gain familiarity with the framework and perform the mathematical operations required for data analysis. As you progress further, you'll learn to implement various machine learning techniques such as classification, clustering, neural networks, and deep learning through practical examples.</p> <p>By the end of this book, you’ll have gained hands-on experience of using TensorFlow and building classification, image recognition systems, language processing, and information retrieving systems for your application.</p>
Table of Contents (12 chapters)

Computing gradients


TensorFlow has functions to solve other more complex tasks. For example, we will use a mathematical operator that calculates the derivative of y with respect to its expression x parameter. For this purpose, we use the tf.gradients() function.

Let us consider the math function y = 2x². We want to compute the gradient di y with respect to x=1. The following is the code to compute this gradient:

  1. First, import the TensorFlow library:

        import TensorFlow as tf
    
  2. The x variable is the independent variable of the function:

        x = tf.placeholder(tf.float32)
    
  3. Let's build the function:

        y =  2*x*x
    
  4. Finally, we call the  tf.gradients() function with y and x as arguments:

        var_grad = tf.gradients(y, x)
    
  5. To evaluate the gradient, we must build a session:

        with tf.Session() as session:
    
  6. The gradient will be evaluated on the variable x=1:

        var_grad_val = session.run(var_grad,feed_dict={x:1}) 
    
    
  7. The var_grad_val value is the feed result, to be printed:

        print(var_grad_val...