Character Recognition

Overview


This example demonstrates using tensor-flow to create a 2 layer neural network

Data


The data for a character recognition problem is a set of digital characters. Each character is represented as a set of pixels. In the present example, we represent each pixel in either in an on or off state. So for example, we reperesent the following character in code as


let matrix3 = [
	[1,1,1,1],
	[0,0,0,1],
	[0,1,1,1],
	[0,0,0,1],
	[1,1,1,1]
];
					
see data

Scripts


First we import the tensor flow library on the tensor flow utility. Notice that the tensor-flow library is a traditional Javascript library, not a module, so it uses the $src function.

await $src('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest');
let ut = await import('/lib/tensor-flow/util/v1.0.0/util.mjs');
					


Next we flatten the inputs into vectors. The tensor flow models require a linear vector structure for each input.


let inputs = [];
let outputs = [];
matrices.forEach((matrix,i)=>{
    let output = $range(1,9).map(p=>0);
    output[i] = 1;
    outputs.push(output);
    let row = [];
    matrix.forEach(r=>{
        row = [...row,...r];
    });
    inputs.push(row);
})
					
Next, define a two layer sequential model. The first layer reads 20 inputs and outputs 5 outputs with a sigmoid function applied.


const model = tf.sequential({
  layers:[
    tf.layers.dense({inputShape:[20],units:5,activation:'sigmoid'}),
    tf.layers.dense({units:9,activation:'softmax'}),
  ]
});

model.compile({
  loss:tf.losses.meanSquaredError,
  optimizer:tf.train.adam(0.1)
});
					
The model is fit using 300 interations. The logCallback function is used to output the current iteration and error to the $console


const logCallback = {
	onEpochEnd: async (epoch, logs) => {			  
	  if(epoch%20 === 0) $console.log(epoch+" "+logs.loss);
	},
	onBatchEnd:async (batch, logs)=>{
  
	}
}

await model.fit(tf.tensor(inputs),tf.tensor(outputs),{
  batchSize:2,
  shuffle:true,
  epochs:300,
  callbacks:[logCallback]
});
					
The fit model is then used to predict the outputs of the given inputs. We use the utility to convert the output to a Javascript array.


let output = model.predict(tf.tensor(inputs));
let matrix = await ut.toArray(output);
for(let i=0;i<matrix.length;i++){
    let row = matrix[i];
    let val = -1;
    let index = -1;
    for(let j=0;j<row.length;j++){
        if(row[j]>val) {
            index=j;
            val = row[j];   
        }
    }
    $console.log((i+1).toString()+' is predicted as '+(index+1).toString());
}
					

Full Script




Contents