Editor’s note: In the tech world, if you don’t understand “machine learning,” you’re out. When someone is talking about machine learning and you don’t know what to do? If you can only nod your head but can’t get a word in edgeways with your colleagues, what should you do? Let’s make a change! Adam Geitgey wrote a simple and easy to understand “machine learning, Fun” material, divided into five parts, mainly for all interested in “machine learning”, but do not know where to start friends, hope to let more people know about “machine learning”, stimulate their interest in “machine learning”. This is the second one.
Machine Learning isn’t that deep, It’s Fun (1)
In the first part of this tutorial, we talked about how machine learning allows you to use generic algorithms to tell you what’s interesting about a set of data and help you solve real problems without having to write specific code for a specific problem.
This time, we’re going to use one of the generic algorithms to do something really cool — let the algorithm generate a game level that looks like an artificial design.
First, we will build a neural network system; Second, provide the network with existing Super Mario Level data. Then you can sit back and wait for new levels to come out!
Above is a game level designed using our algorithm.
As mentioned in part 1, this tutorial is for anyone interested in machine learning who doesn’t know where to start. This guide is general and incomplete because it is intended for the general public. But I still hope to arouse the public’s interest in machine learning and let more people know and understand machine learning.
Make smarter, more accurate guesses
Going back to the first part of the guide, we created a simple algorithm for estimating house prices. The relevant data information of the house to be provided is shown in the following table:
Finally, we get a simple prediction function:
In other words, we multiplied each attribute in the information table above by a specific weight, and then simply added the multiplied numbers together to get an approximate estimated price for the house.
Instead of looking at the complicated code, we can visualize the function visually with a simple diagram:
The arrow in the figure represents the weight of the function.
However, this algorithm can only solve those simple problems where the output has a linear relationship with the input content, and is not suitable for other complex problems. So what if the reality behind housing prices isn’t as simple as we assume? For example, it is possible that the surrounding environment has a strong effect on large and small houses, but little effect on houses of medium size. So do we have all these complexities and details in our model functions?
A smart move is to test the accuracy of the algorithm by taking the data from these complex edge cases one by one.
We can start with four different approaches (i.e., four different weights) to solve this problem.
And then naturally, we’re going to get four different estimates. Combine these four estimates into one final price estimate and apply the algorithm we used earlier.
Our new “super answer” is a combination of four different price estimates. Because of this, it can simulate more complex details and cases than we can cover in a single model.
What is a neural network?
We tried to combine the previous four different price estimates into one big chart:
Look! This is a neural network. Each node can receive a range of data inputs, apply different weights, and calculate an exact output value. By linking these nodes together, we can simulate many complex functions.
Due to limited space, I have omitted some related content in this article, such as feature normalization and activation functions. But the most important part is the following basics:
· We build a simple estimation function, which can take a series of data inputs, multiply these data by the weights of each node, and finally output a relatively accurate estimate. This simple prediction function is called a neuron.
· By linking these simple neurons together, we can simulate functions that are too complex for a single neuron.
It’s like playing with legos. We can’t build different shapes with just one Lego, but if we have enough lego, we can build all kinds of shapes with them.
L gives our neural networks the ability to remember
The “neural networks” we used to know had no memory, and when you put in the same data, they returned the same answer. In programming, this is actually a stateless algorithm.
In many cases (such as home price estimates), this algorithm may be enough to solve your problem. However, this type of model function cannot deal with data where patterns exist over time.
Try to imagine that I give you a keyboard and ask you to write a story. But before you start, I’m going to guess the first letter you’re going to type on the keyboard, so which letter am I going to guess?
I can use my reserve of English knowledge to increase my chances of guessing correctly. For example, you might enter the first letter of a word that is often used at the beginning of a story. If I had read the articles you’ve written, I could have narrowed it down to the words you often use at the beginning of stories. Once I have this information, I can use it to build a neural network that simulates the probability of which letter you will start with.
Our model of the neural network might look something like this:
Now let’s make it a little more complicated. Let’s say I stop at any point in your story and I need to guess what letter you’re going to type next. So the question becomes even more interesting.
Take, for example, the opening words of Hemingway’s The Sun Still Rises:
Can you guess what the first letter after this incomplete word is?
You might guess “N,” because it’s probably going to be boxing. We made this guess based on our judgment of the incomplete word and our knowledge of the most basic English words. The use of “middleweight” also gives us a hint that this sentence may be talking about boxing.
In other words, if we think carefully about the alphabetical order and combine it with our knowledge of the rules of the English language, we can easily guess what letter we are going to type next.
So, in order to use neural networks to solve this problem, we need to add a state to our model. Every time we use this neural network to solve a problem, we automatically save the intermediate results and use them again as part of our input the next time. In this way, our model will adjust the estimates based on recently viewed input data.
By keeping track of the model’s “state,” you might be able to guess not just the first letter in the story, but the next letter anywhere in the story.
So that’s the basic content of a recursive neural network, which basically means that every time we use the network, we automatically update it, and we also update our estimates based on the information input it recently viewed. Given enough memory, it can even simulate patterns under different conditions over time.
What special meaning could one letter possibly have?
Doesn’t it seem boring and useless to guess which letter to type in a story? So what does guessing a letter really mean?
One of the most representative meaning is reflected in the mobile phone input keyboard input content automatic prediction function.
If you look at the picture above, the letter most likely to be entered next is “T”.
Now, let’s try to take this idea to the extreme. What if we asked the model to keep predicting the next possible word, keep predicting the next possible word? Then maybe this model will help us write a complete story.
Create a story piece
Now that we’ve seen how to guess the next letter of a sentence in Hemingway’s novel, let’s try to create a complete story in Hemingway’s style.
What we’re going to use here is a recursive neural network implementation by Andrej Karpathy. Andrej Karpathy is a deep learning researcher at Stanford University. He’s written an excellent introduction to writing with recursive neural networks. You can find all the code for this model on Github.
We’ll build our prediction model from the complete text of Hemingway’s The Sun Still Rises. There are 362,239 words and 84 special characters (including punctuation, uppercase and lowercase letters, etc.). These numbers are actually very small compared to what we typically use in the real world. To build a model closer to Hemingway’s style, we’d better have sample texts that are many times larger than this. But these data are sufficient as examples to illustrate the creation process of recursive neural networks.
At the beginning of the test training of recursive neural network model, it is not good at letter prediction. Here is what our authoring model outputs after 100 iterations of training:
You’ll notice that at this point it will be able to predict that sometimes words will need to be separated by Spaces, but that’s not enough to complete an essay.
After about 1000 iterations, the output of the model finally looks decent:
The model began to recognize basic structural patterns in sentences, such as putting periods at the end of sentences and even quotation marks around conversations. A few of the sentences are smooth and understandable, but there are still a large number of sentences that seem downright ridiculous nonsense.
But with more training, the model’s output looks pretty good:
So far, the algorithm we’re using has managed to master Hemingway’s pattern of short, direct conversations, so a small percentage of the sentences it generates make sense in terms of content.
Then, we compare the output of this creative model with the actual text in the book:
Word by word, our algorithm has been able to reproduce the content of the text, and the content looks very believable and the format is very appropriate. That’s pretty remarkable.
Similarly, instead of writing a text entirely from scratch, we can feed the algorithm the first few letters of the text as seed text, and then have the algorithm automatically output the next few letters based on the seed text.
Play with this automatic output system. We try to make a fake cover of The book we fantasize about, using “Er”, “He” and “The S” as The seed text, creating a new author’s name and a new title.
Photo: The real book on the left and our computer-generated nonsense book on the right.
At least it looks good!
But the most exciting part is the algorithm’s ability to pick out patterns in any order of data, which means it can easily produce either a very lifelike cheque or a fake Obama speech. But why are we limited to our own language? We can apply this idea to any kind of sequential data that has a regular pattern.
Make Mario without Mario
In 2015, Nintendo released “Super Mario Made” for the Wii U gaming system.
To be able to design their own game levels is probably the dream of all children!
The game lets you design your own Super Mario Bros. levels on the board, then upload them to the Web and let your friends play them. In your own Super Mario Levels, include classic special items and enemies from the original game. It’s like a virtual Lego for people who grew up playing Super Mario Bros.
Can we design a fake Super Mario Bros. level using that fake Hemingway-style creation model? It doesn’t hurt to have a try!
First, we need some data for model testing training. Use the outdoor levels of super Mario Bros. released in 1985 as training data.
This is the best Christmas ever! Thanks mom and Dad!
There are 32 levels in the game, and about 70% of them are outdoor, so we’re going to choose the outdoor part.
To get a design for each level, I went to the original version of the game and wrote a program to pull the level design out of the game’s memory bank. Super Mario Bros. is a 30-year-old classic, so there are plenty of resources online that can help you find out how level design is stored in the game’s memory. Pulling level design data out of a video game classic is a fun programming experience, you must try it!
Here is the first level of the game, if you have played you may remember.
Super Mario Bros. level 1
On closer inspection, we can see that the level is actually made up of a few simple mesh block objects:
We can also simply represent these objects in character order, one character for each object:
As shown above, we now have a character for each object in the level:
-
Blank cells are represented by “-“
-
Solid bricklaying is denoted by “=”
-
Fragile brickwork is denoted by a “#”
-
Gold coin bricklaying “?” said
In the same vein, each different object in the level is represented by a corresponding character.
Finally, we end up with a text file that looks like this:
Looking at the text file line by line, the Mario level really doesn’t seem to have a set pattern or pattern:
Line by line, there is no relatively fixed pattern or pattern to be found. There are even many lines that are completely blank.
It’s only when you look at the level text file text in a “series of columns” way of thinking that you see patterns and patterns:
Column by column, it does have a relatively fixed pattern. For example, each column ends in “=”.
So, in order for the algorithm to find patterns and patterns in our data, we need to enter the data as columns. Figuring out the most effective way to represent your data (also known as “feature selection”) is one of the keys to using machine learning algorithms.
To train the model, we need to rotate the text file 90 degrees to ensure that characters are entered into the model in an order that is easier to show patterns and patterns:
Model training
Just like the previous experience of “Hemingway’s prose style” creation model, a model can be perfected step by step through continuous training and testing.
Our model had only a little training in the early stages and was not yet perfected, so the output was completely indecent:
While it has a general framework for the “-” and “=” symbols, there are no rules or patterns for level design.
After thousands of trials and training, the output of this model began to take shape:
The model has found the rule that each line of characters should be the same length, and it has even found a small part of the design logic and rules for Mario game levels: In Mario games the “pipe” object is usually two bricks wide and at least two bricks high, so the letter “P” (P for “pipe”) is usually shown as a 2 by 2 cluster in the text file. This discovery is really pretty cool!
After more training, the model now outputs completely accurate character data based on the level design:
Let’s use our model to output character data for an entire level and then rotate it back to the horizontal state:
And you’re done! Here is an entire game level output from our model.
This character data looks good! Here are some equally cool things to watch:
-
Like the original “Mario” game, this level sets the “happy Cloud” (the game character, a small monster floating on the cloud) in the air at the beginning of the level.
-
It knows that pipes need to be attached to solid blocks to float in the air.
-
It follows normal logic to set up enemy positions.
-
It doesn’t have anything that blocks the player’s progress.
-
It provides the same gameplay experience as the original super Mario Bros. game, as it is based on existing levels from the original game.
Finally, take our level design and recreate the game with Super Mario Productions:
This is what our level data looks like after it was entered into Super Mario Productions.
The game has been made, play by yourself first!
If you have the Super Mario Bros. app, you can bookmark the level before you play it. You can also play the level by searching for the game code “4AC9 — 0000 — 0157-F3C3”.
Toys vs real world apps
Recursive neural network algorithms are the same ones that real-world companies use to solve complex problems like speech detection and language translation. The reason our model is more like a “toy” than a very high-end existence is that our model is trained with very limited, small amounts of data, and there weren’t enough levels in the original Super Mario Bros. game to provide enough data for our model.
If we had thousands of “Super Mario Made” game levels designed by gamers like Nintendo, we’d have a pretty good model. The reality is, we don’t have as much level data as Nintendo, because big companies don’t give it away for free.
Because machine learning is playing an increasingly important role in more and more fields, the difference between a good program and a bad one is going to be the amount of data you use for model testing and training. That’s why companies like Google and Facebook are so desperate for your personal information.
Case in point: Google recently opened source TensorFlow, a software toolkit for building large-scale machine learning applications. It’s a big move for Google to open source such an important and capable technology. Google Translate and other products use this framework.
But you can’t compete with Google Translate without Google’s vast data base in all languages. Data is where Google is at the high end. Think about it. The next time you open up your location history in Google Maps or Your Facebook location history, it keeps a record of all the places you’ve been.
Deep reading
In machine learning, there is usually no standard way to solve a problem. You have unlimited options when deciding how to preprocess your data, or which algorithm to use. In general, a combination of different approaches yields much better results than one approach alone.
Here are some links readers sent me about some other interesting ways to design Super Mario Levels:
Amy K. Hoover’s team was able to represent every object object in a game level (pipes, floors, platforms, etc.) as if it were the sound of each instrument in an entire symphony. Using a process called “functional build,” the system can automatically increase the number of levels with blocks of any given object type: you can sketch out the basic framework of a game level, for example, and then the system can improve your design by adding pipes and barrier blocks.
Steve Dahlskog’s team has shown that if you build a vertical model of game level data based on a series of N-gram words, you are more likely to output a game level with a simpler algorithm than a complex recursive neural network.
Note: This article is compiled by TupuTech. You can follow the wechat official account Tuputech to get the latest and best ai information.