22
|Machine Learning
So far in this book, when we’ve wanted the Wolfram Language to do something, we’ve written code to tell it exactly what to do. But the Wolfram Language is also set up to be able to learn what to do just by looking at examples, using the idea of machine learning.
We’ll talk about how to train the language yourself. But first let’s look at some built-in functions that have already been trained on huge numbers of examples.
Identify the language each phrase is in:
LanguageIdentify[{"thank you","merci","dar las gracias","感謝","благодарить"}]
Out[1]=
,,,,
The Wolfram Language can also do the considerably more difficult “artificial intelligence” task of identifying what an image is of.
Identify what an image is of:
ImageIdentify[
]
Out[2]=
There’s a general function , which has been taught various kinds of classification. One example is classifying the “sentiment” of text.
Upbeat text is classified as having positive sentiment:
Classify["Sentiment","I'm so excited to be programming"]
Out[3]=
Positive
Downbeat text is classified as having negative sentiment:
Classify["Sentiment","math can be really hard"]
Out[4]=
Negative
You can also train yourself. Here’s a simple example of classifying handwritten digits as 0 or 1. You give a collection of training examples, followed by a particular handwritten digit. Then it’ll tell you whether the digit you give is a 0 or 1.
With training examples, correctly identifies a handwritten 0:
Classify[{
0,
1,
0,
1,
1,
0,
0,
1,
1,
0,
0,
0,
1,
0,
1,
0,
1,
1,
1},
]
Out[5]=
0
To get some sense of how this works—and because it’s useful in its own right—let’s talk about the function , that finds what element in a list is nearest to what you supply.
Find what number in the list is nearest to 22:
Nearest[{10,20,30,40,50,60,70,80},22]
Out[6]=
{20}
Find the nearest three numbers:
Nearest[{10,20,30,40,50,60,70,80},22,3]
Out[7]=
{20,30,10}
Find the 3 colors in the list that are nearest to the color you give:
Nearest[{,,,,,,,,,,,,,,,,},,3]
Out[8]=
,,
It also works on words.
Find the 10 words nearest to “good” in the list of words:
Nearest[WordList[],"good",10]
Out[9]=
{good,food,goad,god,gold,goo,goody,goof,goon,goop}
There’s a notion of nearness for images too. And though it’s far from the whole story, this is effectively part of what is using.
Something that’s again related is recognizing text (optical character recognition or OCR). Let’s make a piece of text that’s blurred.
Create an image of the word “hello”, then blur it:
Blur[Rasterize[Style["hello",30]],3]
Out[10]=
Recognize text in the image:
TextRecognize[
]
Out[11]=
hello
If the text gets too blurred can’t tell what it says—and you probably can’t either.
Generate a sequence of progressively more blurred pieces of text:
Table[Blur[Rasterize[Style["hello",15]],r],{r,0,4}]
Out[12]=
,
,
,
,
As the text gets more blurred, makes a mistake, then gives up altogether:
Table[TextRecognize[Blur[Rasterize[Style["hello",15]],r]],{r,0,4}]
Out[13]=
{hello,hello,hella,,}
Something similar happens if we progressively blur the picture of a cheetah. When the picture is still fairly sharp, will correctly identify it as a cheetah. But when it gets too blurred starts thinking it’s more likely to be a lion, and eventually the best guess is that it’s a picture of a person.
Progressively blur a picture of a cheetah:
Table[Blur[
,r],{r,0,22,2}]
Out[14]=
,
,
,
,
,
,
,
,
,
,
,
When the picture gets too blurred, no longer thinks it’s a cheetah:
Table[ImageIdentify[Blur[
,r]],{r,0,22,2}]
Out[15]=
,,,,,,,,,,,
Collect “clusters” of similar colors into separate lists:
Create a graph of connections based on nearness in “color space”:
Show nearby colors successively grouped together:
Make a rasterized image of each letter in the alphabet:
Vocabulary
Q&A
How come I’m getting different results from the ones shown here?
It’s based on artificial neural networks inspired by the way brains seem to work. It’s been trained with millions of example images, from which it’s progressively learned to make distinctions. And a bit like in the game of “twenty questions”, by using enough of these distinctions it can eventually determine what an image is of.
At least 10,000—which is more than a typical human. (There are about 5000 “picturable nouns” in English.)
If the general area (like everyday images) is one it already knows well, then as few as a hundred. But in areas that are new, it can take many millions of examples to achieve good results.
Can a single graph have several disconnected parts?
There’s no easy answer. When it’s given a collection of things, it’ll learn features that distinguish them—though it’s typically primed by having seen many other things of the same general type (like images).
Tech Notes
◼
The Wolfram Language stores its latest machine learning classifiers in the cloud—but if you’re using a desktop system, they’ll automatically be downloaded, and then they’ll run locally.
More to Explore