JavaScript

Tensorflow.js – tf.sinh()

In some scenarios, we need to convert numeric values present in a tensor to Hyperbolic Sine values. There is a built-in method that converts into hyperbolic sine. Hyperbolic Sine is used to find angles in many construction applications, like to find the angles with respect to room corners. While predicting the house prices through Tensorflow in Machine Learning, we must convert normal values to store values and then apply machine learning models. So, we will see how to apply tf.sinh() function on the existing values to get Hyperbolic Sine values.

Tensorflow.js is a framework in Javascript that supports the tf.sinh() function that converts all numeric values to Hyperbolic Sine values present in a tensor.

What is a tensor?

A tensor in tensorflow.js acts as an array that stores elements in single or multiple dimensions.

If we want to create a tensor with one dimension, tf.tensor1d() is used.

Syntax:

tf.tensor1d([elements])

Parameter:

It takes an array that has elements separated by a comma. To create a tensor with two dimensions (rows and columns), tf.tensor2d() is used.

Syntax:

tf.tensor2d([[elements],.........,[elements]])

tf.sinh()

tf.sinh() is used to return hyperbolic sine values from a given tensor.

So, it takes only one parameter: IE tensor that has numbers.

Syntax:

tf.sinh(tensor_input)

Parameter:

tensor_input is a tensor that has numbers.

It can be 1or 2 dimensional.

Let’s explore different examples of this method.

Example :

Let’s create a one-dimensional tensor in js that has some values and return hyperbolic sine values.

<html>

<!-- CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>

<center><h1>Tensorflow.js - tf.sinh() </h1></center>

<script>

let values = tf.tensor1d([30,45,60,90,180]);

//actual tensor

document.write("Actual Tensor: ",values);

document.write("<br>");

//apply sinh() on the above tensor

document.write("Tensor with Hyperbolic Sine Values: "+tf.sinh(values));

</script>

</body>

</html>

Output:

Hyperbolic Sine values were returned from the above one-dimensional tensor.

Example 2:

Let’s create a tensor that has 2 dimensions in js with 5 rows and 2 columns and return Hyperbolic Sine values.

<html>

<!-- CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>

<center><h1>Tensorflow.js - tf.sinh() </h1></center>

<script>

let values = tf.tensor2d([[40,60],[10,20],[45,82],[34,56],[67,43]]);

//actual tensor

document.write("Actual Tensor: ",values);

document.write("<br>");

//apply sinh() on the above tensor

document.write("Tensor with Hyperbolic Sine Values: "+tf.sinh(values));

</script>

</body>

</html>

Output:

Hyperbolic Sine values were returned from the above one-dimensional tensor.

Example 3:

In this case, we will consider the decimal values. Let’s create a tensor that has 2 dimensions in js with 5 rows and 2 columns and return Hyperbolic Sine values.

<html>

<!-- CDN Link that delivers the Tensorflow.js framework -->

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>

<body>

<center><h1>Tensorflow.js - tf.sinh() </h1></center>

<script>

let values = tf.tensor2d([[4.80,6.340],[45.10,2.0],[46.785,8.2],[31.4,5.6],[6.87,43.76]]);

//actual tensor

document.write("Actual Tensor: ",values);

document.write("<br>");

document.write("<br>");

//apply sinh() on the above tensor

document.write("Tensor with Hyperbolic Sine Values: "+tf.sinh(values));

</script>

</body>

</html>

Output:

Hyperbolic Sine values were returned from the above one-dimensional tensor.

Conclusion

In this Tensorflow.js tutorial, we saw how to return Hyperbolic Sine values from actual values using the tf.sinh() function present in one or two-dimensional tensors with three examples. Make sure that CDN Link is provided inside the script tag in every code.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain