Tensorflow BasicsTensorFlow library can do most of the things that we can do with other library also . However, it has a very different approach for doing so. And it can do a whole lot more cool stuff which we'll eventually get into.The major difference to take away from the remainder of this session is that instead of computing things immediately, we first define things that we want to compute later using what's called a graph. Everything in Tensorflow takes place in a computational graph and running and evaluating anything in the graph requires a session. Let's take a look at how these both work and then we'll get into the benefits of why this is useful:Variables We're first going to import the tensorflow library:
In
Think of GraphsLet's try and inspect the underlying graph. We can request the "default" graph where all of our operations have been added: OperationsAnd from this graph, we can get a list of all the operations that have been added, and print out their names:
We can request the output of any operation, which is a tensor, by asking the graph for the tensor's name:
In order to actually compute anything in
We could also explicitly tell the session which graph we want to manage: By default, it grabs the default graph. But we could have created a new graph like so: And then used this graph only in our session.
let's apply what we have learn so far 1 Part One - Comput e the MeanInstructionsUse Python, Numpy, Matplotlib and pandas or csv to load given dataset of images and create a montage of the dataset as a H x W image. You’ll need to make sure tensorflow call placeholder using(in our case) a 4-d array of N x H x W x C dimensions, meaning every image will need to be the same size! You can load an existing dataset of images, find your own images, or perhaps create your own images using a creative process such as painting, photography, or something along those lines.First use Tensorflow to define a session. Then use Tensorflow to create an operation which takes your 4-d array and calculates the mean color image (H xW x 3) using the function tf.reduce mean. Have a look at the documentation for this function to see how it works in order to get the mean of every pixel and get an image of (H x W x 3) as a result.You’ll then calculate the mean image by running the operation you create with your session (e.g. sess.run(...)). 2 Part Two - Compute the Standard DeviationInstructionsNow use tensorflow to calculate the standard deviation and upload the stan- dard deviation image averaged across color channels as a ”jet” heatmap of the N images. This will be a little more involved as there is no operation in tensorflow to do this for you. However, you can do this by calculating the mean image of your dataset as a 4-D array. To do this, you could write e.g. mean img 4d = tf.reduce_mean(imgs, axis=0, keep dims=True) to give you a 1 x H x W x C dimension array calculated on the N x H x W x C images variable. The axis parameter is saying to calculate the mean over the 0th dimension, meaning for every possible H, W, C, or for every pixel, you will have a mean composed over the N possible values it could have had, or what that pixel was for every possible image. This way, you can write images - mean img 4d to give you a N x H x W x C dimension variable, with every image in your images array having been subtracted by the mean img 4d. If you calculate the square root of the expected squared differences of this resulting operation, you have your standard deviation! In summary, you’ll need to write something like: subtraction = imgs- tf.reduce_mean(imgs, axis=0, keep dims=True), then reduce this operation us- ing tf.sqrt(tf.reduce_mean(subtraction * subtraction, axis=0)) to get your stan- dard deviation 3 Part Third - Normalize the DatasetInstructions Using tensorflow, we’ll attempt to normalize your dataset using the mean and standard deviation. We apply another type of normalization to 0-1 just for the purposes of plot-ting the image. If we didn’t do this, the range of our values would be somewhere between -1 and 1, and matplotlib would not be able to interpret the entire range of values. By rescaling our -1 to 1 valued images to 0-1, we can visualize it better. 4 Assignment In the given dataset of handwritten digits • find mean image • standard deviation • use both to normalize the data |

Computer Science >