The First Step of Being an AI Developer: Install Python, TensorFlow & Keras on Your macOS or Windows



Artificial intelligence based on machine learning methods, especially deep learning, has become more and more dominant in recent years. Interestingly, deep learning and its related concept, artificial neural networks (ANNs), are certainly not something new. As a matter of fact, the term, deep learning, can be traced back to 1986, and ANN is even older (1950s). However, for a very long time, they didn’t get too much attention since lacking a notable breakthrough. That is until AlexNet showed up in the ILSVRC (ImageNet Large Scale Visual Recognition Challenge) in 2012. With an outstanding 15.3% error rate in the object recognition task (P.S., it’s about 10% lower than the best score before 2012), it not only won the first place of the competition in that year but also successfully brought artificial neural networks and deep learning back to the mainstream of AI.


Today, with the help of the tools such as TensorFlow and Keras, anyone can build his or her own deep learning model at home without going through too much trouble. But before that, getting your computer ready for the job is necessary. In this article, we will help you set up the environment for developing machine learning algorithms without discussing too many technical details (if you’re interested in getting deeper in artificial intelligence, please subscribe to our blog to get notices for our future articles). Meanwhile, some common problems that one may encounter during the process will be addressed. Finally, a downloadable source code that we wrote about a basic form of ANN called multilayer perceptron (MLP) is provided to you, so that you can test everything that you’ve installed, and at the same time feel the excitement of a working artificial neural network functioning on your computer!



Python


  • What is Python, and why it’s needed?

I recalled when I first entered a graduate program, the first skill I learned is how to code in MATLAB. But a few years later, thing changed. People started to talk about Python, the new star in programming languages that owns the following advantages:

  1. Its syntax is simple and concise, which makes the language easy to read and learn.

  2. Python is supported by a complete set of open-source libraries designed for data analysis, scientific computation and machine learning. P.S., Famous examples include: NumPy (for conducting scientific computation), Pandas (for handling data), Matplotlib (for drawing scientific graphs), Scikit-learn (for machine learning), etc.

  3. It’s easy to install (both on macOS and Windows), especially with the help of a Python distribution.

  4. Last but not least, it’s free! This gives Python a great benefit over MATLAB.


With the said upper hands, Python has become arguably the most frequently used language in data science and machine learning. Thus, to enter the field of artificial intelligence, the first thing we must do is to prepare your computer environment for Python developing, and we’re going to show you one of the easiest ways to do this in the next section.


  • How to install Python?


Despite the fact that you can find anything you need in respect of Python from its official website, we strongly recommend you to get Python with the help of Anaconda, an open-source distribution of Python that helps you to simplify your Python installation, package management and deployment. You can go to the download page of Anaconda by clicking here.


For both Mac and Windows users, installing Anaconda is a very simple process. First, go to the download page and get the correct file matching your operating system. Note that there are two versions available: one for Python 3.x (e.g., Python 3.7) and another for Python 2.x (e.g., Python 2.7). You can choose either one to download according to your preference (P.S., in order to help you decide, here is an article telling you the major differences between Python 3.x and Python 2.7.x. If you don’t want to go through so many details about the differences between Python 3.x and 2.x, you can follow our footstep by choosing the “Python 3.7 version”).


The download page of Anaconda.

There are two versions available. You are free to choose any one of them. In this article, we took Python 3.7 version as our example.

Next, open the downloaded file, and follow all the instructions shown on your screen. The installing processes are basically the same for Mac and Windows except for some minor differences in their interfaces, so don’t worry too much about it. The following is a quick walkthrough of the procedure (P.S., using macOS as an example):



Some detailed information about Anaconda. Feel free to read it, but let's be honest: most people just skip it...

Scroll down in the detailed info page, you can find a long list of packages’ names. These are the packages that are going to be installed in your computer.

Information about license agreement. Again, most people just skip it…

Click “agree” to continue installing (BTW, who will click “disagree”?).

Choose according to your need.

If everything is okay to you, click “install” to initiate installation process.


You can choose whether to install Microsoft Visual Studio Code or not. It’s unnecessary for our installation.


For Windows’ users, there is another advantage of installing Anaconda. That is, you don’t have to set up any environmental variable. Upon finishing the installation, you will find “Anaconda Prompt” in the Start menu. Open it and you are set to go!


We will use Anaconda Prompt to install all the packages we need (on Windows) in this article.

This is how Anaconda Prompt looks like if you open it. As you can see, it’s very similar to the Command Prompt on Windows computers.

  • Checking your Python installation:


After the installation, you can find the icon of Anaconda Navigator in your computer, and the following is what you’ll see when you click it open:


The icon of Anaconda Navigator.

The user interface of Anaconda.

Tutorial on how to use Anaconda is a topic for another article. The point now is to check out whether we’ve successfully installed Python. Please open your Terminal on Mac or Anaconda Prompt on Windows and type the following command before pressing Enter:


python --version

If your Terminal or Anaconda Prompt does not recognize this command, your installation is unsuccessful (but with the method we introduced here it’s unlikely). Otherwise, you should see the version number of your Python listed on the screen:


Check Python’s version on Windows with Anaconda Prompt.

Check Python’s version on macOS with Terminal.

Also, to enter the coding environment of Python, simply type “python” on the Terminal / Anaconda Prompt and execute it. As to exit the environment, execute "exit()" instead.


Here, we gave our demonstration on macOS. The commands used in Windows are exactly the same.

P.S., If you're interested in the version of your Anaconda, the following command can help you:

conda --version

  • The pitfall of Python 3.7.x:


Before we move on, there is an important thing to care about for those who have Python 3.7.0 or up on your computer (if your Python’s version is lower than Python 3.7.x, you are good to skip this section), that is, TensorFlow is currently NOT compatible with Python 3.7.x! If you try to install TensorFlow under such condition, you will get the following message, and the installation cannot be completed:


“Could not find a version that satisfies the requirement tensorflow (from versions: ) No matching distribution found for tensorflow”

Luckily, there is an easy solution for this problem: just downgrade your Python to version 3.6.x, and the following command allows you to do that (we took Python 3.6.5 as an example):


conda install python=3.6.5




The above illustration is for macOS. To downgrade Python on your Windows, just use the exact same command in Anaconda Prompt.



TensorFlow


After you're done installing Python, it’s time for us to turn to our main character – TensorFlow.


  • What is TensorFlow?


TensorFlow is an open-source library for machine learning developed by Google Brain Team and released under Apache 2.0 open-source license on November 9, 2015. It provides all tools you need to build, train and test your artificial neural networks (introduction about TensorFlow in detail is the topic for future articles; for now, please refer to its official website).


One thing worth noticing: there are actually two versions of TensorFlow: the CPU & GPU version. The former uses only CPUs to do the calculation, while the later exploits both CPUs and GPUs (although GPUs will be given priority). So what’s the difference between these two? Well, simply put, GPUs have a better performance in floating point operations, which is often required in deep learning, than CPUs. Therefore, the GPU version of TensorFlow is more efficient than the CPU version, which means that you can spend less time on training your neural networks, especially when working with big data.


  • How to install TensorFlow?


Here, we introduce how to quickly and easily install TensorFlow with Terminal (on Mac) or Anaconda Prompt (on Windows) by simply entering a few commands.


For both Mac and Windows users, just type in the following line to your Terminal / Anaconda Prompt and press enter to execute it:


conda install tensorflow

And as soon as it’s done, the installation of the CPU-version TensorFlow is complete. As simple as that!


But if your Mac or Windows computer is using a NVIDIA’s graphics card, the GPU version of TensorFlow is recommended since it’s more efficient. To do so, you will first need to install two more things: Cuda and cuDNN libraries, and the following is a brief guide on how to do that (Warning: this process is a little bit troublesome comparing to the other installation included in this article. If that’s too much for you right now, just get the CPU version and remember that you can come back to install the GPU version later):


- For both Mac and Windows users: First, make sure that you have a NVIDIA graphics card with Cuda-enabled GPUs in your computer! If you don’t, you should just get the CPU-version TensorFlow. This site will help you to do the check.


- Download the correct Cuda installation file for your operating system. When it finishes, open the file and follow the installing instructions. You can obtain the files you need from here.


Be sure to choose the file with the label "(local)".

- After you finish installing Cuda, go to the download page of cuDNN. But before you can download anything, you must first become the member of NVIDIA Developer Program. Click "Join now" to become a member.


Click "Join now" to become a member of NVIDIA Developer Program.

- Once you get an account for NVIDIA Developer Program, log in and go to the download page again. Checking the "I Agree To the Terms of the cuDNN Software License Agreement" on the page, and you will see files for download. Be sure to get the correct file for your Cuda’s version and operating system.


Choose the correct category according to the version of Cuda you’ve installed earlier.

Then, download the correct file for your operating system.

- Now, unzip the cuDNN file and install it by following the instructions on this page (do follow the correct instructions for your operating system).


After that, you can install the GPU version of TensorFlow by typing the following command to the Terminal / Anaconda Prompt:


conda install tensorflow-gpu

OK, it’s all done now! Enjoy your GPU-version TensorFlow!


Note that if you encounter the following error: “Could not find a version that satisfies the requirement tensorflow (from versions: ) No matching distribution found for tensorflow”, it’s likely that the version of your Python is too new for TensorFlow. Please check out the previous section of this article: “The pitfall of Python 3.7.x” for a solution.


  • Checking your TensorFlow installation:


You can test your TensorFlow in the following way. First, open your Terminal on Mac or Anaconda Prompt on Windows, type in “python” and press enter to initiate the coding environment for Python, and then use the following commands to show TensorFlow's version:


import tensorflow as tf
tf.__version__

If the version number of TensorFlow is successfully shown without any error, your installation should have no problem.



OpenCV


  • What is OpenCV?


The full name of OpenCV is Open Source Computer Vision Library, which is an open-source library for image processing and computer vision released under BSD open-source license. With it, you can easily implement the following functions in your program: input / output image files, applying filters to an image, accessing the webcam on your computer, performing facial or object recognition, etc. Therefore, it’s a must-have library especially for those who are interested in the deep learning models such as convolutional neural network (CNN) that are specifically designed for understanding images.


For more information about OpenCV, please refer to its official website. And if you’d like to learn more about convolutional neural network, please subscribe to our blog for the future update notification.


  • How to install OpenCV?


To install OpenCV, just type the following command in the Terminal / Anaconda Prompt and execute it:


pip install opencv-python

  • Checking your OpenCV installation:


Open your Terminal / Anaconda Prompt and type the command: “python” to enter the Python environment. Then, try to import “cv2”. If no error is shown, there is no problem for your OpenCV installation.


import cv2

Also, as in the case of TensorFlow, the version of your OpenCV library can be shown by executing the following code after successfully importing cv2:


cv2.__version__


Keras


  • What is Keras, and why it's recommended?


Although TensorFlow conspicuously simplifies the process of developing a neural network, it still can be too intimidating for those who haven’t dealt with the concepts such as computational graphs or tensors (P.S., the two are the basic elements for constructing a deep neural network in TensorFlow). In response to this problem, libraries such as Keras, which is open-source under MIT license, were created.


By using Keras, developers no longer have to conceive a computational graph by themselves. Instead, they can create a highly readable program by using the syntax far more human-friendly than that in TensorFlow, and just let Keras taking care of all the rest. With that being said, despite that it allows ones to build, train and test a deep neural network with only a few lines of code, users enjoy less freedom in manipulating their own model. Thus, if you are aiming to be an expert in artificial intelligence, moving from using Keras to TensorFlow should still be an imperative task on your to-do list.


  • How to install Keras?


To install Keras, open your Terminal / Anaconda Prompt and execute the following command:


conda install keras

  • Checking your Keras installation:


Again, let’s open the Terminal or Anaconda Prompt on your computer and enter the magic word “python”. Then, try importing Keras with the following code:


import keras

Note that since Keras is running on top of TensorFlow (i.e., Keras simply proffers an easier language to code for the programmers, while TensorFlow is still the system in the backstage that actually carries out all computations required), you will see the message “ Using TensorFlow backend” appeared on the screen after successfully importing Keras. If you see that message and nothing else, your installation has been set up properly.



To check the version of your Keras, use the command you should be very familiar already (don't forget to import Keras first):


keras.__version__


Can't Wait to Try Out?


By now, you should have finished installing everything you need to build your own neural networks. And for those people who can’t wait to test it out, we’ve prepared a congratulation gift for you: a source code file which you can run on your AI-ready computer (please download yours from here)!


In this file, you can find Python codes for building, training and testing a basic type of neural network called multilayer perceptron (MLP) based on Keras. You will also see the codes that help you download the MNIST dataset (P.S., a dataset consisting of 60,000 training images and 10,000 testing images of handwritten digits) that we’ll use to discipline and examine our neural network.


To run the code, open your Terminal or Anaconda Prompt first and type in "python" to enter the coding environment. Then, we suggest you to paste and execute the program line by line. Through this way, you’ll have a better sense about which function each line in the file serves (note that the lines starting with the symbol "#" are just annotations, whose sole purpose is to explain Python codes to the reader; nothing will happen if you execute them).


That’s it, please enjoy!


-----


Footnote: We’ll be posting more about artificial intelligence, machine learning, deep learning and artificial neural network in the future. If you’re interested in these topics, please join our mailing list by entering your e-mail at the bottom of this page for receiving newest updates!


1,183 views