Python Tutorial For Beginners 1: Install And Setup For Mac
As can be observed above, TensorFlow constants can be declared using the tf.constant function, and variables with the tf.Variable function. The first element in both is the value to be assigned the constant / variable when it is initialised.
- Python Tutorial For Beginners 1 Install And Setup For Macbook Pro
- Python Tutorial For Beginners 1 Install And Setup For Mac

The second is an optional name string which can be used to label the constant / variable – this is handy for when you want to do visualisations (as will be discussed briefly later). TensorFlow will infer the type of the constant / variable from the initialised value, but it can also be set explicitly using the optional dtype argument. TensorFlow has many of its own types like tf.float32, tf.int32 etc. – see them all.
It’s important to note that, as the Python code runs through these commands, the variables haven’t actually been declared as they would have been if you just had a standard Python declaration (i.e. Instead, all the constants, variables, operations and the computational graph are only created when the initialisation commands are run. Next, we create the TensorFlow operations. Ok, so now we are all set to go. To run the operations between the variables, we need to start a TensorFlow session – tf.Session.
The TensorFlow session is an object where all operations are run. TensorFlow was initially created in a static graph paradigm – in other words, first all the operations and variables are defined (the graph structure) and then these are compiled within the tf.Session object. There is now the option to build graphs on the fly using the TensorFlow Eager framework, to check this out see my. However, there are still advantages in building static graphs using the tf.Session object.
You can do this by using the with Python syntax, to run the graph like so. The first command within the with block is the initialisation, which is run with the, well, run command. Next we want to figure out what the variable a should be. All we have to do is run the operation which calculates a i.e.
Python Tutorial For Beginners 1 Install And Setup For Macbook Pro
A = tf.multiply(d, e, name=’a’). Note that a is an operation, not a variable and therefore it can be run. We do just that with the sess.run(a) command and assign the output to aout, the value of which we then print out. Note something cool – we defined operations d and e which need to be calculated before we can figure out what a is. However, we don’t have to explicitly run those operations, as TensorFlow knows what other operations and variables the operation a depends on, and therefore runs the necessary operations on its own. It does this through its data flow graph which shows it all the required dependencies. Using the TensorBoard functionality, we can see the graph that TensorFlow created in this little program: Simple TensorFlow graph Now that’s obviously a trivial example – what if we had an array of b values that we wanted to calculate the value of a over?
2.1 The TensorFlow placeholder Let’s also say that we didn’t know what the value of the array b would be during the declaration phase of the TensorFlow problem (i.e. Before the with tf.Session as sess) stage. In this case, TensorFlow requires us to declare the basic structure of the data by using the tf.placeholder variable declaration. Let’s use it for b. Because we aren’t providing an initialisation in this declaration, we need to tell TensorFlow what data type each element within the tensor is going to be. In this case, we want to use tf.float32.
Python Tutorial For Beginners 1 Install And Setup For Mac
The second argument is the shape of the data that will be “injected” into this variable. In this case, we want to use a (? X 1) sized array – because we are being cagey about how much data we are supplying to this variable (hence the “?”), the placeholder is willing to accept a None argument in the size declaration. Now we can inject as much 1-dimensional data that we want into the b variable. The only other change we need to make to our program is in the sess.run(a,) command.
Notice how TensorFlow adapts naturally from a scalar output (i.e. A singular output when a=9.0) to a tensor (i.e.
An array/matrix)? This is based on its understanding of how the data will flow through the graph. Now we are ready to build a basic MNIST predicting neural network. 3.0 A Neural Network Example Now we’ll go through an example in TensorFlow of creating a simple three layer neural network. In future articles, we’ll show how to build more complicated neural network structures such as convolution neural networks and recurrent neural networks. For this example though, we’ll keep it simple.
If you need to scrub up on your neural network basics, check out my. In this example, we’ll be using the MNIST dataset (and its associated loader) that the TensorFlow package provides. This MNIST dataset is a set of 28×28 pixel grayscale images which represent hand-written digits. It has 55,000 training rows, 10,000 testing rows and 5,000 validation rows. We can load the data by running.
Notice the x input layer is 784 nodes corresponding to the 28 x 28 (=784) pixels, and the y output layer is 10 nodes corresponding to the 10 possible digits. Again, the size of x is (? x 784), where the? Stands for an as yet unspecified number of samples to be input – this is the function of the placeholder variable. Now we need to setup the weight and bias variables for the three layer neural network. There are always L-1 number of weights/bias tensors, where L is the number of layers.
So in this case, we need to setup two tensors for each. Ok, so let’s unpack the above code a little. First, we declare some variables for W1 and b1, the weights and bias for the connections between the input and hidden layer. This neural network will have 300 nodes in the hidden layer, so the size of the weight tensor W1 is 784, 300. We initialise the values of the weights using a random normal distribution with a mean of zero and a standard deviation of 0.03. TensorFlow has a replicated version of the, which allows you to create a matrix of a given size populated with random samples drawn from a given distribution. Likewise, we create W2 and b2 variables to connect the hidden layer to the output layer of the neural network.

Next, we have to setup node inputs and activation functions of the hidden layer nodes. Here we are just using the gradient descent optimiser provided by TensorFlow. We initialize it with a learning rate, then specify what we want it to do – i.e. Minimise the cross entropy cost operation we created. This function will then perform the gradient descent (for more details on gradient descent see and ) and the for you.
How easy is that? TensorFlow has a library of popular neural network training optimisers, see. Finally, before we move on to the main show, were we actually run the operations, let’s setup the variable initialisation operation and an operation to measure the accuracy of our predictions.
The correct prediction operation correctprediction makes use of the TensorFlow tf.equal function which returns True or False depending on whether to arguments supplied to it are equal. The tf.argmax function is the same as the, which returns the index of the maximum value in a vector / tensor. Therefore, the correctprediction operation returns a tensor of size ( m x 1) of True and False values designating whether the neural network has correctly predicted the digit. We then want to calculate the mean accuracy from this tensor – first we have to cast the type of the correctprediction operation from a Boolean to a TensorFlow float in order to perform the reducemean operation. Once we’ve done that, we now have an accuracy operation ready to assess the performance of our neural network. 3.2 Setting up the training We now have everything we need to setup the training process of our neural network.
I’m going to show the full code below, then talk through it. Stepping through the lines above, the first couple relate to setting up the with statement and running the initialisation operation. The third line relates to our mini-batch training scheme that we are going to run for this neural network. If you want to know about mini-batch gradient descent, check out this.
In the third line, we are calculating the number of batches to run through in each training epoch. After that, we loop through each training epoch and initialise an avgcost variable to keep track of the average cross entropy cost for each epoch.
The next line is where we extract a randomised batch of samples, batchx and batchy, from the MNIST training dataset. The TensorFlow provided MNIST dataset has a handy utility function, nextbatch, that makes it easy to extract batches of data for training. The following line is where we run two operations. Notice that sess.run is capable of taking a list of operations to run as its first argument. In this case, supplying optimiser, crossentropy as the list means that both these operations will be performed.
As such, we get two outputs, which we have assigned to the variables and c. We don’t really care too much about the output from the optimiser operation but we want to know the output from the crossentropy operation – which we have assigned to the variable c. Note, we run the optimiser (and crossentropy) operation on the batch samples.
In the following line, we use c to calculate the average cost for the epoch. Finally, we print out our progress in the average cost, and after the training is complete, we run the accuracy operation to print out the accuracy of our trained network on the test set. Running this program produces the following output.
There we go – approximately 98% accuracy on the test set, not bad. We could do a number of things to improve the model, such as regularisation (see this ), but here we are just interested in exploring TensorFlow.
You can also use TensorBoard visualisation to look at things like the increase in accuracy over the epochs: TensorBoard plot of the increase in accuracy over 10 epochs In a future article, I’ll introduce you to TensorBoard visualisation, which is a really nice feature of TensorFlow. For now, I hope this tutorial was instructive and helps get you going on the TensorFlow journey. Just a reminder, you can check out the code for this post.
I’ve also written an article that shows you how to build more complex neural networks such as, and in TensorFlow. You also might want to check out a higher level deep learning library that sits on top of TensorFlow called Keras – see.