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
import * as tf from "@tensorflow/tfjs"
let tensor1 = tf.tensor1d([1, 2, 3]);
let tensor2 = tf.tensor1d([
Math.atan(8 / 10),
Math.atan(4 / 5),
Math.acosh(2)
]);
let result = tf.metrics.cosineProximity(tensor1, tensor2);
result.print();
|
Output:
Tensor
-0.9819149971008301
Example 2:
Javascript
import * as tf from "@tensorflow/tfjs"
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
13 Sep, 2021
Like Article
Save Article