Matrix Multiplication

Abstract

Computation in TensorFlow is represented as a graph.

Nodes in the graph are called ops (short for operations). An op takes zero or more Tensors (typed multi-dimensional arrays), performs some computation, and produces zero or more Tensors.

A TensorFlow graph is a description of computations. To compute anything, a graph must be launched in a Session. A Session places the graph ops onto Devices, such as CPUs or GPUs, and provides methods to execute them.

Introduction

This tutorial is taken, with slight modification and different annotations, from TensorFlow's official documentation.

This tutorial is intended for readers who are new to both machine learning and TensorFlow.

Graph Construction

First of all, we import the TensorFlow library.


In [1]:
import tensorflow as tf

We then create a constant op that produces a 1x2 matrix. The op is added as a node to the default graph.

The value returned by the constructor represents the output of the Constant op.


In [2]:
matrix1 = tf.constant([[3., 3.]])

We create another constant that produces a 2x1 matrix.


In [3]:
matrix2 = tf.constant([[2.],[2.]])

Finally, we create a matmul op that takes matrix1 and matrix2 as inputs. The returned value, product, represents the result of the matrix multiplication.


In [4]:
product = tf.matmul(matrix1, matrix2)

Launching of the Session

To compute anything, a graph must first be launched in a Session. Let's create our Session by launching the default graph.


In [5]:
sess = tf.Session()

To run the matmul op we call the session run() method, passing product (which represents the output of the matmul op). This indicates to the call that we want to get the output of the matmul op back.

All inputs needed by the op are run automatically by the session. They typically are run in parallel.

The call run(product) thus causes the execution of three ops in the graph: the two constants and matmul.

The output of the op is returned in result as a numpy ndarray object.


In [6]:
result = sess.run(product)
print(result)


[[ 12.]]

Once we are done, let's close the Session.


In [7]:
sess.close()