This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const outputSize = Math.floor((inputSize-kernelSize)/stride +1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const LEARNING_RATE = 0.0001; | |
const optimizer = tf.train.adam(LEARNING_RATE); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model.compile({ | |
optimizer: optimizer, | |
loss: 'categoricalCrossentropy', | |
metrics: ['accuracy'], | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const output = tf.oneHot(tf.tensor1d([0,1,0]), 2); | |
//the output will be [[1, 0],[0, 1],[1, 0]] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//h is the output of the fitting module | |
const loss = h.history.loss[0]; | |
const accuracy = h.history.acc[0]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const eTensor = tensor.expandDims(0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model.predict(eTensor); |