Skip to content
Related Articles

Related Articles

Tensorflow.js tf.metrics.cosineProximity() Function

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 13 Sep, 2021

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.


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:


// 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),
// Finding the result using .cosineProximity() Function
let result = tf.metrics.cosineProximity(tensor1, tensor2);
// Printing the result.



Example 2:


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





My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!