Skip to content

Instantly share code, notes, and snippets.

View zaidalyafeai's full-sized avatar
:octocat:
Working from home

Zaid Alyafeai zaidalyafeai

:octocat:
Working from home
View GitHub Profile
const convlayer = tf.layers.conv2d({
inputShape: [28, 28, 1],
kernelSize: 5,
filters: 8,
strides: 1,
activation: 'relu',
kernelInitializer: 'VarianceScaling'
});
const input = tf.zeros([1,28,28,1]);
const outputSize = Math.floor((inputSize-kernelSize)/stride +1);
const LEARNING_RATE = 0.0001;
const optimizer = tf.train.adam(LEARNING_RATE);
model.compile({
optimizer: optimizer,
loss: 'categoricalCrossentropy',
metrics: ['accuracy'],
});
const batch = tf.zeros([BATCH_SIZE,28,28,1]);
const labels = tf.zeros([BATCH_SIZE, NUM_CLASSES]);
const h = await model.fit(batch, labels,
{
batchSize: BATCH_SIZE,
validationData: validationData,
epochs: BATCH_EPOCHs
});
const output = tf.oneHot(tf.tensor1d([0,1,0]), 2);
//the output will be [[1, 0],[0, 1],[1, 0]]
//h is the output of the fitting module
const loss = h.history.loss[0];
const accuracy = h.history.acc[0];
//retrieve the canvas
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
//get image data
imgData = ctx.getImageData(0, 0, 28, 28);
//convert to tensor
const tensor = tf.browser.fromPixels(imgData);
const eTensor = tensor.expandDims(0);
model.predict(eTensor);