This article provides a comprehensive guide on installing TensorFlow.js on an Ubuntu system. It covers prerequisites, installation steps, and a simple example to help you get started with this powerful JavaScript library for machine learning in the browser and on Node.js.
9 min
Edited:06-10-2024
TensorFlow.js is a powerful library that allows you to develop machine learning models in JavaScript and use them in the browser or on Node.js. This guide will walk you through the installation process on Ubuntu and provide a simple example to demonstrate its capabilities.
Before you begin, ensure you have NodeJS and NPM installed:
1. sudo apt update
2. sudo apt install nodejs npm
For running TensorFlow.js in the browser, make sure you have an updated version of Chrome, Firefox, or any other modern browser.
You can install TensorFlow.js via npm. To do this, create a new directory for your project and navigate into it:
1. mkdir tensorflowjs-example
2. cd tensorflowjs-example
Then, initialize a new Node.js project:
1. npm init -y
Now, install TensorFlow.js:
1. npm install @tensorflow/tfjs
Create a new file called index.js:
1. touch index.js
Open the file in a text editor of your choice and add the following code to create a simple TensorFlow.js model that adds two numbers:
1. const tf = require('@tensorflow/tfjs');
2.
3. // Create two tensors
4. const a = tf.tensor1d([1, 2, 3]);
5. const b = tf.tensor1d([4, 5, 6]);
6.
7. // Add the tensors
8. const c = a.add(b);
9.
10. // Print the result
11. c.print(); // Output: [5, 7, 9]
To run your TensorFlow.js example, use the following command:
1. node index.js
You should see the output:
1. Tensor
2. [5, 7, 9]
To use TensorFlow.js in a web application, create an index.html file in the same directory:
1. touch index.html
Add the following code to the HTML file:
1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <meta charset="UTF-8">
5. <meta name="viewport" -content="width=device-width, initial-scale=1.0">
6. <title>TensorFlow.js Example</title>
7.
8. <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
9. </head>
10. <body>
11. <h1>TensorFlow.js Example</h1>
12. <script>
13.
14. // Create two tensors
15. const a = tf.tensor1d([1, 2, 3]);
16. const b = tf.tensor1d([4, 5, 6]);
17.
18. // Add the tensors
19. const c = a.add(b);
20.
21. // Print the result
22. c.print(); // Output: [5, 7, 9]
23. </script>
24. </body>
25. </html>
To view your example in the browser, you can open the index.html file directly or use a simple HTTP server. To quickly serve your files, you can use the following command (if you have npm installed):
1. npx http-server
Navigate to http://localhost:8080 in your browser to see the output.
You have successfully installed TensorFlow.js on Ubuntu and created a simple example both in a Node.js environment and a web browser. TensorFlow.js opens up a world of possibilities for developing machine learning applications directly in JavaScript. Explore the documentation to learn more about its features and capabilities!
14-10-2024
This article offers a detailed guide on installing and configuring IPTables on an Ubuntu VPS. IPTables is a powerful firewall tool that helps secure your server by controlling inbound and outbound traffic. Learn how to set up rules for traffic filtering, configure basic security policies, and apply custom rules to protect your VPS.
IPtables
security
12 min
This article offers a comprehensive guide on installing and configuring ModSecurity, a powerful web application firewall (WAF), on an Ubuntu VPS. Learn how to secure your server by filtering and monitoring HTTP requests, set up ModSecurity with Nginx or Apache, and apply rules to protect against common web attacks.
Modsecurity
security
10 min
14-10-2024
This article provides a comprehensive guide on installing and configuring PHP-FPM (FastCGI Process Manager) on an Ubuntu VPS. Learn how to optimize PHP performance for your web applications by configuring PHP-FPM with Nginx or Apache, managing pools, and fine-tuning settings for efficient processing of PHP scripts.
PHP-FPM
speed
optimise
12 min