# Introduction to Wolfram Language

Introduction to Wolfram Language

## Programming with Built-in Computational Intelligence

Programming with Built-in Computational Intelligence

The Wolfram Language allows programmers to operate at a significantly higher level than ever before, by leveraging built-in computational intelligence that relies on a vast depth of algorithms and real-world knowledge carefully integrated over three decades. Scalable for programs from tiny to huge, with immediate deployment locally and in the cloud, the Wolfram Language builds on clear principles—and an elegant unified symbolic structure—to create what is emerging as the world’s most productive programming language and the first true computational communication language for humans and AIs.

## What you get from the Wolfram language

What you get from the Wolfram language

1. By far the largest web of algorithms ever assembled

2. World’s largest collection of computable knowledge

3. Includes three decades of state-of-the-art algorithm development in Mathematica, Mathematica is a stack of software use to do all kinds of technical computing.

4. Minimize size and complexity of code

5. Code readability; understandability; predictability; learnability; flexibility and interoperability across all domains

6. Incremental programming: any fragment of code is instantly runnable

7. Code can involve any kind of object, such as an image, document, etc.

8. Start using the Wolfram Language with no programming knowledge

9. Put natural language understanding into programs you write

10. Use symbolic expressions to standardize interactions with external data and programs

11. Seamlessly do incremental/exploratory programming

12. Instantly build visualizations and analysis around your code

13. Strong theoretical foundations

14. Long-term team led by Stephen Wolfram himself

2. World’s largest collection of computable knowledge

3. Includes three decades of state-of-the-art algorithm development in Mathematica, Mathematica is a stack of software use to do all kinds of technical computing.

4. Minimize size and complexity of code

5. Code readability; understandability; predictability; learnability; flexibility and interoperability across all domains

6. Incremental programming: any fragment of code is instantly runnable

7. Code can involve any kind of object, such as an image, document, etc.

8. Start using the Wolfram Language with no programming knowledge

9. Put natural language understanding into programs you write

10. Use symbolic expressions to standardize interactions with external data and programs

11. Seamlessly do incremental/exploratory programming

12. Instantly build visualizations and analysis around your code

13. Strong theoretical foundations

14. Long-term team led by Stephen Wolfram himself

## Scope of the Wolfram language

Scope of the Wolfram language

The documentation gives us access to the thousands of functionality available in the Wolfram Language or WL is we call it.

Writing your first program! Follow the instructions below

## Analysing a function

Analysing a function

There are about 5000 built-in functions to do many kinds of computation you want to do like Sorting numbers or anything. All built-in functions have a capital letter as the first letter and arguments are separated by commas.

## Building expressions

Building expressions

When we write programs in WL we are building expressions. Lets write a simple program that detects the edges of a sphere.

First we create a sphere this is a simple as typing the function call for a sphere which returns a symbolic expression of the sphere.

Next we wrap the symbolic expression of the sphere inside another function that turns it into a visual sphere

To detect edges we simply use the EdgeDetect function

You can see that with just using built-in functions we can do something that is algorithmically complicated like doing edge detection. I challenge you to find a programming language out there where you can do this in a single line! The power of WL

## Holding things with Lists

Holding things with Lists

When you’re performing computation, most of the time we hold our input data in Lists and return our output as Lists. Lists are the workhorse of WL and the most useful operation on them is indexing.

Lets create a list of fruits

Lets create a list of fruits

In other programming languages I would have to store the fruit names as strings by putting double quotes “oranges”, but this is not necessary in WL. You can simple write oranges in a list and it stands for itself due to the symbolic nature of the language.

Lets select the 3rd item in this list, maybe we want to compute on it.

Lets get the first

We can also use index number to get the first

Lets get all the items without the first

Or all without the last

Take the first 3

## Iterating through things

Iterating through things

Although you have the basic For loop that you must have seen from other languages, and also the Do and the While, but our basic workhorse iterator is the Table

If you are tasked with computing the square of numbers from 1 to 10 this is how you would do it using Table

If you are tasked with computing the square of numbers from 1 to 10 this is how you would do it using Table

Lets do cubes

Lets get cubes from 10 to 20

Lets make the result also show the number beside the cube

We can get the cubes of only even numbers from 10 to 20

## Assignments

Assignments

Values can be assigned to variables using = or :=

Using = is called immediate assignment, meaning you want the value on the right hand side to be immediately given to the variable name. But if the value is something want recomputed anytime you call the variable then you use delayed assignment :=

After a short while we ask for the time again and we get a new value

## Patterns

Patterns

We use pattern expression to represent expressions in an abstract way without getting specific. it is used mostly to help us search and match things.

The _ (pronounced “blank”) is used to match anything. In the example above the Cases function searches searches a list of expressions for anything matching the pattern f[ _ ] and it finds two expressions that match that criteria.

Rather than use a blank like that we can name it using a name like x_

The Replace function takes a function and performs a match and replace on the expressions.

/. means “replace everywhere”

__ (“double blank”) stands for any sequence of expressions:

## Defining Functions

Defining Functions

So far we have been using built in functions that come with the Wolfram language. All we need to do is just supply the needed arguments. But we can also come up with our own functions to do the things we want to do and doing so is very easy!

First lets create a number that squares it’s input! Remember how we used Iterators to square a list of numbers, this time around we will use a function. A function is like a machine that takes input, performs operations on that input and returns some output

First lets create a number that squares it’s input! Remember how we used Iterators to square a list of numbers, this time around we will use a function. A function is like a machine that takes input, performs operations on that input and returns some output

When defining our own functions it is customary to start the names with lower case letters so that we don’t conflict with the built in names.

Multi-argument functions are also easy, just separate the arguments with a comma. You define much more complex functions by building expressions we will see that in the computational thinking section.

## Pure functions

Pure functions

There are also what we call pure functions or anonymous functions. These functions can be defined inline and do not need a name. We can redefine the functions above as pure functions and name them later. The square function will then be

The # represents the argument while the & terminates the function expression. We can supply arguments to the pure function too.

We can name it to reuse it, but if you require named functions its better off to just do a full function definition. But lets name it.

We can do multiple arguments

Supply arguments

Another way to specify the subtraction function above is

We use the function Function (lol) to define anonymous functions. We pass arguments to the function in the first argument of Function and then the second argument of Function is the body of the function. Then we can use our calling convention [ ] to call the function and supply values for the arguments. Re-read this section paying attention to capitalization of Function and function.

We use anonymous function in places where a function definition is needed for some one off operation and we don’t want to do a fully definition like in the Select function.

The anonymous function supplied as the last argument of the Select function is used of filter the list, picking out only number that a strictly greater than 7.

## Mapping and Applying functions

Mapping and Applying functions

A common operation in WL programming is mapping and applying functions over expressions.

Below we Map the function Capitalize over a list of animals. It capitalizes the first letter of the string.

Below we Map the function Capitalize over a list of animals. It capitalizes the first letter of the string.

Map applies a function over an expression! Lets look at an abstract example to see what is going on

We can see much clearly what Map is doing! it is picking out each element of the list expression and applying f over it. f here represents some function.

We can use the /@ short form notation for Map.

We can use the /@ short form notation for Map.

We can use pure functions

Apply on the other hand applies a function to multiple arguments

Expressions have “levels” -- corresponding to the number of indices needed to extract a part. Functions like Map can operate at specific levels.

Map defaults to operate at level 1

Map defaults to operate at level 1

Lets Map at level 2

@@ is equivalent to Apply, operating by default at level 0

@@@ means “apply at level 1”:

@ simple means ordinary function application

## Operator form of functions

Operator form of functions

Many built-in functions in the Wolfram language can use the operator form, where the function is applied as an operator to arguments.

Lets find the number in this list above that is nearest to 7.5

We can setup the operator form of the nearest function and apply it to arguments.

Now we apply it to arguments

Lets try another number

Remember the Select function with this pure function # > 7& ? lets build a Select operator.

## Supplying options to functions

Supplying options to functions

Many built-in functions have “options” that affect the details of thier operation or output.

With an option we can change the background color of the frame

To investigate the available options of a function simply

Some options are given as pure functions

## Easy visualization

Easy visualization

One of the biggest advantage of using the Wolfram system is in it’s powerful visualization system. You don’t need to “import” any libraries! All visualization capacities are built-in directly into the language making it very efficient.

Remember randomNumbers is a list of randomly generated numbers we got earlier.

Creating and visualizing graphs is also super easy.

## Manipulating stuff

Manipulating stuff

We can take any expression and build interactive interfaces with them without doing any kind of GUI programming.

Simply moving the slider about and we can change the value of the variables a and x and thus see changes instantly in the graph below.

## Procedures, Control flow and conditionals

Procedures, Control flow and conditionals

The programming structures you have in other languages are all available in Wolfram but in many cases you do not need them and can just use functions for many tasks. But if you need them they are there. If you have a big program full of If, Do, Return, etc. you’re probably not doing things right.)

Module is used to localize variables

With is used to set constants, the difference in effect of With is very subtle. In this short introduction to the language I will not go into it but when you read programs you will see the subtlety in the computational thinking section.

The semi-colon is used to separate different operations

While is a simple function that requires 2 arguments, the test and the body.

If is a function that requires 3 arguments, the test, what to do if test is true, what to do if test is false.

## Using Wolfram language as a calculator

Using Wolfram language as a calculator

Most of the time computations are exact but you can get numerical results

You can handle numbers of any precision in WL. lets get Pi to 50 decimal digits

Lets get 200 digits

You’re only limited by the size of the memory on your computer.

## Associations aka dictionaries

Associations aka dictionaries

If you come from a Python background you must have seen the way Key, Value pairs are stored as dictionaries. In WL we call them Associations.

We extract values by indexing with the appropriate key.

Using a pure function and #key to extract a value.

## Natural language input

Natural language input

Sometimes you don’t need to type code to do stuff. Wolfram language accepts natural language input.

## Real World entities

Real World entities

We can compute with real world entities like any other symbolic expression. We can compute with countries, cities, chemicals, species,people, movies,satellites, airports, etc. Image the power of such a system.

You can enter units and measures directly using CTRL + =

## Cloud Deployment

Cloud Deployment

Instantly deploy web apps unto the Wolfram Cloud. You can create sophisticated web apps with as little code as possible. Let us create a simple web app that returns a map around a particular geographic area!

What you have to do next is click the link above and it will open your newly created web app in a browser

As simple as ABC we just created a Web app that would have been ridiculously complicated to do on any other platform. You are not limited to maps, any computation you can conceive of can be deployed on the Cloud and with appropriate permissions, you can invite others to use your app!

## External Connections

External Connections

The Import and Export functions automatically handle hundreds of formats from files, programs, the web, etc.:

You can even compute directly on social media data like connecting to a Facebook account

## What next?

What next?

We have done a very light tour of the capabilities of the Wolfram language. Programming like any endeavour is a lifelong process of mastery. The Wolfram language is BIG with about 5000+ functions for do all kinds of things. We cannot cover all of the language in a session as brief as this but I have given you a taste of whats available and as much of the language as we will need to do AI/ML

In the next module on Computational Thinking we will see how you can apply the concepts you have learnt here to think computationally.

Stephen Wolfram’s Introductory book: An Elementary Introduction to the Wolfram Language available online for free to read goes into greater details of the Wolfram language than I have done in this Quick Introduction. The purpose of the Introduction is to prepare you to use the language features that we need for AI/ML.

For those who need a more hands-on approach to learning the language the Wolfram Programming Lab is available

The entire language is neatly documented in the Documentation Center

These are the resources you need to start developing your Software Engineering Career.

## What is computational thinking?

What is computational thinking?

If we want to solve problems in a sophisticated way, we cannot use Natural Language to express our ideas. The first step in humanity’s technical history was with Mathematical thinking which built the society we live and all enjoy. But there is a new kind of thinking on the horizon, it is called computational thinking.

Computational thinking is kind of like a superset of mathematical thinking, because the equations that are the foundation of mathematical thinking can be expressed with a powerful symbolic language like the Wolfram language. Apart from being naturally able to do mathematical thinking in a computational language, we can also do things that are not purely mathematical but more generic computational things.

Computational thinking is kind of like a superset of mathematical thinking, because the equations that are the foundation of mathematical thinking can be expressed with a powerful symbolic language like the Wolfram language. Apart from being naturally able to do mathematical thinking in a computational language, we can also do things that are not purely mathematical but more generic computational things.

Whenever you are trying to solve a computational problem, or a problem that arises in your own activities like finding the shortest tour around Europe or any other country for that matter, you are doing computational thinking.

## The need for interactivity

The need for interactivity

You don’t need to write a full blow software application to do computational thinking, you need an interactive notebook like the Wolfram Notebook.

An interactive environment requires that we can get instant output from the programmatic expressions that we make without waiting for any full compilation process that is required in traditional software engineering.

An interactive environment requires that we can get instant output from the programmatic expressions that we make without waiting for any full compilation process that is required in traditional software engineering.

We write code, run it, and instantly get results in the output cells of our Wolfram notebook. This is real computational thinking because we are trying to solve a computational problem instantly, most of the time using only the built in functionality of the language.

Without much ado, we will go into the details of computational thinking by building expressions that solve real world problems in this module. I want to give you the best. Although in Wolfram language we can perform large computations with only a single function like this:

But in this module we are going to focus on long form examples of Computational thinking to solve real world problems. You can find a lot of these short form examples of Computational thinking in Stephen Wolfram’s book or about 100,000+ of them in the documentation.

## Handwritten digit classification

Handwritten digit classification

This is the classical “hello world” example of the machine learning world. The goal of the program is to enable us determine which digit is represented in a typical handwritten digit. While this is a breeze for humans to perform, it is advanced stuff for computers.

First we need the training data

The training data is a mapping (a -> b) of handwritten digits to standard computer digit characters. Next we use the automatic classifier built into the Wolfram language to do the classification. This would be a laborious process in any other language.

Next we test the classifier function on images that were not in the train set.

As we can see from the results in the output line, there were errors with the classification because the data we used was insufficient. It is always a good thing to get a lot of data to improve the accuracy.

Let us get the complete MNIST data from the Wolfram Data Repository

Lets take a random sample of 10 training examples from the trainingData

We can see that it looks like what we had before but lets see how many training examples are in trainingData

We have 60,000 examples! that’s good enough to enable the system learn properly compared to what we had before. Now let’s use the automatic classification system to classify the data and get a classifier function.

This is the GUI that pops up while doing automatic classification but it disappears after classification is finished. Lets use our new classifier function to classify the previous example that failed.

We get 100% accuracy now. This is our first example of computational thinking. We were giving the task of classifying handwritten digits and we used the Classify built-in function to classify the training data. The next step was to use the classifier function that was output from the Classify function to classify some test data and we got 100% accuracy.

## Classify Images as Day or Night

Classify Images as Day or Night

Lets take our classification of things a step further. We are going to make a function that recognizes whether an image was taken during the day or during the night. Classification is a core task in machine learning and artificial intelligence so it makes sense to see another variation.

The first step in our workflow as usual is to get the training data which we did in the previous cell. The next step is to use our automatic classification function to get a classifier function.

Then we test the Classifier function on images that were not in the training set.

There were errors but the last 3 images were classified correctly. How would you as a machine learning expert correct the errors that were generated from the classification?

## Plan a City tour

Plan a City tour

If you were tasked with planning a tour using shortest route possible through the largest cities in France or any other country for that matter how would you go ahead with that?

These are the kinds of questions that can only be successfully answered using Computational thinking and the Wolfram language is the most sophisticated system for doing this. Lets go ahead and solve the problem.

These are the kinds of questions that can only be successfully answered using Computational thinking and the Wolfram language is the most sophisticated system for doing this. Lets go ahead and solve the problem.

First step is to get all our data. We start by getting the GeoPositions of all the largest cities in France

We then go ahead to find the shortest tour through this positions which is a bunch of numbers:

Drawing the tour on the map of France from the numerical data of the tour we obtained. this is super simple.

We first of all draw a line through the positions.

We first of all draw a line through the positions.

Using the GeoGraphics function we draw a red line connecting all the points.

## Create Art

Create Art

Imagine you’re an artist and you’re tasked with creating some popart you can do that directly with computation. Lets see what we can produce.

First we grab an image from the Wolfram data repository, any image will do though.

We then break the image down into components:

The imageComponents are just a bunch of numerical data

To create the art we simply supply the imageComponents data to the function Colorize

That is simply how we create art. Now we see how an image is simply numerical data! This understanding is going to be crucial as we get into machine learning and artificial intelligence.

## Hide Secret Messages in Images

Hide Secret Messages in Images

Now we are going to see a more complicated example of Computational thinking. I am going to do my best to break down the program into understandable bits then we are going to put it all together into a running program.

Steganography is a technique for hiding information in images. The hidden information is so subtle that for all practical purposes, it leaves the appearance of the image unchanged.

Convert the carrier image data into bytes and set the least significant bit of each to zero.

We get the ImageData in Bytes

We BitAnd it with a number to get the carrierBytes

Convert the secret message into a list of bits corresponding to the ASCII serialization of the symbolic object representing the message. This sounds a little complex but bit by bit we will break up the code and understand it.

We turn the message into ASCII text string

We get the character codes

We turn them into binary digits of length 8

Flatten turns a nested list into a flat one, this is our secretBits

Encode the length of the secret message into 48 bits and join this to the message. Then reshape the bit stream to match the shape of the image data. One bit per color channel per pixel.

First we get the length of the secretBits

We turn this into a 48 bits binary number

We then join it with the secretBits

Get the Dimensions of the carrierBytes

Multiply the Dimensions

Pad it to the right of the join of the secretBits that we did earlier

We take the last two dimensions out of the dimensions of the carrierBytes

We reverse the results

Then we Fold the Partition function over these results to get our secretBytes

Add the secret data to the carrier data and turn it back into an image. The image will look the same, but contain the secret message, and can be shared, in plain sight, with your recipient.

To decode the message, start by extracting the image data and discarding all but the least significant bit of each value.

We get ImageData first

BitAnd it with 1

Flatten it all and that’s our secretData

The first 48 bits tell us how much secret data was encoded.

We take the first 48 bits from secretData

Then we get a base 10 number from base 2 using FromDigits and that’s the messageLength

Excluding those 48 bits, we turn the bit stream into 8-bit blocks.

For each block of bits, up to the end of the message, turn into a byte, then into an ASCII character. Finally de-serialize the character stream to a Wolfram Language expression representing the message payload.

First take 208 bytes out of secretData

Transform them into integers in base 10

Change this raw numerical data into characters

Turn it into an expression and remove the blank spaces which are represented in the data by zeros.

That is how we do computational thinking! we use code to think and find solutions to problems. To make everything we have done convenient to use so we don’t have to pick things out bit by bit, we need to simply package all our computational work in functions that encapsulate the machinery inserting and extracting secret messages from images.

Extract the secret application, and its ready to run.

## What is Artificial Intelligence (AI)?

What is Artificial Intelligence (AI)?

Essentially what we do with computers is automate task that would otherwise be too laborious for humans to perform. But what defines the field of AI is any task that is way up on the cognitive hierarchy beyond common automation of everyday tasks that we would naturally ascribe to humans alone.

The field of AI is broad and we will not go into the entire field, which is very large and encompasses many paradigms. The paradigm of AI we will focus on in this session is the Neural Networks (DeepLearning).

Deep learning is one of the most popular and most useful paradigms of AI and learning it will give you marketable skills because it is the most demanded.

Deep learning is one of the most popular and most useful paradigms of AI and learning it will give you marketable skills because it is the most demanded.

## What is Machine learning (ML)?

What is Machine learning (ML)?

AI is a subfield of the general paradigm of machine learning. Learning is a process by which a program gradually attains better performance by training on some piece of data. Incremental improvement and data are the key words here. Machine learning algorithms train on some data and perform and we perform some kinds of inference on the results of training to answer specific questions.

## Why are (Artificial) Neural Networks Interesting?

Why are (Artificial) Neural Networks Interesting?

In Machine Learning, neural nets provide the state of the art results for the following:

#### Image Classification

Image Classification

#### Object Detection

Object Detection

#### Image Segmentation

Image Segmentation

This finds great application in the building of Self Driving Cars

#### Image Captioning

Image Captioning

This finds great application in the building of web search engines.

#### Image Generation

Image Generation

#### Other areas of application include

Other areas of application include

Text generation (language modeling)

◼

Speech-to text-systems

◼

Question answering

◼

Language Translation

◼

With Wolfram language we are mostly interested in automation, we don’t want to do routine boring stuff that the computer can already do for us. Traditionally machine learning can be difficult to learn and some areas of it require a PhD in Computer Science/Maths or related fields. We are here in this workshop because we want to learn how to use the automated machinery within the Wolfram language to do PhD grade stuff without having one.

The Wolfram machine learning architecture is broken into task oriented functions

◼

Core tasks in machine learning are automated, they include:

◼

Data type inference: What type of data are we supplying to our model

◼

Preprocessing: The Wolfram system automatically does most of the preprocessing that is required to prepare data for training.

◼

Feature extraction: The process of extracting features is fully automated so you don’t have to determine what features are necessary by yourself.

◼

Model & hyperparameter selection: The training model and all the associated hyperparameters are automatically selected.

◼

High-level user specifications: You can simply specify high level goals for your training and have the entire stack of technology contained in the Wolfram machine learning system automate away everything so you focus on your real goals. e.g. are below:

◼

PeformanceGoal "Memory"

◼

## A machine learning classification example

A machine learning classification example

In the computational thinking section we saw several classification examples like handwritten digit classification and day and night classification. Now we have to obtain our own training data and do things from scratch. Don’t be scared with Wolfram language the most complicated tasks are a piece of cake.

Usual Machine Learning approach:

◼

Training set: compute & minimize the loss function during training

◼

This is what we really train on

◼

Validation set: only compute the loss function during training

◼

Used to optimize hyperparameters & monitor overfitting

◼

Test set: only compute the loss function after training

◼

Used to estimate the performance of the final model out in the wild

◼

In the goulash example below we only need to use a train and test set since we use automated machine learning we don’t need to bother much about adjusting hyperparameters.

We first of all create a function for obtaining images from the web.

The three classes we want to classify. A goulash is a European meal.

We use our web scraping function to get the images of the meals from the web

Next we apply Join to the images and randomize it using RandomSample to get our data. We also take the first 100 images.

A short sample of our training data

Lets also get a short sample of our test data. Our test data is not included in our training data, but we will use it to test the ability of our system to classify correctly.

As usual we use our automated machine learning function Classify to do everything. What actually goes on under the hood is ridiculously complicated and that is the part that requires a PhD to master. But with the Wolfram system we don’t need to be bogged down by all those details now. We want to focus on our goal of finding out what type of goulash we have in our plates!

We get a single test image

We perform the crucial step of inference to determine what type of goulash it is.

100% accurate. While this might look like a trivial task for a human brain, it is extremely hard for computers. If you try to set up a system that Works this well even with a tiny data set on other manual machine learning systems it will be ridiculously hard to find the right algorithm and model to use. You will have to fine tune to many hyperparameters and train on large datasets to be able to get accuracy like this. So much work has gone into the Wolfram system to enable you achieve this with such simple code interfaces.

Let’s check out some probabilities and see what other goulashes it thought this was.

Let’s do something fun! Lets create an api (application programer interface) and classify goulash meals from the web! Don’t be scared! on other systems this will be hard but Wolfram as always makes it easy.

We deploy our form on the cloud and make it available for everyone.

Simply open this url in your browser to find out what kind of goulash a particular meal is. Let’s get another test image

Inputing the test image above into the webform and clicking submit.

Results page. It classified it correctly.

## Predicting with machine learning

Predicting with machine learning

Apart from classification another thing we want to do with machine learning is prediction. With the automated machine learning system within the Wolfram language we can quickly get a PredictorFunction that quickly provides a prediction if we ask for certain values. Lets construct a Dataset that we will use for our prediction.

Now we want to be able to predict the age of someone if we give the predictor function other values. Next step we are going to train on the data and get a predictor function.

Now let us query the predictor function, giving it values that were not in the Dataset and see what it thinks the age of such an individual will be.

The predictor guessed that such an individual with height of 120cm and gender female will be around 29 years of age. Like in all machine learning scenarios more data will often result in better predictions. If this data was sourced in volume from the real world, then from practical experience we will find this prediction to be of great accuracy and value.

## Automatic Image Identification

Automatic Image Identification

Sometimes you don’t want to train a neural network or even use the very simply automated machine learning capabilities of the Wolfram language. Sometimes you want to go even simpler, a scenario could be where you want to build a web application that does image identification without having to set up a machine learning system to do that. We have one handy function to do such a task and it is ridiculously easy to build a webapp with it.

As a note you will need a free Wolfram account to use the functionality below. First step is to create a web form.

We deploy our form on the cloud and make it available for everyone.

Neural networks are all the craze these days. If you have every tried to learn AI you would have encountered the term deep-learning somewhere. Without much ado lets get to train some deepnets.

Previously we did automated machine learning. We simply put our training data in a function like Classify and it choose the machine learning method it preferred. There are lots of methods out there from NearestNeigbours, Markov models, RandomForest, SVMs, and of course Neural nets.

Neural nets are just one method from a bunch of methods the Wolfram system knows about. In this section we go into more details about neural networks because they are one of the most popular machine learning systems out there.

It is usually better for beginners to use the automated systems available. You should only go deeper when you know you need it.

Neural nets are just one method from a bunch of methods the Wolfram system knows about. In this section we go into more details about neural networks because they are one of the most popular machine learning systems out there.

It is usually better for beginners to use the automated systems available. You should only go deeper when you know you need it.

let’s obtain a pre-trained version of LeNet from the Wolfram Neural Net Repository

We are not going to classify the images using the pretrained net:

In the end we get the same Netchain like the pre-built network.

Let us go into greater details and see how the innards of a neural network look like

### Layers

Layers

A Neural Network is a function defined by combining elementary operations which are called layers. You can think of layers as simple functions that transform tensors (arrays of numbers)

A 0-tensor is a number: 1

A 1-tensor is a list: {1, 2, 3}

A 2-tensor is a matrix (list of lists): {{1,2,3}, {4,5,6}}

A 3-tensor (list of lists of lists): { {{1,2,3}, {4,5,6}}, {{7,8,9}, {10,11,12}} }

Lets take a peek at the lenetModel from above

A 0-tensor is a number: 1

A 1-tensor is a list: {1, 2, 3}

A 2-tensor is a matrix (list of lists): {{1,2,3}, {4,5,6}}

A 3-tensor (list of lists of lists): { {{1,2,3}, {4,5,6}}, {{7,8,9}, {10,11,12}} }

Lets take a peek at the lenetModel from above

#### NetChain: sequential operations

NetChain: sequential operations

We can combine layers in a sequence to create a NetChain, were the output of the first layer is used as the input for the next layer and so on.

The LeNet NetModel is a bigger NetChain

Take a look at the last layer of the net.

While the net above has only 11 layers, there a lot of layers in the system lets see them

#### NetGraph: arbitrarily shaped computational graphs

NetGraph: arbitrarily shaped computational graphs

A simple computational graph:

◼

Let us see some larger net graphs obtained from the Neural Net repositories.

### More Properties of Layers (Advanced)

More Properties of Layers (Advanced)

This section summarizes a few key properties of neural net layers in the Wolfram Language.

Layers perform shape inference: if the dimensions of the input are specified, then the layer can infer what the dimensions of the output will be, and vice versa.

◼

Layers are differentiable. Differentiability is a key property that allows for the efficient training of nets.

◼

Layers evaluate using lower numerical precision (single-precision floating point) than is typical in Wolfram Language functions.

◼

## Net Encoders

Net Encoders

Create an image NetEncoder that produces a 1 x 12 x 12 tensor:

## Net Decoders

Net Decoders

The output of a neural net is often a prediction. For a regression problem, this prediction is typically a point estimate, which means it is a single number representing the value the net thinks is most likely for the task. Such outputs do not typically need to be decoded.

For classification problems, however, the output of the net is typically a vector whose components represent the probability of each class. For example, a net that classifies images of foods as "hot dog", "pizza", or "salad" produces a vector with three components that sum to one, representing the probabilities of those three classes.

For these kinds of probability vector outputs, we typically care about the most likely class rather than the raw probabilities. To determine this, we have to know how classes are associated with particular vector components.

For the MNIST task, the 10 classes we will use are the digits 0 through 9. Let us create an appropriate decoder.

By default, the decoder will decode a probability vector as a most likely class (if this looks confusing, recall that the first class is actually 0).

Apply the decoder to a probability vector:

## Neural Net Repository

Neural Net Repository

Before we start our exploration of the Neural Net Repository a few Words of Wisdom from the Director of AI at Tesla will suffice.

“If you’re feeling a bit of a fatigue in thinking about the architectural decisions, you’ll be pleased to know that in 90% or more of applications you should not have to worry about these. I like to summarize this point as ‘don’t be a hero’: Instead of rolling your own architecture for a problem, you should look at whatever architecture currently works best on ImageNet, download a pre-trained model and fine-tune it on your data. You should rarely ever have to train a ConvNet from scratch or design one from scratch.”

With that said, Let’s get on with our exploration of existing NetModels.

The Way to get to this is just to issue a programming command using the NetModel function to access a pretrained network.

#### Another reason for using pretrained nets

Another reason for using pretrained nets

However, training state-of-the art neural nets often requires huge datasets and significant computational resources that are inaccessible to most users. A repository of nets gives Wolfram Language users easy access to the latest net architectures and pre-trained nets, representing thousands of hours of computation time on powerful GPUs.

This frees users from the difficult and laborious task of building good net architectures from individual layers and allows them to transfer knowledge from nets trained on different domains to their own problems.

This frees users from the difficult and laborious task of building good net architectures from individual layers and allows them to transfer knowledge from nets trained on different domains to their own problems.

#### Typical use cases of the Neural Net Repository

Typical use cases of the Neural Net Repository

Many of these examples you see below which require enormous amounts of resources to build are available in no more than 2 lines of code, the power of Wolfram Language.

1. Lets Estimate someone’s age from a picture of thier face.

2. Let’s do industrial grade speech recognition.

3. There are nets that perform language modeling. For example, an English character-level model gives the probability of the next character given a sequence of characters:

4. Colorize photos

If you have old photographs in black and white and you have been wondering how to turn them into color images, this simple program below will help you achieve that. As a project you could even build a web app that helps people colorize images or write a Wolfram language program that sequentially colorizes images in bulk!

If you have old photographs in black and white and you have been wondering how to turn them into color images, this simple program below will help you achieve that. As a project you could even build a web app that helps people colorize images or write a Wolfram language program that sequentially colorizes images in bulk!

5. Image segmentation.

Suppose you plan on building self driving cars, the number one challenge you face apart from building the required hardware is a way of making the car differentiate between the road, pedestrians other cars and other objects you might encounter on the road. You will also also want to do this as fast as possible so therefore you will want to preprocess the image in a way that makes it fast and easy for your driving algorithms.

In the program below I will give you code that does the segmentation part of the program.

Suppose you plan on building self driving cars, the number one challenge you face apart from building the required hardware is a way of making the car differentiate between the road, pedestrians other cars and other objects you might encounter on the road. You will also also want to do this as fast as possible so therefore you will want to preprocess the image in a way that makes it fast and easy for your driving algorithms.

In the program below I will give you code that does the segmentation part of the program.

6. Object detection

Let us say that you were tasked by your company to write a software for their new robot that enables it identify certain objects and pick them up when requested to. You will have solved half of your problem by remembering that you could use the Neural Net Repository. Lets solve such a problem.

Let us say that you were tasked by your company to write a software for their new robot that enables it identify certain objects and pick them up when requested to. You will have solved half of your problem by remembering that you could use the Neural Net Repository. Lets solve such a problem.

Doing this in any other language will be a hideously difficult task requiring thousands of lines of codes. But the Wolfram Language automates most things so we can just build on top all that automation.

## What next?

What next?

What you have experienced in this entire training is only the tiny piece of the possibilities of the Wolfram language. AI and Machine learning are very complicated topics and there are people who dedicate thier entire waking life to building and optimizing neural net systems out there. You can join this worldwide group and become a Artificial Intelligence/Machine learning expert by continuing your exploration from the Wolfram documentation and the Wolfram website.

The opportunities for applying Wolfram language to all kinds of tasks are endless and you have just begun your journey by completing this Workshop.

Congratulations!

The opportunities for applying Wolfram language to all kinds of tasks are endless and you have just begun your journey by completing this Workshop.

Congratulations!

Wolfram Research © 2018 All Rights Reserved