What is @tensorflow/tfjs-core?
@tensorflow/tfjs-core is a JavaScript library for developing and training machine learning models in the browser and on Node.js. It provides a flexible and efficient way to perform numerical computations using tensors, which are the core data structure for machine learning.
What are @tensorflow/tfjs-core's main functionalities?
Tensor Operations
This feature allows you to create and manipulate tensors, which are multi-dimensional arrays. The code sample demonstrates creating two tensors and performing an element-wise addition.
const tf = require('@tensorflow/tfjs-core');
const a = tf.tensor([1, 2, 3, 4]);
const b = tf.tensor([5, 6, 7, 8]);
const c = tf.add(a, b);
c.print();
Model Training
This feature allows you to define, compile, and train machine learning models. The code sample demonstrates creating a simple linear regression model, training it with some data, and making a prediction.
const tf = require('@tensorflow/tfjs-core');
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
model.fit(xs, ys, {epochs: 10}).then(() => {
model.predict(tf.tensor2d([5], [1, 1])).print();
});
GPU Acceleration
This feature leverages WebGL to perform computations on the GPU, which can significantly speed up numerical operations. The code sample demonstrates a simple tensor addition, which will be accelerated if a GPU is available.
const tf = require('@tensorflow/tfjs-core');
const a = tf.tensor([1, 2, 3, 4]);
const b = tf.tensor([5, 6, 7, 8]);
const c = tf.add(a, b);
c.print();
Other packages similar to @tensorflow/tfjs-core
onnxjs
ONNX.js is a JavaScript library for running ONNX (Open Neural Network Exchange) models in the browser and Node.js. It provides similar functionalities for running pre-trained models but focuses on the ONNX model format, which is widely used for interoperability between different machine learning frameworks.
brain.js
Brain.js is a JavaScript library for neural networks, which can be used in Node.js and the browser. It provides a simpler API compared to @tensorflow/tfjs-core and is more focused on ease of use for common neural network tasks, but it may not offer the same level of flexibility and performance.
TensorFlow.js Core API
A part of the TensorFlow.js ecosystem, this repo hosts @tensorflow/tfjs-core
,
the TensorFlow.js Core API, which provides low-level, hardware-accelerated
linear algebra operations and an eager API for automatic differentiation.
Check out js.tensorflow.org for more
information about the library, tutorials and API docs.
To keep track of issues we use the tensorflow/tfjs Github repo.
Importing
You can install TensorFlow.js via yarn or npm. We recommend using the
@tensorflow/tfjs npm package,
which gives you both this Core API and the higher-level
Layers API:
import * as tf from '@tensorflow/tfjs';
On the other hand, if you care about the bundle size and you do not use the
Layers API, you can import only the Core API:
import * as tfc from '@tensorflow/tfjs-core';
Note: If you are only importing the Core API, you also need to import a
backend (e.g., tfjs-backend-cpu,
tfjs-backend-webgl, tfjs-backend-wasm).
For info about development, check out DEVELOPMENT.md.
For more information
Thanks BrowserStack for providing testing support.