How to Install and Setup TensorFlow Virtual Environment platform.
image source<datasciencelearner.com>

How to Install and Setup TensorFlow Virtual Environment platform

Kwesi Welbred
4 min readOct 20, 2020

--

Python virtual environments are used to isolate package installation from the host system. This is done to avoid the Installed packages or libraries within a virtual environment not to conflict with the host or the local system setup. This means that each project can have its own dependencies, regardless of what dependencies every other project would have. So, in this wise, your tensorflow project would have a separate working environment from other project you might create.

You must also make sure that TensorFlow has the version of Python that it is compatible with. The best way to accomplish this is with an Anaconda environment. Besides, you can create different python virtual environment for different projects as well.

Now, having known this, let’s get started:➡

System requirement needed✨

Python 3.5–3.8
Python 3.8 support requires TensorFlow 2.2 or later.
pip 19.0 or later (requires manylinux2010 support)

Ubuntu 16.04 or later (64-bit)
macOS 10.12.6 (Sierra) or later (64-bit) (no GPU support)
Windows 7 or later (64-bit)

Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019
Raspbian 9.0 or later
GPU support requires a CUDA®-enabled card (Ubuntu and Windows)
Note: Installing TensorFlow 2 requires a newer version of pip.

Hardware requirements
Starting with TensorFlow 1.6, binaries use AVX instructions which may not run on older CPUs.
Read the GPU support guide to set up a CUDA®-enabled GPU card on Ubuntu or Windows.

Incase you’re confuse with CPU vs GPU in Machine Learning follow this URL

  1. Install the Python development environment on your system
    Check if your Python environment is already configured:

Note: TensorFlow Requires Python 3.5–3.8, pip and venv >= 19.0

Run the following code to check your pip and python version

$python3 — version
$pip3 — version

If these packages are already installed, skip to the next step.
Otherwise, install Python: here and make sure that you have anaconda installed on your machine click here to install

Having done this, install inbuilt IDE in the anaconda environment which would be used for this tutorial

conda install -y jupyter

2. Create a virtual environment (recommended)

Like I said earlier, each environment that you create can have its own Python version, drivers, and Python libraries. I suggest that you create an environment to hold the Python instance for your project.
Use the following command to create your environment. I am calling my environment tensorflowEnv, you can name yours whatever you like.

#Your tensorflow environment
conda create — name tensorflowEnv python=3.7

#Activate the environment
conda activate tensorflowEnv

#add Jupyter support to your new environment.
conda install nb_conda

#Install packages within a virtual environment without affecting the host system setup. Start by upgrading pip:
pip install — upgrade pip

# show packages installed within the virtual environment
pip list

“””add new or additional packages/ libraries to your environment such as matplotlib and requestHtml which was not part of my virtual packages when i was doing this. Just do this to avoid any difficult task.
You can ignore this as these might be added automatically in virtual environment as we progress with the steps, but should incase you get any ModuleNotFound error, come back and do this.
“””

pip install matplotlib
pip install requestHtml

#do a pip list to check your updates
deactivate # don’t exit until you’re done using TensorFlow

3. Installing TensorFlow : CPU | GPU
#Install TensorFlow for CPU Only
The following command installs TensorFlow for CPU support. Even if you have a GPU, it will not be used.

conda install -c anaconda tensorflow

#Install TensorFlow for GPU and CPU
The following command installs TensorFlow for GPU support. All of the complex driver installations should be handled by this command.

conda install -c anaconda tensorflow-gpu

#Install and upgrade tensorflow package dependencies inside your virtual environment

pip install — upgrade tensorflow

#verify the installation

python -c “import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))”

4.Register your Environment
The following command registers your tensorflow virtual environment in jupyter kernel.
And make sure, you do “conda activate” in your new tensorflow environment.

conda activate tensorflowEnv
python -m ipykernel install — user — name tensorflow — display-name “Python 3.7 (tensorflowEnv)”

5.Testing your Environment✔
You can now start Jupyter notebook. Use the following command.

jupyter notebook

You can now run the following code in your jupyter notebook to check that you have the versions expected.

# What version of Python do you have? ..
import sys
import tensorflow.keras
import pandas as pd
import sklearn as sk
import tensorflow as tf

print(f”Tensor Flow Version: {tf.__version__}”)
print(f”Keras Version: {tensorflow.keras.__version__}”)
print()
print(f”Python {sys.version}”)
print(f”Pandas {pd.__version__}”)
print(f”Scikit-Learn {sk.__version__}”)
gpu = len(tf.config.list_physical_devices(‘GPU’))>0 ..
print(“GPU is”, “available” if gpu else “NOT AVAILABLE”)

CLOSING YOUR PROJECT
[type] ctrl + c to shutdown the jupyter kernel
conda deactivate to deactivate your environment
exit to exit the cmd

OPENING YOUR PROJECT
cmd
conda activate tensorflowEnv
jupyter notebook

👏👏👏👏👏

🙌
References
TensorFlow official website Installation guide [https://www.tensorflow.org/install]

Instructor: Jeff Heaton, McKelvey School of Engineering, Washington University in St. Louis
Applications of Deep Neural Networks → Software Installation

For more information visit the class website.[https://sites.wustl.edu/jeffheaton/t81-558/]

--

--