Introduction

This post introduces a way to use deep learning to detect programming languages. Take the following code as an example.

We will get an answer python if we use the program to be introduced in the post to detect the language of the above code, which is also the correct answer. In fact, through a preliminary test, the accuracy of the program is around 90%. We have reason to believe that we are able to get a better result if the training dataset is larger or further tuning is conducted.

Execution

First let’s try running the program, so we can have an intuitive perspective on what the program is about.

1. Install third-party libraries

• Anaconda(Python 3.6+)

• Gensim

• Keras

• Tensorflow

3. Train the model

We will have three important files as soon as the above step is completed.

• resources/models/model.h5
• resources/models/model.json
• resources/vocab_tokenizer

We will introduce the three files in detail later on.

4. Detection

The following python code is detected by default by detector.py

Of course you can modify detector.py to detect other code.

Project Structure

Let’s first have a rough idea of the project structure. Don’t worry, it will only take 1 ~ 2 minutes.

• resources/code/train: training data. The name of each subfolder representes a programming language. There are around 10 code files in each subfolder, i.e. 10 files per programming language for training.

• resources/code/test: the same as resources/code/train except that it’s used for testing accuracy instead of training.

• models directory & vocab_tokenizer: stored training result
• src/config.py: some constants used in the program
• src/neural_network_trainer.py: code used to train the model
• src/detector.py: code used to load the model and detect programming languages

How It Works

Construct Vocabulary

let’s first get our heads around the training process, aka the contents in neural_network_trainer.py. the first step to train the neural network is to build a vocabulary. Vocabulary is actually a list of words, which consists of some common words in the training data. When we are done with building a vocabulary and start detecting the programming language, we will try splitting the code into a list of words, and remove those which are not in the vocabulary, then we put the remaining words into the neural network for detection.

OK, you might want to ask, why removing words that are not in the vocabulary? Wouldn’t it work if we just put all the words into the neural network? Actually, this is impossible. Because each word in the vocabulary is mapped to a word vector, which is constructed during training. So words that are not in the vocabulary don’t have word vectors to map, which means the neural network is unable to process this word.

So how do we build the vocabulary? It’s fairly easy, we just need to scan all the code in resources/code/train and extract common words in it. Those common words will make up our vocabulary. Key code is as follows.

Run build_vocab to get the vocabulary.

So, as you can see, the vocabulary is just a list of words, that’s it.

Build vocab_tokenizer

The next step is to build vocab_tokenizer. So what is vocab_tokenzier? It’s a simple variable, you can imagine it as a dictionary, which maps each word in the vocabulary to a number. Why would we map those words to numbers? Because our neural network is only able to run with numbers, rather than strings.

We use Tokenizer provided by Keras to build vocab_tokenizer.

Then we save this vocab_tokenizer as a file, to be used later.

Build Word Vectors

Before diving into word vectors, we first need to know what they are.

To put it simply, word vectors are just vectors, and each word in the vocabulary is mapped to a word vector. You may still not get it. This may seem too simple, let’s take the following Java code as an example.

The word2vec variable we are building here is actually a dictionary, which is like this(word -> word_vector).

Here comes the question. Why would we build word vectors, instead of just using the number given by vocab_tokenizer? This is because word vectors have a very special and useful characteristic: The more close two words are, the smaller their word vectors are(Note that the calculation of the distance between vectors are of the field of math, which can be dealt with using multiple methods. It doesn’t matter if you don’t know how to calculate it, you only need to know the distance between vectors can be calculated). This characteristic will boost the accuracy of our neural network dramatically.

For example, public and staic are only seen together in Java, so the distance between their word vectors should be small. However, public and System is not that close, i.e. we may only see one of them at a time, so the distance between their word vectors are larger.

Now that we know why it is necessary to build word vectors, the next problem is how we build them. There are multiple ways to do it. Here we use the Word2Vec algorithm provided by gensim to achieve it. Steps are as follows.

1. Load all the training data, extract those words which are in the vocabulary.
2. Map each word into its respective number by using vocab_tokenizer.
3. Put those numbers into Word2Vec library and obtain word vectors.

The code is as follows.

Build the Neural Network

Everything is ready, now it’s the time to train the neural network! First we need to know the input and output of the neural network, take the following code as an example.

Map def, test, print and something into their respective numbers, we get the input

The output of the neural network is the probability of each language.

The code is as follows.

So we know the above code is most likely to be written by Python, because Python has the most probability(0.5)

Now that we know the input and output, let me introduce how the neural network is constructed. There are three parts in total.

1. Embedding Layer: it’s used to map each word into its respective word vector
2. Conv1D, MaxPooling1D: this part is a classic deep learning layer. To put it simply, what it does is extraction and transformation. Refer to corresponding tutorials of deep learning for details.
3. Flatten, Dense: convert the multi-dimensional array into one-dimensional, and output the prediction.

Key code is as follows.

All right, we built our neural network, not a trivial achievement! Then let’s write a function, which uses the neural network to detect test code, check out its accuracy.

As what we have got before, the test accuracy is around 94%~95%, which is good enough. Let’s save the neural network as files, so we can load it when detecting.

Load the Neural Network For Detection

This part is simple, we only need to load vocab_tokenizer and the neural network for detection. The code is as follows.

Use it like this.

Summary

All in all, here are the steps to build the neural network.

1. Build vocabulary.
2. Build vocab_tokenizer using vocabulary, which is used to convert words into numbers.
3. Load words into Word2Vec to build word vectors.
4. Load word vectors into the neural network as part of the input layer.
5. Load all the training data, extract words that are in the vocabulary, convert them into numbers using vocab_tokenizer, load them into the neural network for training.

Three steps for detection:

1. Extract words in the code and remove those that are not in the vocabulary.
2. Convert those words into number through vocab_tokenizer, and load them into the neural network.
3. Choose the language which has the most probability, which the answer we want.

Exercise

You may have already found out that, we only saved vocab_tokenizer and the neural network(which lies in the model directory), why didn’t we save word2vec and vocab?

Question

If you have any question, please leave it in the comment below, I’ll try to answer it.

Share