Perceptron
Overview
A
Perceptron
is a simple function that takes a vector input {% \vec{v} %}, multiplies it by a matrix of weights and then
applies the sign (1 for positive input, 0 for negative) function to its single output.
{% value = sgn(M \vec{v}) %}
in order to be able to optimize the perceptron using tensor-flow's optimizers, we need a
differentiable function as the activation function. In this case, we use the
sigmoid function and create the percetron as
{% value = sigmoid(M \vec{v}) %}
Code
The following code creates a perceptron using the sequential model of tensor-flow.
await $src('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js');
let model = tf.sequential();
model.add(tf.layers.dense({units: 1, activation: 'sigmoid', inputShape: [2]}));
Try it!
The perceptron created here is initiallized with random weights, so even though it hasnt been trained on data yet,
it can still be applied to a set of inputs.
As specified above, this perceptron has 2 inputs, so to apply it, you need to create a tensor with two inputs.
The following code applies the perceptron to a single input.
let input = tf.tensor([[1, 2]]);
let output = model.predict(input);
Try it!
The perceptron can also be applied to a batch of inputs. The following code will run the perceptron on
each array in the tensor.
let input2 = tf.tensor([[1, 2],[2,4]]);
let output2 = model.predict(input2);
Try it!
Fitting
Fitting the perceptron as constructed, then just follows the basic procedure of
Fitting Sequential Models.
let X = [[1,1],[1,2],[3,1]];
let y= [[1],[1],[0]];
let xs = tf.tensor(X);
let ys = tf.tensor(y);
model.compile({
loss:tf.losses.meanSquaredError,
optimizer:tf.train.adam(0.1)
});
await model.fit(xs,ys,{
batchSize:2,
shuffle:true,
epochs:100
});
Try it!
MultiClass Classification
Multi Class classification can be modeled by using mutiple perceptrons run in parallel.
(see
multi class classification)
const model = tf.sequential({
layers:[
tf.layers.dense({inputShape:[20],units:9,activation:'softmax'})
]
});
Ass an example, please see
Digit Recognition Example.