Tensorflow.js tf.sparseReshape() Function
Last Updated :
19 Aug, 2021
Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .sparseReshape() function possesses identical syntax like reshape() function on the rendered compressed tensor. Where the input indices are recalculated on the basis of the required new shape.
Note:
- In case, one element of the stated new shape is the distinct value i.e. -1, then the size of such dimension is calculated such that the count of the dense size stays constant.
- At best only one component of the stated new shape can be -1.
- Here, the count of compressed components which is indicated by the stated new shape should be the same as the count of compressed components which is originally indicated by the stated input shape.
- The reshaping fails to impact the order of the values in the stated sparse tensor.
- In case, the input tensor possesses rank R_in, as well as N, loaded values, and the new shape, possesses length R_out, then the shape of the input indices is [N, R_in], the length of input shape is R_in, the shape of the output indices is [N, R_out], and the length of the output shape is R_out.
Syntax:
tf.sparseReshape(inputIndices, inputShape, newShape)
Parameters: This method accepts the following parameter:
- inputIndices: It is the stated 2-D. N x R_in matrix along with the indices of the loaded values in a sparse tensor. It can be of type tf.Tensor2D, TypedArray, or an Array.
- inputShape: It is the stated 1-D. R_in Tensor1D along with the input sparse tensor's compressed shape. It can be of type tf.Tensor1D, TypedArray, or an Array.
- newShape: It is the stated 1-D. R_out Tensor1D along with the demanded new compressed shape. It can be of type tf.Tensor1D, TypedArray, or an Array.
Return Value: It returns tf.Tensor object.
Example 1: In the following example, we have called sparse.sparseReshape() function with all its parameter and printed the output response without its indices and shape.
JavaScript
// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
// Calling sparse.sparseReshape() function
// with all its parameter
const res = tf.sparse.sparseReshape(
[[1, 0, 1], [2, 0, 1], [1, 1, 2], [0, -1, 0], [-3, 1, 2]],
[1, 2, 9], [-1, 9]);
// Printing output
console.log(res);
Output:
{
"outputIndices": {
"kept": false,
"isDisposedInternal": false,
"shape": [
5,
2
],
"dtype": "float32",
"size": 10,
"strides": [
2
],
"dataId": {
"id": 82
},
"id": 82,
"rankType": "2",
"scopeId": 36
},
"outputShape": {
"kept": false,
"isDisposedInternal": false,
"shape": [
2
],
"dtype": "float32",
"size": 2,
"strides": [],
"dataId": {
"id": 83
},
"id": 83,
"rankType": "1",
"scopeId": 36
}
}
Example 2: In the following example, we have called sparse.sparseReshape() function with all its parameter and printed the output response with its indices and shape.
JavaScript
// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
// Calling sparse.sparseReshape() function
// with all its parameter
const res = tf.sparse.sparseReshape(
[[1.1, 0, 1.2], [2.1, 0.2, 1.3], [1.4, 2.3, 2.5], [null, -1, 0], [-3, 1, 2]],
[1.0, 3, 3], [-1, 9]);
// Printing output indices
res['outputIndices'].print();
// Printing output shape
res['outputShape'].print();
Output:
Tensor
[[1 , 2 ],
[2 , 2 ],
[2 , 3 ],
[0 , -3],
[-2, -4]]
Tensor
[1, 9]
Reference: https://js.tensorflow.org/api/latest/#sparseReshape
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read