WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

TensorFlow · Advanced · question 57 of 100

What is TensorFlow.js, and how can you use it to deploy machine learning models in web applications?

📕 Buy this interview preparation book: 100 TensorFlow questions & answers — PDF + EPUB for $5

TensorFlow.js is an open-source JavaScript library developed by Google that allows you to train, test, and deploy machine learning models in web browsers or Node.js. With TensorFlow.js, you can run pre-trained models or create and train custom models directly in the browser, without needing to install any additional software.

One of the key benefits of using TensorFlow.js is that it provides a high-level API that abstracts away the complexities of building and training machine learning models. This makes it easy for web developers to integrate machine learning into their applications, without needing to have a deep understanding of machine learning algorithms.

Some of the key features of TensorFlow.js include:

Ability to load pre-trained models: TensorFlow.js supports loading pre-trained models that have been created using TensorFlow or other frameworks. This means you can leverage existing models to perform tasks such as image recognition or natural language processing.

Training models in the browser: TensorFlow.js provides a high-level API for building and training machine learning models directly in the browser. This is useful for scenarios where you need to train models on user data or perform online learning.

Support for hardware acceleration: TensorFlow.js supports running models on both CPU and GPU, and it also provides support for WebGL, which allows you to accelerate computation using the graphics card.

Integration with web frameworks: TensorFlow.js provides integration with popular web frameworks such as React, Vue, and Angular, making it easy to add machine learning capabilities to your web applications.

Here is an example of how to use TensorFlow.js to load a pre-trained model and perform image recognition:

    // Load the model
    const model = await tf.loadLayersModel('path/to/model.json');
    
    // Get a reference to the image element
    const img = document.getElementById('myImage');
    
    // Preprocess the image
    const tensor = tf.browser.fromPixels(img)
    .resizeNearestNeighbor([224, 224])
    .toFloat()
    .sub(meanImageNetRGB)
    .div(stdImageNetRGB)
    .expandDims();
    
    // Make a prediction
    const predictions = model.predict(tensor);
    
    // Print the top 3 predictions
    const top3 = Array.from(predictions.dataSync())
    .map((p, i) => {
        return {
            probability: p,
            className: IMAGENET_CLASSES[i]
        };
    })
    .sort((a, b) => b.probability - a.probability)
    .slice(0, 3);
    
    console.log('Top 3 predictions:', top3);

In this example, we first load a pre-trained model using the tf.loadLayersModel function. We then get a reference to an image element on the page, and preprocess it using various operations such as resizing and normalization. Finally, we use the predict method of the model to make a prediction on the input image, and display the top 3 predictions.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic TensorFlow interview — then scores it.
📞 Practice TensorFlow — free 15 min
📕 Buy this interview preparation book: 100 TensorFlow questions & answers — PDF + EPUB for $5

All 100 TensorFlow questions · All topics