# Dear Math, I Am Not Your Therapist, Solve Your Own Problems.

## Dear Math, I Am Not Your Therapist, Solve Your Own Problems.

## Or actually, I will build a neural network that will solve those problems for you.

Imagine a website that is like a private math tutor. It can solve equations, and even guide you through the solution so you can solve the next equation yourself. Now imagine that one day you want to interact with your automatic tutor not only by equations, but also using your natural language.

Well, imagine no more. This website, Simplisico , exists. And in order for you to be able to interact with it in your natural language, one thing that needs to happen along the way is that Simplisico will understand by itself what equation you are actually trying to solve. Which is where I come in. In this post I will share with how I created a POC for the challenge of reading a textual math problem and extracting the underlying equation .

### Let’s Make a Plan!

Before we go into the details, let’s think about how we would approach this challenge in general.

Step (1)- when facing a textual math problem, make sure the problem is written in a language that we can read. I mean, if the question is in Basque and I don’t speak Basque, I would have to translate the question to a language I understand before I can even try to tackle the mathematical task.

Then, one way to solve the mathematical task itself, is:

(2)“hide” the numbers in the question with parameters, ** ** understand the structure of the equation,

(4)insert the numbers in the correct places.

In the next sections, we will see how we perform steps **(1)-(2)** using **existing tools** , and how we build a **neural network** that achieves steps **(3)-(4)** .

### Preprocessing - Teaching Our Machine to Read

Let’s see how we translate the question to a language the machine understands, in two steps, using two existing tools:

First, I did something called **Tokenization** , where I replaced the numbers in the question with variable names, separated the questions into words and removed punctuation. And I did all that easily using NLTK package.

Then, I did something called **Embedding** , which means that I “translated” the words into vectors in high dimensional space, in a way that preserves their semantic and syntactic relationships. This was also a rather simple step, thanks to the Word2Vec algorithm in the Gensim package.

Let’s think of what we gained thus far. Let’s say my machine already knows how to solve questions where it is asked to count trees in a park. Now, if it also knows that there is a similar relationship between trees in a park, and steps in a solution, then when I ask it to count steps in a solution, for the first time ever, it will know to apply the same logic it already knows for counting trees.

So now, actually, we kinda taught our machine to read.

Let’s continue to the main part, where we talk about the neural network that will extract the underlying equation .

### Buzzword-Driven-Development

If you know a little bit about neural networks (if not, you can read this quick intro I wrote ) then I can tell you straight away that the neural network I constructed for this task is A Sequence to Permutation Recurrent Neural Network With Long-Short-Term Memory and Attention .

Damn that’s a mouthful.

Let’s take it in bit by bit.

First, the input is a textual math problem, hence, a **sequence** of words. And why would the output be a **permutation** ? Because we expect that every number the machine saw in the input question, will appear in the output equation. Then, if we know what the structure of the equation is, then we can reduce our problem to finding the correct permutation of the numbers in the input question.

Now, **Recurrent Neural Network** **(RNN)** refer to a general architecture in which the network contains some sort of feedback loop. In our case, the network will read each word at a time, this is the **Short-Term Memory** part, but it will also read the word in the context in which it was read. This is the **Long-Term Memory** part. In addition, let’s say in the beginning of the question I added a sentence “I want to write a blog post on Medium”. Obviously this does not add any information to the equation I want to solve, so I want the network to know not to give too much **Attention** to these kinds of sentences.

The last missing piece is that we want to reintroduce the **numbers** we kept on the side earlier. This we can done by **concatenating** the input numbers with the output of the previous step, and flow them through another layer of neurons.

All these aspects of the architecture of the network can be obtained using somewhat basic features of Keras package.

### That’s All Folks

If you followed the steps thus far, voilà - you got yourself an algorithm that can read a textual math problem and extract the underlying equation!

And better yet, you now know one way to approach challenges involving text understanding in general.

Now you can think - what linguistic riddle do YOU want to solve? how can you use what you learned here to solve this riddle? .

These are truly exciting times to explore this fascinating domain, where increasingly complex tools become increasingly accessible, and the sky in no longer the limit to what you can achieve with basic understanding of already existing mechanisms. The internet is full of Lego blocks. We just need to learn how to connect them.

*[Watch* *this* *to learn more about my adventures working on this project.]*

- 神经网络反向传播算法
- 除了智能手机，高通的 5G 技术还瞄准了哪些领域？
- 产品设计原则：技术VP的独家看法
- PingCAP CTO 黄东旭：用户选择，技术公司通往商业的规模化之路
- Bug-proofing our application with Error Boundaries and the Strict Mode
- 以智能货柜为例，谈谈CV领域“识别错误”的3类场景
- Android 开发技术周报 Issue#246
- 任正非接受采访：可一次性出售华为5G技术许可
- 代码整洁 vs 代码肮脏
- 大众汽车600亿整合开发部门，上万名技术人才招聘或是难点