pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

Chapter 13AudioTextGeneralUtilitiesSpeechCommands(https://github.com/tensorflow/tfjs-models/tree/master/speechcommands)UniversalSentenceEncoder(https://github.com/tensorflow/tfjs-models/tree/master/universalsentenceencoder)Text ToxicityKNN Classifier(https://github.com/tensorflow/tfjs-models/tree/master/knnclassifier)Classify 1 second audio snippetsfrom the speech commandsdataset (https://github.com/tensorflow/docs/blob/master/site/en/r1/tutorials/sequences/audio_recognition.md).Encode text into a512-dimensional embeddingto be used as inputs to naturallanguage processing tasks suchas sentiment classification andtextual similarity.Score the perceived impacta comment might have on aconversation, from "Very toxic" to"Very healthy".This package provides a utilityfor creating a classifier using theK-nearest neighbors algorithm; itcan be used for transfer learning.npm i @tensorflowmodels/speechcommandsnpm i @tensorflowmodels/universalsentence-encodernpm i @tensorflowmodels/toxicitynpm i @tensorflowmodels/knnclassifierEach pretrained model can be directly used from HTML. For instance, this is anexample with the KNN Classifier:<html><head><!-- Load TensorFlow.js --><script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script><!-- Load MobileNet --><script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet"></script><!-- Load KNN Classifier --><script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/knn-[ 487 ]

TensorFlow for Mobile and IoT and TensorFlow.jsclassifier"></script></head>The next section will explain how to use pretrained models in Node.js.Node.jsIn this section we will give an overview of how to use TensorFlow with Node.js. Let'sstart:The CPU package is imported with the following line of code, which will work for allMac, Linux, and Windows platforms:import * as tf from '@tensorflow/tfjs-node'The GPU package is imported with the following line of code (as of November 2019this will work only on a GPU in a CUDA environment):import * as tf from '@tensorflow/tfjs-node-gpu'An example of Node.js code for defining and compiling a simple dense model isreported below. The code is self-explanatory:const model = tf.sequential();model.add(tf.layers.dense({ units: 1, inputShape: [400] }));model.compile({loss: 'meanSquaredError',optimizer: 'sgd',metrics: ['MAE']});Training can then start with the typical Node.js asynchronous invocation:const xs = tf.randomUniform([10000, 400]);const ys = tf.randomUniform([10000, 1]);const valXs = tf.randomUniform([1000, 400]);const valYs = tf.randomUniform([1000, 1]);async function train() {await model.fit(xs, ys, {epochs: 100,validationData: [valXs, valYs],});}train();[ 488 ]

TensorFlow for Mobile and IoT and TensorFlow.js

classifier"></script>

</head>

The next section will explain how to use pretrained models in Node.js.

Node.js

In this section we will give an overview of how to use TensorFlow with Node.js. Let's

start:

The CPU package is imported with the following line of code, which will work for all

Mac, Linux, and Windows platforms:

import * as tf from '@tensorflow/tfjs-node'

The GPU package is imported with the following line of code (as of November 2019

this will work only on a GPU in a CUDA environment):

import * as tf from '@tensorflow/tfjs-node-gpu'

An example of Node.js code for defining and compiling a simple dense model is

reported below. The code is self-explanatory:

const model = tf.sequential();

model.add(tf.layers.dense({ units: 1, inputShape: [400] }));

model.compile({

loss: 'meanSquaredError',

optimizer: 'sgd',

metrics: ['MAE']

});

Training can then start with the typical Node.js asynchronous invocation:

const xs = tf.randomUniform([10000, 400]);

const ys = tf.randomUniform([10000, 1]);

const valXs = tf.randomUniform([1000, 400]);

const valYs = tf.randomUniform([1000, 1]);

async function train() {

await model.fit(xs, ys, {

epochs: 100,

validationData: [valXs, valYs],

});

}

train();

[ 488 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!