How to install TensorFlow (with or without Docker) | by Beth Graham | Feb, 2024


If you are interested in developing deep learning algorithms, you may wish to get started with TensorFlow. This machine learning platform, which was developed by Google, can be used to create powerful models with applications like computer vision, speech recognition, and natural language processing.

The first step in using TensorFlow is installing it on your computer. The simple (but less recommended) approach is to install TensorFlow directly to your system. The slightly more challenging (but recommended) approach is to install TensorFlow within a Docker container. Below, you will find instructions for both approaches.

Installing TensorFlow directly to your system

If you wish to install TensorFlow to your system without the use of a Docker container, follow the instructions here.

Installing TensorFlow from the command line is relatively simple. You can find quick start instructions here and more complex instructions here. The purpose of this article is to make these instructions — which can be somewhat technical — more accessible to a broader audience.

Before you install TensorFlow, you need to make sure that you have up-to-date versions of Python and pip. Installing TensorFlow requires Python 3.9–3.11 and pip version >19.0 (or >20.3 for macOS). To see which versions of Python 3 and pip you have, run the following code at the command line:

# Check python3 version
python3 --version

# Check pip version
python3 -m pip --version

If you need to upgrade your version of Python 3 or you don’t have it installed yet, you can download the latest release here.

pip should be included with your installation of Python 3. You can upgrade your version of pip and install the tensorflow package using the following code:

# Upgrade pip
pip install --upgrade pip

# Install current stable release of TensorFlow
pip install tensorflow

You can verify that the tensorflow package was installed correctly by running:

# Verify TensorFlow installation
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

If TensorFlow is installed correctly, this code should return a tensor. A tensor is a multidimensional array of elements of the same data typeand is represented by the tf.Tensor object in TensorFlow. A tensor has two properties, shape and data type. You can learn about the various properties of tensors at the TensorFlow “Introduction to Tensors” Guide. The tensor produced by the code above looks like this:

tf.Tensor(-927.67163, shape=(), dtype=float32)

Don’t worry if you also see a message that says something like, “This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.” That’s ok — it’s not an error. For now, you can probably ignore it.

Docker is a tool that can be used to package applications and their dependencies in isolated environments called containers. If you don’t have Docker on your computer already, you will need to install it. You can find instructions for installing Docker here.

Once you have installed Docker, you might need to do slightly different things to use Docker from the command line depending on which operating system you have. On a Mac, you will need to navigate to your Applications and folder and open the Docker application before running any commands at the command line. Otherwise, you may get a notification asking whether the docker daemon is running. On Linux, you may need to preface each docker command with “sudo” — alternately, you can follow the instructions here.

You can verify that your Docker installation is working by running the following code:

# Verify that your installation of Docker is working
docker run hello-world

If your installation is successful, you should see this message:

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

Once you have Docker installed, you are ready to create a Docker container. This container will be used to isolate TensorFlow and its dependencies from the rest of the system. TensorFlow programs are run within the container.

A Docker image is a template containing instructions for how to create a container. Google provides Docker images that are compatible with TensorFlow at the tensorflow/tensorflow Docker Hub repository. In order to create the TensorFlow Docker container, you need to download and run one of these Docker images.

You can download the stable version of the TensorFlow Docker image by using the “docker pull” command:

# Pull the stable TensorFlow Docker image
docker pull tensorflow/tensorflow

The pull command downloads the Docker image, but does not run it. When you are ready to run the image, you can use the run command. (Note: If you try to run a package that you have not downloaded, the run command will automatically pull the package for you.)

# Run the stable TensorFlow Docker image
docker run -it --rm tensorflow/tensorflow bash

Once you see the output below, you are working inside of the tensorflow container:

If you are installing TensorFlow on your own computer and you are the root user, you can probably ignore the warning on this screen.

Now that you are working inside your TensorFlow container, you are ready to install TensorFlow. The instructions are similar to installing the package directly on to your system. First, you need to make sure you have an up-to-date version of pip. Then, you will use pip to install the tensorflow package.

# Upgrade pip
pip install --upgrade pip

# Install current stable release of TensorFlow
pip install tensorflow

TensorFlow is a Python package, so you will start a Python session and import the tensorflow package as tf.

# Start a Python session
python3

# Import the tensorflow package
import tensorflow as tf

Now you are ready to start using TensorFlow! Here is a very simple example, in which we create and print a tensor:

x = tf.constant([[1., 2., 3.],
[4., 5., 6.]])
print(x)

For more complicated examples of how to use TensorFlow, check out the official tutorials and user guides here.

If you want to exit Python3 from within the TensorFlow container, use:

exit()

If you want to exit the TensorFlow container, use:

exit

That’s it! You’re up and running!

In some cases, more advanced TensorFlow users may wish to specify which type of hardware the Docker container can use. Both central processing units (CPUs) and graphics processing units (GPUs) are hardware components that perform computations. CPUs are more powerful, but GPUs are more efficient at performing certain types of tasks, including deep learning algorithms. More information on CPUs and GPUs can be found here. Information on how to run CPU-only or GPU Docker containers can be found here.



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*