You’ve heard about it, you are curious, but can’t seem to get started. Do you have a background in .Net or Java? Getting started with AI is very much within your grasp. Most every major company has a strategy for AI, does yours? Whether or not you are working with a team which is driving AI research, this should not prevent you from moving forward. What are the reasons you are interested in the technology? Once this is answered, it’ll make the next steps easier.
What is AI? Wikipedia describes it as “the study of ‘intelligent agents’: any device that perceives its environment and takes actions that maximize its chance of success at some goal.” (Wikipedia). It goes on to say the term is used when “a machine mimics ‘cognitive’ functions that humans associate with other human minds, such as ‘learning’ and ‘problem solving’.” (Wikipedia) Think of an artificial agent as one to many infant minds that needs to be trained and will have their own unique personalities, one that you have some influence over.
Though the term is broadly used, often times in the context of a rules engine or a type of Workflow Foundation logic, this article will focus on modern neural networks with supervised and unsupervised “deep learning”. These types of AIs are typically very resource intensive and will challenge any hardware you may be able to supply, including any cloud systems which can be given. For this article, the minimum requirements will be that of a modern laptop running 64 bit Windows 7 and meets the requirements of Visual Studio 2019 (this would include an m5/i3 processor, 8GB ram and above).
What is the best language for AI? The answer is still a resounding Python. Though there are numerous libraries available in C#, most of the advanced highly scalable libraries which are readily available for use have been written in Python. There are also many active communities which regularly supply new scripts on a daily basis and new optimization which are only realized in that space. Further, Microsoft is a supporter of the language and has been for several years now. This includes direct support within Visual Studio, articles on MSDN and integrations within Azure. Also Python is one of the few languages which continues to get major enhancements with each new edition.
How are neural networks and data science related?
The two are not the same, though neural networks and Python can be used for data science, data science will not get a high score in a game or automate processes. Though often related, the language R is used for data science and statistics.
Libraries such as TensorFlow and SciPy make AI a breeze.
Introducing the players:
Visual Studio – The preferred IDE for most Microsoft developers. Python support has been available since 2010. It offers a standard debugging ID, framework management and an immediate window.
Tensorflow – an industry grade open source library from Google which allows developers to create scalable machine learning software. Used widely throughout Google’s portfolio, it’s easily one of the best tools available for complex projects.
OpenAI Gym – a tool which allows developers to train and test their deep learning algorithms. There are several applications and games available for it, and the list continues to grow. Each test application has a set of standardized inputs allowing developers to quickly leverage them. It is currently fully supported on Linux and is available exclusively for Python although other languages are on the way.
Python – the most popular and widely supported language for the development of machine learning and data science algorithms.
How to get started
For this tutorial, you will need to have Visual studio with the Python Workflow (PTVS) and Python for windows.
Install Visual Studio 2019.
The Community Edition is easy to get ahold of and should allow you to quickly get started however all versions work with Python. Browse to the visual studio site and download the version which pertains to your license. There is no need to uninstall older versions since 2019 can be installed safely over them.
If you already have Visual Studio 2019 set up, go to Apps & Features (Windows 10) to modify the Visual Studio installation. If prompted to update the installer, be sure to as many of the capabilities required were not part of the initial release of Visual Studio 2019
Select the Python Development checkbox
If not already selected, be sure to select Python miniconda as it will deliver many of the core libraries needed for machine learning.
Optionally if you have Visual Studio 2019 installed, you can update it through the Extensions and Updates view:
To read more about this process, visit: https://docs.microsoft.com/en-us/visualstudio/install/update-visual-studio
Unfortunately this does not install everything needed to proceed with Python for AI. You will need to install items inside of Visual Studio once it launches.
Now you are ready to launch Visual Studio.
Hello World Setup:
Once you’ve launched Visual Studio, select Create a new Project
Select “Python Application” from the project template and click Next
Here you can give it a name. For this you can title it “HelloPython”.
For the purposes of the exercise you will need to get up and running with TensorFlow.
Make sure Python 3.7 is selected at the top of the screen in the dropdown menu.
Note, if 3.7 is not available from the dropdown menu at the top, it can be added at any time by clicking on the menu and selecting > “Add Environment”. Here you can select Python 3.7 64-bit.
If you are reading this when a newer version of Python is available but is not compatible with Tensorflow, you can always go to the Python Download site and pick up the version you need. It will show up in Visual Studio for selection later. From the MSDN Documentation:
“If you’ve installed a distribution outside of the Visual Studio installer, there’s no need to check the equivalent option here. Visual Studio automatically detects existing Python installations. See The Python Environments window. Also, if a newer version of Python is available than what’s shown in the installer, you can install that version separately and Visual Studio will detect it.”
At the top of the Visual Studio screen click on the green package icon next to the selected python environment.
When the “Python Environments” tab appears on the right hand side of the screen, select “Python 3.7 (64-bit)”.
Just below you’ll see a search box and 2 or more green packages. The green package icons indicate already installed libraries. The up arrow button indicates an update is available for the package, and the X will delete the package.
Before starting, it is advisable to update pip and setuptools by pressing the up-arrow button or you will see a notice with every installation moving forward.
Tensorflow comes in two different builds – CPU Only and GPU. When using the GPU version, the speed improvement can be immense. A modern NVidia GPU will help yield a performance boost up to several times over a comparable run on a CPU.
Due to the complexity of configuring the GPU setup, this exercise will focus on CPU only.
Type tensorflow in the search box and click “’pip install tensorflow’ from PyPi” (or press enter after typing it in).
A successful install should produce the following text in the Output window:
“—– Successfully installed ‘tensorflow’ —–“
If you are getting this error:
“Could not find a version that satisfies the requirement tensorflow (from versions: )
No matching distribution found for tensorflow”
You have not selected the appropriate version of Python.
Running the code:
Now switch to the Solution Explorer tab. You should see the HelloPython.py file. Above it, expand the Python 3.7 (64-bit) Python Environment. It should now have considerably more than it did before. In particular, it should have Tensorflow.
In the code window paste in the following code:
import tensorflow as Tf
S = Tf.Session()
H = Tf.constant(‘Hello World’)
Go ahead and click the green “play” button.
A console window should appear with “b’Hello World'”
This means your TensorFlow environment is running!
Since we, as good Visual Studio coders, always should remember code quality (as well as code security..), I will mention the easiest way to assure code quality is to install PyLint.
Install PyLint by right clicking on the project, selecting “Python” then “Run PyLint”. This will allow you quickly install PyLint or run it if it’s already set up.
If this doesn’t work, go back to the Python Environments tab, type pylint into the search box and install it.
For this sample, we are getting a score of somewhere between 2.5 and 5.0 out of 10. Not bad for the first time!
Now it’s time to begin learning how to build a fully capable Machine Learning project.
Open a new instance of Visual Studio and select “Clone or check out code”
Enter the repository location: https://github.com/tflearn/tflearn
For the purposes of the tutorial this will help you get up and running with SciPy, TensorFlow and other libraries to analyze an Atari 2600 version of PacMan.
Browse to the “Examples>reinforcement_learning>atari_1step_qlearning.py” file and open it.
As in the previous example, make sure Python 3.7 is selected at the top of the screen in the dropdown menu.
Install More Libraries
At the top of the Visual Studio screen click on the green package icon next to the selected python environment.
Now it’s time to install OpenAI Gym. OpenAI Gym is a tool which allows the testing and training of artificial intelligent models and agents.
The support for it has been shaky in the past and it may not work the first time. Before trying the workarounds, simply try installing gym directly by typing “gym” in the search box and install it.
Note: The below binaries may be dated. You may have to download more recent versions but use the below as a base. Also, AMD64 is what you should target.
When completed, type this into the install package name field: [YOUR USER PROFILE PATH]\Downloads\numpy-1.12.1+mkl-cp35-cp35m-win_amd64.whl
A successful install should output:
—– Successfully installed ‘C:\Users\Owner\Downloads\numpy-1.12.1+mkl-cp35-cp35m-win_amd64.whl’ —–
This should allow you to finish installing OpenAI Gym.
Type gym in the packages field and click the “pip install gym” link below it.
Type “gym[atari]” into the search field and install it
—– Successfully installed ‘gym[atari]’ —–
Now that everything is installed, it’s time to run the first sample.
Browse to examples > reinforcement_learning > atari_1step_qlearning.py and right click and run without debugging the file.
If successful, you should see a bunch of little Atari Pacman windows pop up and run alongside a console window.
Congratulations, you are well on your way to Machine Learning through Visual Studio!
Now that you have been through 2 samples below are the definitions of some of the things you’ve touched on.
Wheel – A zip file with a whl extension containing a distribution of a framework or module that can be referenced and used by Python applications.
CUDA – is a parallel computing platform and application programming interface (API) model created by Nvidia. It allows software developers and software engineers to use a CUDA-enabled graphics processing unit (GPU) for general purpose processing – an approach termed GPGPU (General-Purpose computing on Graphics Processing Units). The CUDA platform is a software layer that gives direct access to the GPU’s virtual instruction set and parallel computational elements, for the execution of compute kernels.
MKL – Intel Math Kernel Library (Intel MKL) is a library of optimized math routines for science, engineering, and financial applications. Core math functions include BLAS, LAPACK, ScaLAPACK, sparse solvers, fast Fourier transforms, and vector math. The routines in MKL are hand-optimized specifically for Intel processors.
BLAS – Pronounced “Blahs” The BLAS (Basic Linear Algebra Subprograms) are routines that provide standard building blocks for performing basic vector and matrix operations. The Level 1 BLAS perform scalar, vector and vector-vector operations, the Level 2 BLAS perform matrix-vector operations, and the Level 3 BLAS perform matrix-matrix operations. Because the BLAS are efficient, portable, and widely available, they are commonly used in the development of high quality linear algebra software, LAPACK for example.
AI Practical Uses:
fraud and anomaly detection, character and speech recognition, data routing, search engines, suggestions and gaming.
Microsoft Cognitive Toolkit
Google Tensorflow in DataLab
Many of the technologies listed above have been around in various forms for many years but much of the major AI investments have been in IBM, Google, Amazon and Microsoft technologies.
It’s important to note that Microsoft and Google platforms both support GPU capabilities for acceleration. I recommend you consider the Udacity course “Introduction to Parallel Programming With Cuda”. This can be found on Youtube at: https://www.youtube.com/watch?v=F620ommtjqk&list=PLGvfHSgImk4aweyWlhBXNF6XISY3um82_
References and Further Reading:
Step up to Recurrent Neural Networks:
Deep Learning Demystified
Monkey brain research
Free Stock data – CSV exports