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
import tensorflow as tf | |
const1 = tf.constant([[1,2,3], [1,2,3]]); | |
const2 = tf.constant([[3,4,5], [3,4,5]]); | |
result = tf.add(const1, const2); | |
print(result) |
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
function calculateNewPosition(positionx, positiony, direction) | |
{ | |
return { | |
'up' : [positionx, positiony - 10], | |
'down': [positionx, positiony + 10], | |
'left' : [positionx - 10, positiony], | |
'right' : [positionx + 10, positiony], | |
'default': [positionx, positiony] | |
}[direction]; | |
} |
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
async function run() { | |
recognizer = speechCommands.create('BROWSER_FFT', 'directional4w'); | |
await recognizer.ensureModelLoaded(); | |
var canvas = document.getElementById("canvas"); | |
var contex = canvas.getContext("2d"); | |
contex.lineWidth = 10; | |
contex.lineJoin = 'round'; | |
var positionx = 400; |
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
<html> | |
<head> | |
<script src="https://unpkg.com/@tensorflow/[email protected]/dist/tf.js"></script> | |
<script src="https://unpkg.com/@tensorflow-models/[email protected]/dist/speech-commands.min.js"></script> | |
</head> | |
<body> | |
<section class='title-area'> | |
<h1>TensorFlow.js Speech Recognition</h1> | |
<p class='subtitle'>Using pretrained models for speech recognition</p> |
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
function predict(model, data, testDataSize = 500) { | |
const testData = data.nextDataBatch(testDataSize, true); | |
const testxs = testData.xs.reshape([testDataSize, 28, 28, 1]); | |
const labels = testData.labels.argMax([-1]); | |
const preds = model.predict(testxs).argMax([-1]); | |
testxs.dispose(); | |
return [preds, labels]; | |
} |
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
async function trainModelFunction(model, data, epochs) { | |
const metrics = ['loss', 'val_loss', 'acc', 'val_acc']; | |
const container = { | |
name: 'Model Training', styles: { height: '1000px' } | |
}; | |
const fitCallbacks = tfvis.show.fitCallbacks(container, metrics); | |
const batchSize = 512; | |
const [trainX, trainY] = getBatch(data, 5500); |
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
function createModelFunction() { | |
const cnn = tf.sequential(); | |
cnn.add(tf.layers.conv2d({ | |
inputShape: [28, 28, 1], | |
kernelSize: 5, | |
filters: 8, | |
strides: 1, | |
activation: 'relu', | |
kernelInitializer: 'varianceScaling' |
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
async function singleImagePlot(image) | |
{ | |
const canvas = document.createElement('canvas'); | |
canvas.width = 28; | |
canvas.height = 28; | |
canvas.style = 'margin: 4px;'; | |
await tf.browser.toPixels(image, canvas); | |
return canvas; | |
} |
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
async function getDataFunction() { | |
var data = new MnistData(); | |
await data.load(); | |
return data; | |
} |
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
async function run() { | |
const data = await getData(); | |
await displayDataFunction(data, 30); | |
const model = createModel(); | |
tfvis.show.modelSummary({name: 'Model Architecture'}, model); | |
await trainModel(model, data, 20); | |