JavaScript

Tensorflow.js – tf.tanh()

In some scenarios, we need to convert numeric values present in a tensor to Hyperbolic Tangent values. There is a built-in method that converts into hyperbolic tangent.

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

tf.tanh()

tf.tanh() is used to return hyperbolic tangent values from a given tensor.

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

Syntax:

tf.tanh(tensor_input)

Parameter:

tensor_inputis a tensor that has numbers.

It can be 1 or 2 dimensional.

Let’s explore different examples of this method.

Example 1:

Let’s create a one-dimensional tensor in js that has some values and return hyperbolic tangent 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.tanh() </h1></center>

<script>

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

//actual tensor

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

document.write("<br>");

document.write("<br>");

//apply tanh() on the above tensor

document.write("Tensor with Hyperbolic Tangent Values: "+tf.tanh(values));

</script>

</body>

</html>

Output:

Hyperbolic Tangent 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 Tangent 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.tanh() </h1></center>

<script>

let values = tf.tensor2d([[0,null],[10,NaN],[45,82],[34,undefined],[67,43]]);

//actual tensor

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

document.write("<br>");

document.write("<br>");

//apply tanh() on the above tensor

document.write("Tensor with Hyperbolic Tangent Values: "+tf.tanh(values));

</script>

</body>

</html>

Output:

Hyperbolic Tangent values were returned from the above one-dimensional tensor. We observed that for null.NaN and undefined, it returned 0.

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 Tangent 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.tanh() </h1></center>

<script>

let values = tf.tensor2d([[4.80,0],[45.10,null],[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 tanh() on the above tensor

document.write("Tensor with Hyperbolic Tangent Values: "+tf.tanh(values));

</script>

</body>

</html>

Output:

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

Conclusion

In this Tensorflow.js tutorial, we saw how to return Hyperbolic Tangent values from actual values using the tf.tanh() 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. We observed that for the values – null,0,NaN and undefined the tanh() function returns 0.

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