Skip to content
Related Articles

Related Articles

Improve Article

Tensorflow.js tf.metrics.cosineProximity() Function

  • Last Updated : 13 Sep, 2021
Geek Week

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js.

The tf.metrics.cosineProximity() function is defined as: -[sum(l2Normalize(tensor1)) * (l2Normalize(tensor2))], where l2Normalize() normalizes the L2 norm of the input to 1 and * represents multiplication.

Syntax:

tf.metrics.cosineProximity(yTrue, yPred)

Parameters: This function accepts the following two parameters:

  • yTrue: It is a simple Truth tensor.
  • yPred: It is a simple Prediction tensor.

Return Value: It returns the tf.Tensor object.



Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Initializing truth tensor.
let tensor1 = tf.tensor1d([1, 2, 3]);
 
// Initializing Prediction tensor.
let tensor2 = tf.tensor1d([
    Math.atan(8 / 10),
    Math.atan(4 / 5),
    Math.acosh(2)
]);
 
// Finding the result using .cosineProximity() Function
let result = tf.metrics.cosineProximity(tensor1, tensor2);
 
// Printing the result.
result.print();

Output: 

Tensor
    -0.9819149971008301

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Finding the cosime proximity between
// truth and prediction tensor
// using .cosineProximity() Function
tf.metrics.cosineProximity(
    tf.tensor1d([1, 2, 3]),
    tf.tensor1d([4, 5, 6])
)
   .print();

Output:

Tensor
    -0.9746317863464355

Reference: https://js.tensorflow.org/api/3.6.0/#metrics.cosineProximity

 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :