First steps with TensorFlow
In reality, this post was intended for my DLAI course’s students, although I think it may be of interest to other students. I am going to share in this blog the teaching material that I am going to generate for the part of DLAI course that will cover the basic principles of Deep Learning from a computational perspective.
TensorFlow is an open source software library for numerical computation using data flow graphs. Nodes in the graph represent mathematical operations, while the graph edges represent the multidimensional data arrays (tensors) communicated between them. The flexible architecture allows you to deploy computation to one or more CPUs or GPUs in a desktop, server, or mobile device with a single API.
Task 1: Update DLAI lab docker image
We need to be sure that we have the last version of DLAI lab docker. Update or download our docker image :
docker pull jorditorresbcn/dlai-met:latest
Next we need to create a new container with the following options:
- Forward port 6006
- Forward port 8888
- Share the container folder /app/code
docker run -it -p 8888:8888 -p 6006:6006 jorditorresbcn/dlai-met:latest
In your container, clone the course repository inside the /app/code folder:
cd /app/code git clone https://github.com/jorditorresBCN/dlaimet.git
Start a jupyter notebook using this command:
jupyter notebook --ip=0.0.0.0 --allow-root
Open a browser and go to http://localhost:8888 , the password is dlaimet.
In this lab we will use the ports 8888 and 6006, if you are using a remote computer please open these ports.
If you are on windows and you are experiencing connectivity issues, please check THIS.
Task 2: Run your first TensorFlow program
Run your first TensorFlow program following these instructions:
First of all, using your browser with jupyter, open the TensorFlow examples folder and locate the mnist-book file. Try to run all the blocks t in order to check your TensorFlow installation.
The output should be something like:
... Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes. Extracting /tmp/tensorflow/mnist/input_data/t10k-labels-idx1-ubyte.gz Running epoch 1 ... 12% (117/937)
Task 3 : Analyzing the code
Using your browser with jupyter, look for these parts on the code:
- Identify how TensorFlow reads the data.
- Identify where the neural net definition is.
- Which are the layers used on this net? Which are the activation functions used on this net?
- Identify the loss and optimizer functions.
Task 4 : What differences can you see between Keras and TensorFlow?
TensorBoard is a visualization tool included with TensorFlow. You can use TensorBoard to visualize your TensorFlow graph, plot quantitative metrics about the execution of your graph, and show additional data like images that pass through it. In this lab we will use it to visualise information about our Keras network.
The code contains the variables tensorboard_dir and tensorboard_active that allow the TensorBoard execution. If you put tensorboard_active to True, TensorFlow will start to save TensorBoard data to tensorboard_dir every epoch.
Task 5: TensorBoard
Modify the tensorboard_dir value to a folder for saving the TensorBoard data. Change the tensorboard_active value to True.
Hint: You will need another terminal for running TensorBoard and Jupyter at the same time. Open a new terminal and then use these commands:
docker ps docker exec -it $DOCKER_ID /bin/bash cd /app/code/dlaimet/tf
Run the code with TensorBoard activated and then start it using the following command :
tensorboard --logdir=$tensorboard_dir Starting TensorBoard Starting TensorBoard 0.1.6 at http://localhost:6006 (Press CTRL+C to quit)
Go to http://localhost:6006 through your browser and TensorBoard will start. We recommend Google Chrome or Chromium in order to avoid compatibility and lag problems.You will see an output like:
You can run TensorBoard and Keras at same time, Tensor-Board will update the data every epoch.
Task 6: Hyperparameters
Going through the shared folder, modify the hyperparameters (batch size, number of epochs, learning rate), optimizer and loss functions and the layers of the model. Use TensorBoard and the logs to observe if you can get better accuracy and better speed with your modifications. Make charts comparing accuracy/epochs with the different configurations, you can do it manually or use TensorBoard. Take a look to the TensorBoard docs for seeing how to merge different runs in one chart.
Task 7: Lab Report
Build a lab report with a brief explanation of the previous Tasks. Follow the indications of your teacher about how to create your lab report and how to submit it.
My thanks to Francesc Sastre for helping me with the preparation of this lab.