(An Elementary Introduction to the Wolfram Language is available in print, as an ebook, and free on the web—as well as in Wolfram Programming Lab in the Wolfram Open Cloud. There’s also now a free online hands-on course based on it.)
A year ago I published a book entitled An Elementary Introduction to the Wolfram Language—as part of my effort to teach computational thinking to the next generation. I just published the second edition of the book—with (among other things) a significantly extended section on modern machine learning.
I originally expected my book’s readers would be high schoolers and up. But it’s actually also found a significant audience among middle schoolers (11- to 14-year-olds). So the question now is: can one teach the core concepts of modern machine learning even to middle schoolers? Well, the interesting thing is that—thanks to the whole technology stack we’ve now got in the Wolfram Language—the answer seems to be “yes”!
Here’s what I did in the book:
After this main text, the book has Exercises, Q&A and Tech Notes.
What was my thinking behind this machine learning section? Well, first, it has to fit into the flow of the book—using only concepts that have already been introduced, and, when possible, reinforcing them. So it can talk about images, and real-world data, and graphs, and text—but not functional programming or external data resources.
With modern machine learning, it’s easy to show “wow” examples—like our imageidentify.com website from 2015 (based on the Wolfram Language function). But my goal in the book was also to communicate a bit of the background and intuition of how machine learning works, and where it can be used.
I start off by explaining that machine learning is different from traditional “programming”, because it’s based on learning from examples, rather than on explicitly specifying computational steps. The first thing I discuss is something that doesn’t really need all the fanciness of modern neural-net machine learning: it’s recognizing what languages text fragments are from:
Kids (and other people) can sort of imagine (or discuss in a classroom) how something like this might work—looking words up in dictionaries, etc. And I think it’s useful to give a first example that doesn’t seem like “pure magic”. (In reality, uses a combination of traditional lookup, and modern machine learning techniques.)
But then I give a much more “magic” example—of :
I don’t immediately try to explain how it works, but instead go on to something different: sentiment analysis. Kids have lots of fun trying out sentiment analysis. But the real point here is that it shows the idea of making a “classifier”: there are an infinite number of possible inputs, but only (in this case) 3 possible outputs:
Having seen this, we’re ready to give a little more indication of how something like this works. And what I do is to show the function classifying handwritten digits into 0s and 1s. I’m not saying what’s going on inside, but people can get the idea that is given a bunch of examples, and then it’s using those to classify a particular input as being 0 or 1:
OK, but how does it do this? In reality one’s dealing with ideas about attractors—and inputs that lie in the basins of attraction for particular outputs. But in a first approximation, one can say that inputs that are “nearer to”, say, the 0 examples are taken to be 0s, and inputs that are nearer to the 1 examples are taken to be 1s.
People don’t usually have much difficulty with that explanation—unless they start to think too hard about what “nearest” might really mean in this context. But rather than concentrating on that, what I do in the book is just to talk about the case of numbers, where it’s really easy to see what “nearest” means:
isn’t the most exciting function to play with: one potentially puts a lot of things in, and then just one “nearest thing” comes out. Still, is nice because its functionality is pretty easy to understand (and one can have reasonable guesses about algorithms it could use).
Having seen for numbers, I show for colors. In the book, I’ve already talked about how colors are represented by red-green-blue triples of numbers, so this isn’t such a stretch—but seeing operate on colors begins to make it a little more plausible that it could operate on things like images too.
Next I show the case of words. In the book, I’ve already done quite a bit with strings and words. In the main text I don’t talk about the precise definition of “nearness” for words, but again, kids easily get the basic idea. (In a Tech Note, I do talk about , another good algorithmic operation that people can think about and try out.)
OK, so how does one get from here to something like ? The approach I used is to talk next about OCR and . This doesn’t seem as “magic” as (and lots of people know about “OCR’ing documents”), but it’s a good place to get a further idea of what is doing.
Turning a piece of text into an image, and then back into the same text again, doesn’t seem that impressive or useful. But it gets more interesting if one blurs the text out (and, yes, blurring an image is something I talked about earlier in the book):
Given the blurred image, the question is: can one still recognize the text? At this stage in the book I haven’t talked about () or (last output) yet, so I have to write the code out a bit more verbosely. But the result is:
And, yes, when the image isn’t too blurred, can recognize the text, but when the text gets too blurred, it stops being able to. I like this example, because it shows something impressive—but not “magic”—happening. And I think it’s useful to show both where machine learning-based functions succeed, and where they fail. By the way, the result here is different from the one in the book—because the text font is different, and those details matter when one’s on the edge of what can be recognized. (If one was doing this in a class, for example, one might try some different fonts and sizes, and discuss why some survive more blurring than others.)
shows how one can effectively do something like , but with just 26 letterforms (well, actually, handles many more glyphs than that). But now in the book I show again, blurring like we did with letters:
It’s fun to see what it does, but it’s also helpful. Because it gives a sense of the “attractor” around the “cheetah” concept: stay fairly close and the cheetah can still be recognized; go too far away and it can’t. (A slightly tricky issue is that we’re continually producing new, better neural nets for —so even between when the book was finished and today there’ve been some new nets—and it so happens they give different results for the not-a-cheetah cases. Presumably the new results are “better”, though it’s not clear what that means, given that we don’t have an official right-answer “blurred cheetah” category, and who’s to say whether the blurriest image is more like a whortleberry or a person.)
I won’t go through my whole discussion of machine learning from the book here. Suffice it to say that after discussing explicitly trained functions like and , I start discussing “unsupervised learning”, and things like clustering in feature space. I think our new is particularly helpful.
It’s fairly clear what it means to arrange colors:
But then one can “do the same thing” with images of letters. (In the book the code is a little longer, because I haven’t talked about yet.)
And what’s nice about this is that—as well as being useful in its own right—it also reinforces the idea of how something like might work by finding the “nearest letter” to whatever input it’s given.
My final example in the section uses photographs. does a nice job of separating images of different kinds of things—again giving an idea of how might work:
Obviously in just 10 pages in an elementary book I’m not able to give a complete exposition of modern machine learning. But I was pleased to see how many of the core concepts I was able to touch on.
Of course, the fact that this was possible at all depends critically on our whole Wolfram Language technology stack. Whether it’s the very fact that we have machine learning in the language, or the fact that we can seamlessly work with images or text or whatever, or the whole (28-year-old!) Wolfram Notebook system that lets us put all these pieces together—all these pieces are critical to making it possible to bring modern machine learning to people like middle schoolers.
And what I really like is that what one gets to do isn’t toy stuff: one can take what I’m discussing in the book, and immediately apply it in real-world situations. At some level the fact that this works is a reflection of the whole automation emphasis of the Wolfram Language: there’s very sophisticated stuff going on inside, but it’s automated at all levels, so one doesn’t need to be an expert and understand the details to be able to use it—or to get a good intuition about what can work and what can’t.
OK, so how would one go further in teaching machine learning?
One early thing might be to start talking about probabilities. has various possible choices of identifications, but what probabilities does it assign to them?
This can lead to a useful discussion about prior probabilities, and about issues like trading off specificity for certainty.
But the big thing to talk about is training. (After all, “machine learning trainer” will surely be a big future career for some of today’s middle schoolers…) And the good news is that in the Wolfram Language environment, it’s possible to make training work with only a modest amount of data.
Let’s get some examples of images of characters from Guardians of the Galaxy by searching the web (we’re using an external search API, so you unfortunately can’t do exactly this on the Open Cloud):
Now we can use these images as training material to create a classifier:
And, sure enough, it can identify Rocket:
And, yes, it thinks a real raccoon is him too:
How does it do it? Well, let’s look at :
Some of this looks good—but some looks confusing. Because it’s arranging some of the images not according to who they’re of, but just according to their background colors. And here we begin to see some of the subtlety of machine learning. The actual classifier we built works only because in the training examples for each character there were ones with different backgrounds—so it can figure out that background isn’t the only distinguishing feature.
Actually, there’s another critical thing as well: isn’t starting from scratch in classifying the images. Because it’s already been pre-trained to pick out “good features” that help distinguish real-world images. In fact, it’s actually using everything it learned from the creation of —and the tens of millions of images it saw in connection with that—to know up front what features it should pay attention to.
It’s a bit weird to see, but internally is characterizing each image as a list of numbers, each associated with a different “feature”:
One can do an extreme version of this in which one insists that each image is reduced to just two numbers—and that’s essentially how determines where to position an image:
Under the Hood
OK, but what’s going on under the hood? Well, it’s complicated. But in the Wolfram Language it’s easy to see—and getting a look at it helps in terms of getting an intuition about how neural nets really work. So, for example, here’s the low-level Wolfram Language symbolic representation of the neural net that powers :
And there’s actually even more: just click and keep drilling down:
And yes, this is hard to understand—certainly for middle schoolers, and even for professionals. But if we take this whole neural net object, and apply it to a picture of a tiger, it’ll do what does, and tell us it’s a tiger:
But here’s a neat thing, made possible by a whole stack of functionality in the Wolfram Language: we can actually go “inside” the neural net, to get a sense of what’s happening. As an example, let’s just take the first 3 “layers” of the network, apply them to the tiger, and visualize what comes out:
Basically what’s happening is that the network has made lots of copies of the original image, and then processed each of them to pick out a different aspect of the image. (What’s going on actually seems to be remarkably similar to the first few levels of visual processing in the brain.)
What if we go deeper into the network? Here’s what happens at layer 10. The images are more abstracted, and presumably pick out higher-level features:
Go to level 20, and the network is “thinking about” lots of little images:
But by level 28, it’s beginning to “come to some conclusions”, with only a few of its possible channels of activity “lighting up”:
Finally, by level 31, all that’s left is an array of numbers, with a few peaks visible:
And applying the very last layer of the network (a “softmax” layer) only a couple of peaks are left:
And the highest one is exactly the one that corresponds to the concept of “tiger”:
I’m not imagining that middle schoolers will follow all these details (and no, nobody should be learning neural net layer types like they learn parts of the water cycle). But I think it’s really useful to see “inside” , and get even a rough sense of how it works. To someone like me it still seems a little like magic that it all comes together as it does. But what’s great is that now with our latest Wolfram Language tools one can easily look inside, and start getting an intuition about what’s going on.
The Process of Training
The idea of the Wolfram Language function is to do machine learning at the highest possible level—as automatically as possible, and building on as much pre-training as possible. But if one wants to get a more complete feeling for what machine learning is like, it’s useful to see what happens if one instead tries to just train a neural net from scratch.
There is an immediate practical issue though: to get a neural net, starting from scratch, to actually do anything useful, one typically has to give it a very large amount of training data—which is hard to collect and wrangle. But the good news here is that with the recent release of the Wolfram Data Repository we have a growing collection of ready-to-use training sets immediately available for use in the Wolfram Language.
Like here’s the classic MNIST handwritten digit training set, with its 60,000 training examples:
One thing one can do with a training set like this is just feed a random sample of it into . And sure enough this gives one a classifier function that’s essentially a simple version of for handwritten digits:
And even with just 1000 training examples, it does pretty well:
And, yes, we can use to see how the different digits tend to separate in feature space:
But, OK, what if we want to actually train a neural net from scratch, with none of the fancy automation of ? Well, first we have to set up a raw neural net. And conveniently, the Wolfram Language has a bunch of classic neural nets built in. Here one’s called LeNet:
It’s much simpler than the net, but it’s still pretty complicated. But we don’t have to understand what’s inside it to start training it. Instead, in the Wolfram Language, we can just use (which, needless to say, automatically applies all the latest GPU tricks and so on):
It’s pretty neat to watch the training happening, and to see the orange line of the neural net’s error rate for fitting the examples keep going down. After about 20 seconds, decides it’s gone far enough, and generates a finally trained net—which works pretty well:
If you stop the training early, it won’t do quite so well:
In the professional world of machine learning, there’s a whole art and science of figuring out the best parameters for training. But with what we’ve got now in the Wolfram Language, nothing is stopping a middle schooler from doing their own experiments, visualizing and analyzing the results, and getting as good an intuition as anyone.
What Are Neural Nets Made Of?
OK, so if we want to really get down to the lowest level, we have to talk about what neural nets are made of. I’m not sure how much of this is middle-school stuff—but as soon as one knows about graphs of functions, one can already explain quite a bit. Because, you see, the “layers” in a neural net are actually just functions, that take numbers in, and put numbers out.
Take layer 2 of LeNet. It’s essentially just a simple function, which we can immediately plot (and, yes, it looks like a ramp):
Neural nets don’t typically just deal with individual numbers, though. They deal with arrays (or “tensors”) of numbers—represented in the Wolfram Language as nested lists. And each layer takes an array of numbers in, and puts an array of numbers out. Here’s a typical single layer:
This particular layer is set up to take 2 numbers as input, and put 4 numbers out:
It might seem to be doing something quite “random”, and actually it is. Because the actual function the layer is implementing is determined by yet another array of numbers, or “weights”—which here just sets randomly. Here’s what it set them to in this particular case:
Why is any of this useful? Well, the crucial point is that what does is to progressively tweak the weights in each layer of a neural network to try to get the overall behavior of the net to match the training examples you gave.
There are two immediate issues, though. First, the structure of the network has to be such that it’s possible to get the behavior you want by using some appropriate set of weights. And second, there has to be some way to progressively tweak weights so as to get to appropriate values.
Well, it turns out a single like the one above can’t do anything interesting. Here’s a contour plot of (the first element of) its output, as a function of its two inputs. And as the name might suggest, we always get something flat and linear out:
But here’s the big discovery that makes neural nets useful: if we chain together several layers, it’s easy to get something much more complicated. (And, yes, in the Wolfram Language outputs from one layer get knitted into inputs to the next layer in a nice, automatic way.) Here’s an example with 4 layers—two linear layers and two ramps:
And now when we plot the function, it’s more complicated:
We can actually also look at an even simpler case—of a neural net with 3 layers, and just one number as final output. (For technical reasons, it’s nice to still have 2 inputs, though we’ll always set one of those inputs to the constant value of 1.)
Here’s what this particular network does as a function of its input:
Inside the network, there’s an array of 3 numbers being generated—and it turns out that “3” causes there to be at most 3 (+1) distinct linear parts in the function. Increase the 3 to 100, and things can get more complicated:
Now, the point is that this is in a sense a “random function”, determined by the particular random weights picked by . If we run a bunch of times, we’ll get a bunch of different results:
But the big question is: can we find an instance of this “random function” that’s useful for whatever we’re trying to do? Or, more particularly, can we find a random function that reproduces particular training examples?
Let’s imagine that our training examples give the values of the function at the dots in this plot (by the way, the setup here is more like machine learning in the style of than ):
Here’s an instance of our network again:
And here’s a plot of what it initially does over the range of the training examples (and, yes, it’s obviously completely wrong):
Well, let’s just try training our network on our training data using :
After about 20 seconds of training on my computer, there’s some vague sign that we’re beginning to reproduce at least some aspects of the original training data. But it’s at best slow going—and it’s not clear what’s eventually going to happen.
It’s a frontier question in neural net research just what structure of net will work best in any particular case (yes, we’re working on this question). But here let’s just try a slightly more complicated network:
Random instances of this network don’t give very different results from our last network (though the presence of that layer makes the functions a bit smoother):
But now let’s do some training (data was defined above):
And here’s the result—which is surprisingly decent:
In fact, if we compare it to our original training data we see that the training values lie right on the function that the neural net produced:
Here’s what happened during the training process. The neural net effectively “tried out” a bunch of different possibilities, finally settling on the result here:
In what sense is the result “correct”? Well, it fits the training examples, and that’s really all we can ask. Because that’s all the input we gave. How it “interpolates” between the training examples is really its own business. We’d like it to learn to “generalize” from the data it’s given—but it can’t really deduce much about the whole distribution of the data from the few points it’s being given here, so the kind of smooth interpolation it’s doing is as good as anything.
Outside the range of the training values, the neural net does what seem to be fairly random things—but again, there’s no “right answer” so one can’t really fault it:
But the fact that with the arbitrariness and messiness of our original neural net, we were able to successfully train it at all is quite remarkable. Neural nets of pretty much the type we’re talking about here had actually been studied for more than 60 years—but until the modern “deep learning revolution” nobody knew that it was going to be practical to train them for real problems.
But now—particularly with everything we have now in the Wolfram Language—it’s easy for anyone to do this.
So Much to Explore
Modern machine learning is very new—so even many of the obvious experiments haven’t been tried yet. But with our whole Wolfram Language setup there’s a lot that even middle schoolers can do. For example (and I admit I’m curious about this as I write this post): one can ask just how much something like the tiny neural net we were studying can learn.
Here’s a plot of the lengths of the first 60 Roman numerals:
After a small amount of training, here’s what the network managed to reproduce:
And one might think that maybe this is the best it’ll ever do. But I was curious if it could eventually do better—and so I just let it train for 2 minutes on my computer. And here’s the considerably better result that came out:
I think I can see why this particular thing works the way it does. But seeing it suggests all sorts of new questions to pursue. But to me the most exciting point is the overarching one of just how wide open this territory is—and how easy it is now to explore it.
Yes, there are plenty of technical details—some fundamental, some superficial. But transcending all of these, there’s intuition to be developed. And that’s something that can perfectly well start with the middle schoolers…
Он думал, что сможет обойтись без ее участия - принимая во внимание ее склонность к самостоятельности - и сам найдет этот ключ, но уже столкнулся с проблемами, пытаясь самостоятельно запустить Следопыта.
Рисковать еще раз ему не хотелось. - Сьюзан, - в его голосе послышалась решимость, - я прошу тебя помочь мне найти ключ Хейла. - Что? - Сьюзан встала, глаза ее сверкали. Стратмор подавил желание встать с ней .