TensorFlow Guide V2

TensorFlow Guide V2

What is TensorFlow?

TensorFlow is an open-source software library designed for high-performance numerical computation. Developed by the Google Brain team, TensorFlow excels in handling deep learning and machine learning algorithms, making it particularly useful for tasks involving artificial intelligence, such as neural networks. Its flexible architecture allows for easy computation across a variety of platforms (CPUs, GPUs, and TPUs), making it accessible for users with different levels of computing resources. TensorFlow's ability to execute complex calculations with minimal code makes it an excellent tool for both beginners and advanced researchers in the field of computational science.

How to use TensorFlow on Storrs HPC?

First, we need to create a custom conda environment. If you are not familiar:

Miniconda Environment Set Up - Storrs HPC - UConn Knowledge Base

The following steps sets up an environment for tensor flow with GPU support:

Connect to gpu

(base) [netid@login4 ~]$ srun --x11 -N 1 -n 12 -p general-gpu --gres=gpu:1 --pty bash

Create a Custom Conda Environment

(base) [netid@gpu33 ~]$ conda create -n tf-gpu (base) [netid@gpu33 ~]$ conda activate tf-gpu

Install Packages

(tf-gpu) [netid@gpu33 ~]$ conda install python=3.12.1 (tf-gpu) [netid@gpu33 ~]$ conda install nvidia::cuda (tf-gpu) [netid@gpu33 ~]$ conda install conda-forge::python=3.12.1 cudnn tensorflow

To install a specific version of cuda (11.6.0 as an example):

(tf-gpu) [netid@gpu33 ~]$ conda install nvidia/label/cuda-11.6.0::cuda

For a full cuda installation:

(tf-gpu) [netid@gpu33 ~]$ conda install -y -c nvidia cuda=11.6.0 cuda-tools=11.6.0 cuda-toolkit=11.6.0 cuda-version=11.6.0 cuda-command-line-tools=11.6.0 cuda-compiler=11.6.0 cuda-runtime=11.6.0

Check Installation

Use python3 to check if tensorflow is installed and working (you can ignore the warnings).

python3 >>> import tensorflow as tf

Check version.

>>> print("TensorFlow version:", tf.__version__) TensorFlow version: 2.14.0

Detect GPU.

>>> print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU'))) Num GPUs Available: 1

Run TensorFlow

A simple TensorFlow operation:

The following script creates two constants and computes their sum using TensorFlow. This operation verifies that TensorFlow can execute computations.

nano test_tensorflow.py

Enter the following into the script:

import tensorflow as tf # Create TensorFlow objects for addition a = tf.constant([1.0, 2.0]) b = tf.constant([3.0, 4.0]) # Use TensorFlow to add the two objects result = tf.add(a, b) print("Addition result:", result.numpy())

Save and run the script.

(tf-gpu) [netid@gpu33 ~]$ python test_tensorflow.py Addition result: [4. 6.]

Running TensorFlow on Jupyter

There are two options:

  1. Use VSCode (Jupyter on VSCode - Storrs HPC - UConn Knowledge Base)

  2. Use JupyteHub (Jupyter Guide - Storrs HPC - UConn Knowledge Base)

 

Please do not hesitate to reach out if you have any questions or if issue arise.

Happy TensorFlowing!

~

Storrs HPC

hpc@storrs.uconn.edu