Neural Network Calculator

Neural Network Calculator

As an entry exercise into Machine Learning I chose to make a simple addition and subtraction calculator. For the creation of this model I used Tensorflow JS and NodeJS to make a simple Convolutional Neural Network.

The code for the calculator is an edited version of the example code from the Tensorflow JS website. The code uses randomly made data to predict simple addition and subtraction calculations.

The predictions the model makes are somewhat accurate for calculations which are within the range (additions of numbers 0 - 1000) but as it tries to calculate the numbers outside of the range the neural network starts to approximate more and loses it's precision compared to the calculations which are within got range.

Here is the code:


// Add tensorflowjs and prompt functions to the library
const tf = require('@tensorflow/tfjs');

const prompt = require('prompt');

prompt.start();

require('@tensorflow/tfjs-node');

// Setup of the single model with 2 input nodes, 10 processing nodes, and 2 output nodes
const model = tf.sequential();
model.add(tf.layers.dense({units: 10, activation: 'relu', inputShape: [2]}));
model.add(tf.layers.dense({units: 2, activation: 'linear'}));
model.compile({optimizer: 'adam', loss: 'meanSquaredError'});

// Setup variables for the date to be processed
var inputs = [];
var outputs = [];

// Randomly create the data from which the neural network will learn (inputs and outputs)
for ( i = 0; i < 100; i++)
{
    inputs[i] = [];
    outputs[i] = [];
    inputs[i][0] = Math.floor(Math.random() * 1000);
    inputs[i][1] = Math.floor(Math.random() * 1000);
    outputs[i][0] = inputs[i][0] + inputs[i][1];
    outputs[i][1] = inputs[i][0] - inputs[i][1];
}
// Set the created data as variables to be input into the model
const xs = tf.tensor(inputs);
const ys = tf.tensor(outputs);

// Train the model over 2000 epochs (this number is interchangable)
model.fit(xs, ys, {
   epochs: 2000,
});

// Ask for user input and display the results of running the model on the data
// The output for the data is displayed in an array in this format [addition, subtraction]
prompt.get(['numOne', 'numTwo'], function (err, result) {
   if (err) { return onErr(err); }
   console.log('Results : ' + model.predict(tf.tensor([[parseInt(result.numOne),parseInt(result.numTwo)]])));
});


The results look something like this:

*Note this data is in the range that the Network was trained on (0-1000) any values outside of this range will be more approximated and less precise

// The last epoch in the training cycle
15ms 151us/step - loss=0.0689 
Epoch 2000 / 2000

15ms 148us/step - loss=0.0701 
// User Prompts for the two numbers they want to add/subtract
40
prompt: numTwo:  7
Results : Tensor
// The results of the processing in the format [addition, subtraction]

     [[47.7827721, 33.3206902],]

This is just a simple example and was the first project my father thought of to learn how neural networks work.

Komentáre