Setup VS Code for TensorFlow

yasiru withanage
setup-pc-for-tensorflow
3 min readJun 14, 2020

Machine Learning

Summary

  1. Install Python
  2. Setup VS Code
  3. Virtual Environment
  4. Install TensorFlow 2.0
  5. Install Jupyter Notebook (Optional)
  6. Testing Environment

Implementation

Install Python

Download Python 3.7.6 from www.python.org(Currently, Tensorflow doesn’t support Python 3.8). I would suggest to install it with “customize installation” option and allow all users.

After installation, check the Python version on the terminal. If there are multiple versions of python installed in the machine then change PATH in environment variable to the installed version and restart terminal to check version.

Setup VS Code

Download and install VS Code if not already installed.

Install the following VS Code extension from the marketplace.

Python VS Code Extension

Note: Make sure you have installed the latest version of the extension.

First time, open the VS Code Command Palette with the shortcut CTRL + SHIFT + P (Windows) or Command + SHIFT + P (macOS) in VSCode and select “Python: Select Interpreter” command. It will display all installed versions. Select the appropriate python environment where Jupyter notebook is installed.

To create new Jupyter notebook, open VS Code Command Palette again and run the “Python: Create Blank New Jupyter Notebook” command.

Installing Virtualenv using pip3

Virtualenv is installed by default on all DreamHost servers for Python 2 versions. If you’re working with Python 3, you should install virtualenv using pip3.

You may want to first upgrade pip3.

[server]$ python3 -m pip install — upgrade pip
These instructions assume you’ve already installed a custom version of Python 3. After it’s installed and your shell is using this version, run pip3 to install virtualenv:
[server]$ pip3 install virtualenv
Collecting virtualenv
Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
100% |████████████████████████████████| 1.8MB 367kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0

then

[server]$ virtualenv randomName

To activate the new virtual environment, run the following:

[server]$ virtualenv randomName/bin/activate

If this script doesn’t execute under execution policies, run this command on the power shell.

& : File C:\Users\Yasiru\Documents\Machine Learning\Test Project\01\venv\Scripts\activate.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:3 + & “c:/Users/Yasiru/Documents/Machine Learning/Test Project/01/venv/Sc … + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess

Run

Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser

docs.microsoft.com execution policies

Install TensorFlow 2.0

TensorFlow is open source deep learning framework by Google, helps us to build and design Deep Learning models.

For simplicity, we will install CPU version of TensorFlow.

python -m pip install — upgrade pip
pip install tensorflow==2.0

It will install all supportive extensions like numpy …etc.

Note: Install the GPU version of TensorFlow only if you have an Nvidia GPU. It is good and recommended for better performance. It needs to Install/Update nvidia driver, cuda toolkit, cuDNN and then run following command to install

pip install tensorflow-gpu

For more information, check out the official guide here.

The next is to install Matplotlib- a Python library for 2D plotting and can work together with NumPy.

pip install matplotlib

Install Jupyter Notebook

Jupyter Notebook is web based interactive environment for writing the code, creating & sharing files and doing visualizations as well.

run following command to install it:

pip install jupyter

Start the notebook server from the command line:

jupyter notebook

You should see the notebook open in your browser.

Testing Environment

Now, it is time to test the environment.

Create a new Jupyter book in VS Code and run the following code to test :

import tensorflow as tf
print(‘tensorflow version’, tf.__version__)

x = [[3.]]
y = [[4.]]
print(‘Result: {}’.format(tf.matmul(x, y)))

The output should be the following:

tensorflow version 2.0.0
Result: [[12.]]

--

--