Installing Keras on Windows – January 2021

I’m a big fan of both the Keras and PyTorch neural network libraries. I tend to use one library for several months, and then switch to the other for several months, depending on the project I’m working on. It’s possible, but very difficult, to use both libraries more or less at the same time. The libraries are very complicated, and although the principles are the same, the APIs are very different.

I’d been using PyTorch for several months — and I hadn’t used Keras during that time. So I figured I’d install Keras from scratch to see what had changed. Keras is now included in TensorFlow, so instead of installing the two libraries separately, I decided to install just TensorFlow and use the embedded Keras.

I mostly use Windows for experimentation and development, and then Linux for training large systems. First I installed Ananconda Python version Anaconda3-2020.02 which includes Python 3.7.6. Installing Ananconda Python is quick and easy. See the first half of my blog post at https://jamesmccaffreyblog.com/2020/05/25/installing-pytorch-1-5-for-cpu-on-windows-10-with-anaconda-2020-02-for-python-3-7/.

Next I went to pypi.org and found the TensorFlow .whl installation file tensorflow_cpu-2.4.0-cp37-cp37m-win_amd64.whl. I used the CPU-only version, which I prefer for experimentation — lots less to go wrong than the GPU+CPU version. One of the requirements is “Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019”. Because I had Visual Studio on my machine, I had the required C++ DLL files.



I right-clicked on the appropriate .whl file link and then selected the “Save link as . .” and then saved the .whl file to my local machine at C:\Keras\WheelFiles. I issued the command “pip install (file.whl)” and installation . . . worked the first time. Sometimes miracles do happen.

I tested the system and verified that I had TensorFlow 2.4.0 installed and the embedded Keras 2.4.0 too.

I concluded my experiment by running a non-trivial demo of a generative adversarial network, using code I found on the excellent Machine Learning Mastery web site. The GAN demo is to generate y = x^2. The only real change I had to made was to edit a couple of import statements to use the embedded Keras instead of a standalone Keras:

from keras.models import Sequential
from keras.layers import Dense

to

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

Installing Keras or PyTorch is not trivial, but the process is dramatically easier than it used to be as recently as a year ago. The major issue is still package version incompatibilities. Keras and PyTorch have many dozens of package dependencies. Distributions like Anaconda go a long way in mitigating Python package dependency hell, but distributions aren’t magic and dependencies can still cause some major headaches.


Left: “Chandu the Magician” is a 1932 movie. Chandu (wearing the Arab headdress, played by Edmund Lowe) had mystic powers he learned in the East. The movie holds up surprisingly well and the special effects are quite good even today. Chandu had to stop the evil Roxor (in black) played by Bela Lugosi. Lugosi is best known as “Dracula” (1931). Center: Chandu’s niece is a damsel in distress in the movie. Right: In the “The Return of Chandu” (1934), this time Lugosi played the hero Chandu. He had to stop a cult of Black Magic sorcerers. Chandu was the inspiration for the comic book and movie character “Dr. Strange” (2016).

This entry was posted in Keras, Machine Learning, PyTorch. Bookmark the permalink.