TensorFlow.js for browser is a library that lets you run machine learning models directly in a web browser using JavaScript, without needing a server.
Click to reveal answer
intermediate
How does TensorFlow.js run models in the browser?
It uses WebGL or WebAssembly to perform fast math operations on your computer's graphics card or CPU, enabling efficient machine learning computations inside the browser.
Click to reveal answer
beginner
What are the main benefits of using TensorFlow.js in the browser?
You get instant results without sending data to a server, better privacy since data stays on the user device, and easy integration with web apps.
Click to reveal answer
intermediate
How do you load a pre-trained TensorFlow.js model in the browser?
You use the function `tf.loadLayersModel()` with the URL or path to the model's JSON file to load it asynchronously in your JavaScript code.
Click to reveal answer
intermediate
What is the difference between `tf.tensor()` and `tf.tensor2d()` in TensorFlow.js?
`tf.tensor()` creates a tensor of any shape from data, while `tf.tensor2d()` specifically creates a 2-dimensional tensor (like a matrix) from data.
Click to reveal answer
Which technology does TensorFlow.js use to speed up math operations in the browser?
AFlash
BJava Applets
CWebGL
DSilverlight
✗ Incorrect
TensorFlow.js uses WebGL to access the graphics card for fast math operations.
What function do you use to load a pre-trained model in TensorFlow.js?
Atf.loadModel()
Btf.loadLayersModel()
Ctf.importModel()
Dtf.getModel()
✗ Incorrect
The correct function is tf.loadLayersModel() to load models saved in the Layers format.
Which of these is NOT a benefit of running ML models in the browser with TensorFlow.js?
AFaster training than on a GPU server
BNo need for internet connection after loading
CInstant feedback to user
DImproved user privacy
✗ Incorrect
Training on a GPU server is usually faster than in the browser; TensorFlow.js is better for inference and small training tasks.
What does `tf.tensor2d()` create?
AA 1-dimensional tensor
BA 3-dimensional tensor
CA scalar value
DA 2-dimensional tensor
✗ Incorrect
tf.tensor2d() creates a 2-dimensional tensor, like a matrix.
Which language do you use to write TensorFlow.js code for the browser?
AJavaScript
BPython
CJava
DC++
✗ Incorrect
TensorFlow.js is a JavaScript library designed to run in web browsers.
Explain how TensorFlow.js enables machine learning in the browser and why this is useful.
Think about how your browser can do math fast and keep data local.
You got /4 concepts.
Describe the steps to load and use a pre-trained TensorFlow.js model in a web page.
Loading is async, then you predict with tensors.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using TensorFlow.js in a web browser?
easy
A. Needs a separate app to visualize data
B. Requires installing special software on the user's computer
C. Run and train machine learning models directly in the browser without extra software
D. Only works on server-side environments
Solution
Step 1: Understand TensorFlow.js purpose
TensorFlow.js is designed to run and train ML models directly in the browser environment.
Step 2: Compare options
Options A, B, and C mention installing software, server-only use, or separate apps, which are not true for TensorFlow.js.
Final Answer:
Run and train machine learning models directly in the browser without extra software -> Option C
Quick Check:
TensorFlow.js runs ML in browser = D [OK]
Hint: TensorFlow.js runs ML models inside browsers without installs [OK]
Common Mistakes:
Thinking TensorFlow.js needs server or software install
Confusing browser use with server-side ML
Assuming extra apps are required
2. Which of the following is the correct way to create a 1D tensor with values [1, 2, 3] in TensorFlow.js?
B. The reshape size [2, 2] does not match the number of elements in tensor
C. arraySync() is not a valid method
D. reshape() requires a 1D array argument
Solution
Step 1: Check tensor size and reshape target shape
The tensor has 3 elements, but reshape tries to make shape [2, 2] which needs 4 elements.
Step 2: Understand reshape requirements
Reshape must keep total elements same. Here 3 != 4, so it causes an error.
Final Answer:
The reshape size [2, 2] does not match the number of elements in tensor -> Option B
Quick Check:
Reshape size must match elements = A [OK]
Hint: Reshape shape must multiply to original tensor size [OK]
Common Mistakes:
Ignoring total element count mismatch
Thinking arraySync() is invalid
Believing reshape needs 1D array argument
5. You want to build a simple linear regression model in TensorFlow.js that predicts y from x. Which sequence of steps is correct to train the model in the browser?
hard
A. Define model layers, compile with optimizer and loss, prepare tensors for x and y, then call model.fit() with data
B. Directly call model.predict() on raw arrays without compiling or training
C. Use tf.tensor() to create model weights manually and skip model.fit() entirely
D. Train the model only on the server and send predictions to browser
Solution
Step 1: Understand model training workflow in TensorFlow.js
First, define the model architecture with layers, then compile it specifying optimizer and loss function.
Step 2: Prepare input and output data as tensors and call model.fit()
Training requires data tensors for inputs (x) and outputs (y), then call model.fit() to train the model in the browser.
Final Answer:
Define model layers, compile with optimizer and loss, prepare tensors for x and y, then call model.fit() with data -> Option A
Quick Check:
Model training steps = B [OK]
Hint: Define, compile, prepare tensors, then fit model [OK]
Common Mistakes:
Skipping compile before training
Trying to predict without training
Manually setting weights without training
Assuming training must happen on server
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.