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

Using the Matrix Inverse Method


In this recipe, we will use TensorFlow to solve two dimensional linear regressions with the matrix inverse method.

Getting ready

Linear regression can be represented as a set of matrix equations, say . Here we are interested in solving the coefficients in matrix x. We have to be careful if our observation matrix (design matrix) A is not square. The solution to solving x can be expressed as . To show this is indeed the case, we will generate two-dimensional data, solve it in TensorFlow, and plot the result.

How to do it…

  1. First we load the necessary libraries, initialize the graph, and create the data, as follows:

    import matplotlib.pyplot as plt
    import numpy as np
    import tensorflow as tf
    sess = tf.Session()
    x_vals = np.linspace(0, 10, 100)
    y_vals = x_vals + np.random.normal(0, 1, 100)
  2. Next we create the matrices to use in the inverse method. We create the A matrix first, which will be a column of x-data and a column of 1s. Then we create the b matrix from the y-data...