Data in Tensorflow

Data is represented in Numpy differently than it is in Tensorflow, let’s go through some basic differences that will affect our work. This stuff is pretty basic but worth repeating:

Numpy

  • A matrix is represented by (# or rows, # of columns) so a 2x3 matrix has 2 rows and 3 columns which means it is a 2D array of numbers
  • So to store it in numpy we use the following formula with each row contained in […] brackets
  • Here below we will have a row vector, which is a 2D, 1 row by 2 column matrix, or vector
  • The next one is a column vector, which is 2 rows and 1 column matrix
  • Note all formulas have used [[..]] double brackets which makes them 2 D vectors.
  • If we use one [ set of brackets ] we end up with a 1D vector with no columns

So when we run the previous model shown below we get out of a1 a tf.tensor matrix as shown below.A 1x3 matrix. If we want to convert it to a numpy array, all we do is use a1.numpy() and we end up with an array

And for layer2 if the output is 0.8 this is what we will have when using Tensorflow which is a 1x1 matrix

Matrix Multiplication


We can use the matmul() to get the multiplication

Here is where it all came from: Numpy

or if matrices are reversed

Remember Transpose is flipping a matrix on its side

Back to our example:

In code it would look like this

Compare that to vectorization we used in TF, you see the difference being A_in instead of AT. It is that way because TF lays out the data in rows similar to the Transpose

Building with TF


Layer at a time

So let’s put it all together and see how to build a NN with TF.

  • Here is what we learned already
  • The way to compute one layer at a time
  • Remember Dense() is how we put together a layer
  • We initialize x
  • Create layer 1
  • Compute layer 1 and output the activation a1
  • Feed a1 into layer 2
  • Compute layer 2 and output the activation a2

Simpler

  • Instead of feeding the input to each layer we
  • create the layers the same way
  • create the model by stringing together all the layers in a Sequential() way
  • Create a numpy array = x from the training set input (4x2) in this case
  • Create a 1D array = y from the target values
  • To Compile the NN all we do is call two functions: model.compile()
  • To Train the NN on the training data we call model.fit(x,y)
  • To create inference, or make a prediction based on a new input: x_new we just call model.predict(x_new)

Simplify Again

  • We can simplify the top 3 lines in the image above
  • We don’t need to assign the two layers we can directly write it like this

Digit Classification

Let’s go back to the digit classification model from last page and simplify it

  • If you remember the model looked like this

  • We can write it like this