1. Sign In

Setting up your TensorFlow environment

Setting up your environment is easily the most time consuming and frustrating part of writing code. Even if you've been writing code for 20+ years it's an absolute pain. This is what we signed up for - we came to code and instead we're clicking buttons and interpreting cryptic error messages.

I'll show you one way to do it. It's not the only way. I guarantee you it isn't the right way, because the right way is the way that suits you. If you're far enough along to know the answer to that then you don't need this guide.

There are a lot of shortcuts, abstractions, and virtualizations. I'm not about that life. Let's take the old school route where you gain some insight & experience along the way. It's not the fastest to configure but it will impart the knowledge you need to know.

Operating System

I'm writing this on OS X but I'm going to spin up an Amazon EC2 instance to set up. I'll do my best to note the differences as we go. I'll give some advice to my windows brothers & sisters but you might have to jump through extra hoops here.

All of the examples assume you are at the terminal running these commands directly. I've met more than a few folks who are nervous to run commands in a terminal. I get it, it's daunting. But this is the best way to understand what is going on and take the most control of your environment. It is not an easy road but fortunately learning enough to do the job correctly is fairly easy.

Unfortunately I can't also cover how to start a terminal session, ssh, etc so you'll have to know enough to do that.

Example: Universal Sentence Encoder with Keras and TensorFlow

We'll use our example model from the previous article to test our setup. You should have two files example.py and example-val.py. Be sure to have these files handy and copied onto your linux, OS X, or windows machine where you'll be running it.

Prerequisite: Versioning

Versioning is a fickle beast. To simplify the great semver the format looks like this: $major.$minor.$patch.$build. For our purposes I'll say simply that when $major changes we can expect compatibility issues, and certainly that is the case here.

Let's start with python. Python 3 will run code meant for python 2 but not vice-versa. Pretty much everyone is using python 3 these days so there isn't much worry on that front, but it's important to point out that it is not uncommon that python 2 and 3 will be installed in parallel on the same machine. When this is the case often the command python will be version 2 and python3 will be the version 3 you want. It's annoying but that's the way it is.

It is a fairly universal flag that --version will give you the version output for your tool. In my case:


❯ python3 --version
Python 3.8.5

Now let's talk TensorFlow. Versioning for this is incredibly frustrating. Not only are version 1 and 2 incompatible but the relationship to Keras is not clear. To make matters worse there is a keras python package to import and there is a tf.keras module as well. I'm not going to get into the specifics. I'll just say you should prefer TensorFlow 2.0 and the tf.keras usage.

Step 1: Dependencies & Python3

I just launched a "Amazon Linux 2 AMI" instance. Here are the commands needed to get python in order:


sudo yum groupinstall "Development Tools"
sudo yum install python36
sudo pip-3.6 install --upgrade pip

On OS X you'll need to do:


xcode-select —install

Choose to install the command line tools and follow the instructions at https://brew.sh/ to install HomeBrew. Afterwards run brew install python3.

On windows you can download and install the python MSI.

Step 2: pip

Pip is python's package manager. You'll need to use this often to install new packages and occasionally uninstall conflicting ones. Because this process is somewhat opaque it isn't always clear when this happens.

Here's what we get when we try and run the example right off the bat:


[ec2-user@ip-172-31-6-130 ~]$ ls -lh
total 8.0K
-rw-rw-r-- 1 ec2-user ec2-user 1.8K Sep 26 19:21 example.py
-rw-rw-r-- 1 ec2-user ec2-user  743 Sep 26 19:20 example-val.py
[ec2-user@ip-172-31-27-144 ~]$ python3 example.py
Traceback (most recent call last):
  File "example.py", line 1, in <module>
    import numpy as np
ModuleNotFoundError: No module named 'numpy'

No module named 'numpy'. That's a pretty important one. Let's use pip for it:


[ec2-user@ip-172-31-6-130 ~]$ pip3 install numpy
Defaulting to user installation because normal site-packages is not writeable
Collecting numpy
    Downloading numpy-1.19.2-cp36-cp36m-manylinux2010_x86_64.whl (14.5 MB)
        |████████████████████████████████| 14.5 MB 4.2 MB/s
Installing collected packages: numpy
Successfully installed numpy-1.19.2

Let's try again:


[ec2-user@ip-172-31-6-130 ~]$ python3 example.py
Traceback (most recent call last):
    File "example.py", line 2, in 
    from tensorflow.keras import models
ModuleNotFoundError: No module named 'tensorflow'

So what to do? If you guessed 'pip3 install tensorflow' you'd be absolutely right. To minimize version issues though we'll specify the version desired. 2.3.0 in this case.


[ec2-user@ip-172-31-6-130 ~]$ pip3 install tensorflow==2.3.0
Defaulting to user installation because normal site-packages is not writeable
Collecting tensorflow==2.3.0
    Downloading tensorflow-2.3.0-cp36-cp36m-manylinux2010_x86_64.whl (320.4 MB)
        |████████████████████████████████| 320.4 MB 14 kB/s
...

[ec2-user@ip-172-31-6-130 ~]$ python3 example.py
2020-09-26 19:28:06.880529: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory
2020-09-26 19:28:06.880568: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "example.py", line 4, in <module>
    from keras.utils.np_utils import to_categorical
ModuleNotFoundError: No module named 'keras'

[ec2-user@ip-172-31-6-130 ~]$ pip3 install keras
Defaulting to user installation because normal site-packages is not writeable
Collecting keras
  Downloading Keras-2.4.3-py2.py3-none-any.whl (36 kB)
Requirement already satisfied: scipy>=0.14 in ./.local/lib/python3.6/site-packages (from keras) (1.4.1)
Requirement already satisfied: h5py in ./.local/lib/python3.6/site-packages (from keras) (2.10.0)
Requirement already satisfied: numpy>=1.9.1 in ./.local/lib/python3.6/site-packages (from keras) (1.18.5)
...

[ec2-user@ip-172-31-6-130 ~]$ python3 example.py
2020-09-26 19:28:48.339567: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory
2020-09-26 19:28:48.339605: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "example.py", line 5, in <module>
    import tensorflow_text
ModuleNotFoundError: No module named 'tensorflow_text'

[ec2-user@ip-172-31-6-130 ~]$ pip3 install tensorflow_text
Defaulting to user installation because normal site-packages is not writeable
Collecting tensorflow_text
  Downloading tensorflow_text-2.3.0-cp36-cp36m-manylinux1_x86_64.whl (2.6 MB)
     |████████████████████████████████| 2.6 MB 4.7 MB/s
...

[ec2-user@ip-172-31-6-130 ~]$ pip3 install tensorflow_hub
Defaulting to user installation because normal site-packages is not writeable
Collecting tensorflow_hub
  Downloading tensorflow_hub-0.9.0-py2.py3-none-any.whl (103 kB)
     |████████████████████████████████| 103 kB 5.9 MB/s
Requirement already satisfied: six>=1.12.0 in ./.local/lib/python3.6/site-packages (from tensorflow_hub) (1.15.0)
Requirement already satisfied: numpy>=1.12.0 in ./.local/lib/python3.6/site-packages (from tensorflow_hub) (1.18.5)
Requirement already satisfied: protobuf>=3.8.0 in ./.local/lib/python3.6/site-packages (from tensorflow_hub) (3.13.0)
Requirement already satisfied: setuptools in /usr/lib/python3.6/dist-packages (from protobuf>=3.8.0->tensorflow_hub) (36.2.7)
Installing collected packages: tensorflow-hub
Successfully installed tensorflow-hub-0.9.0

Run it!

Here's the fun part - you can now train and evaluate the model.


[ec2-user@ip-172-31-6-130 ~]$ python3 example.py
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
dense (Dense)                (None, 64)                32832
_________________________________________________________________
dense_1 (Dense)              (None, 4)                 260
=================================================================
Total params: 33,092
Trainable params: 33,092
Non-trainable params: 0
_________________________________________________________________
Epoch 1/4
1/1 [==============================] - 0s 464us/step - loss: 1.3944 - accuracy: 0.1111
Epoch 2/4
1/1 [==============================] - 0s 346us/step - loss: 1.3608 - accuracy: 0.3889
Epoch 3/4
1/1 [==============================] - 0s 381us/step - loss: 1.3292 - accuracy: 0.6667
Epoch 4/4
1/1 [==============================] - 0s 339us/step - loss: 1.2995 - accuracy: 0.8333

Don't worry if you're getting warnings and other messages from TensorFlow. Also your values won't match exactly. Right now your directory should look something like this:


./example.py
./example-val.py
./data
./data/output
./data/output/example
./data/output/example/assets
./data/output/example/saved_model.pb
./data/output/example/variables
./data/output/example/variables/variables.data-00000-of-00001
./data/output/example/variables/variables.index

Evaluation Time:


[ec2-user@ip-172-31-6-130 ~]$ python3 example-val.py
Type your evaluation phrase below
My friends are mice and cats
    ['other', 'aerospace', 'animals', 'body_parts']
>	[0.24444461 0.25173247 0.25702375 0.24679916]
>	animals

Type your evaluation phrase below
I have a bad knee
    ['other', 'aerospace', 'animals', 'body_parts']
>	[0.22769259 0.2435894  0.263064   0.265654  ]
>	body_parts

Type your evaluation phrase below
It's time to blast off into space
    ['other', 'aerospace', 'animals', 'body_parts']
>	[0.21890956 0.27392063 0.24783897 0.25933087]
>	aerospace

Type your evaluation phrase below
quit
Bye!

Next Steps

It did take some effort, but we were able to get a fresh linux environment set up from scratch, or maybe your own machine. Afterwards we could run our very basic example model and get some results.

What we didn't cover here are IDEs, editors, how to structure your projects, and so many other fun topics. Again, these are largely matter of preference. I'll tell you that when it comes to python I prefer a simplistic text editor with basic syntax highlighting. I don't use integrated terminals or immediate output or anything like that. I have found jupyter notebooks helpful to load an expensive model and then quickly evaluate against it. VSCode does everything I need and I'm happy with it. Some folks use Atom or Sublime and those are essentially the same.

Next we'll dockerize it and make a small app. After all, what good is our model if nobody can interact with it?