priorities = [
('+', '*'),
('-', '*'),
('+', '/'),
('-', '/'),
('*', '**'),
('/', '**'),
('**', '0'),
('c', '+'),
('c', '-'),
('0', 'c')
]
The values are (lower_priority_op, higher_priority_op). I am minimizing the data purposely to have more of the intelligence contained in the model. 'c' will be anything that is not an op. '0' just means nothing. This is a work in progress and I thought this was a good place to start.
I trained a neural net that given "higher_priority_op" will return softmax probs for the "lower_priority_op" in a one hot vector. That is easy to do. The next step is to generate a model that will take in a sequence of one hot vectors corresponding to the input expression. and return a sequence of probabilities that the corresponding element is the next to process. For example, assuming the ops are c, +, -, *, /, **, 0 for the expression "1 + 2 * 3", the inputs will be
[1,0,0,0,0,0,0] [0,1,0,0,0,0,0] [1,0,0,0,0,0,0] [1,0,0,0,0,0,0] [0,0,0,1,0,0,0] [1,0,0,0,0,0,0]
The output target will be
[0,0,0,1,0]
Which indicates that the op to process next is the multiply. For this I am not training the model. The model is only trained on pairwise op priorities. That output is used as input for the model. This is an example of how the parse model works. For each input vector, a prediction will be calculated using the standard softmax with the trained model.
[0,0,0,0,0,0,1] [1,0,0,0,0,0,1] [0,0,0,0,0,0,1] [1,1,1,0,0,0,1][0,0,0,0,0,0,1]
The next step is to sum the columns.
[2,1,1,0,0,0,5]
Next step is to subtract this from the input with a relu yielding
[0,0,0,0,0,0,0] [0,0,0,0,0,0,0] [0,0,0,0,0,0,0] [0,0,0,1,0,0,0][0,0,0,0,0,0,0]
Then sum the vector for each position
[0,0,0,0,1,0]
Then select the argmax and that is the next op to run. The numbers that I showed are perfect. The real number are more fractional.
This is a sample of the app running
Enter an arithmetic expression: 1 + 1 + 3 * 4
Input Expression: ['1', '+', '1', '+', '3', '*', '4']
Output Expression: [14.0]
The app is here https://github.com/cooledge/nn/blob/master/expressions/expressions.py
No comments:
Post a Comment