logo
down
shadow

what is wrong with my cosine similarity? Tensorflow


what is wrong with my cosine similarity? Tensorflow

Content Index :

what is wrong with my cosine similarity? Tensorflow
Tag : python , By : Hitesh Prajapati
Date : November 26 2020, 03:01 PM

Hope that helps There's a problem with dimensions in your example, I think w1 should have a [3, 10] shape. But ignoring these minor details, your implementation seems to be correct.
Though, I would suggest an approach closer to the top example, that is using tf.nn.l2_normalize, because it guarantees to return the same shape as input, so gives flexibility to choose the dimensions along which to normalize. In addition, tf.nn.l2_normalize provides numerical stability when the denominator is near zero, and is likely to be a bit more efficient.
a = tf.placeholder(tf.float32, shape=[None, 3], name="input_placeholder_a")
b = tf.placeholder(tf.float32, shape=[3, 10], name="input_placeholder_b")

normalize_a = tf.nn.l2_normalize(a, dim=0)
normalize_b = tf.nn.l2_normalize(b, dim=0)
cos_similarity=tf.matmul(normalize_a, normalize_b)

sess=tf.Session()
cos_sim=sess.run(cos_similarity,feed_dict={
  a: np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
  b: np.arange(30).reshape([3, 10])
})
print cos_sim

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Cosine similarity returning wrong distance


Tag : java , By : Jay Crockett
Date : March 29 2020, 07:55 AM
To fix this issue Your code is fine.
The vectors are dominated by several large features. In those features, the two vectors are almost collinear, which is why the similarity measure is close to 1.
feature     vec1    vec2        vec2/vec1

64806110    2875    1.85E+07    6.43E+03
64806108    5750    3.68E+07    6.40E+03
64806107    8625    5.49E+07    6.37E+03
64806106    11500   7.29E+07    6.34E+03
64806111    14375   9.07E+07    6.31E+03
64806109    17250   1.08E+08    6.28E+03

Choice between an adjusted cosine similarity vs regular cosine similarity


Tag : development , By : Joe Sweeney
Date : March 29 2020, 07:55 AM
Hope this helps
Why would a regular cosine similarity result in a positive number for such 'different' items?
from scipy import spatial
import numpy as np
a = np.array([2.0,1.0])  
b = np.array([5.0,3.0])
1 - spatial.distance.cosine(a,b)
#----------------------
# 0.99705448550158149
#----------------------
c = np.array([5.0,4.0])
1 - spatial.distance.cosine(c,b)
#----------------------
# 0.99099243041032326
#----------------------
mean_ab = sum(sum(a,b)) / 4  
# mean_ab : 3.5
# adjusted vectors : [-1.5, -2.5] , [1.5, -0.5]
1 - spatial.distance.cosine(a - mean_ab, b - mean_ab)
#----------------------
# -0.21693045781865616
#----------------------
mean_cb = sum(sum(c,b)) / 4
# mean_cb : 6.5
# adjusted vectors : [-1.5, -3.5] , [-1.5, -2.5]
1 - spatial.distance.cosine(c - mean_cb, b - mean_cb)
#----------------------
# 0.99083016804429891
#----------------------

Pairwise Cosine Similarity using TensorFlow


Tag : development , By : hsdfhksh
Date : March 29 2020, 07:55 AM
will be helpful for those in need There is an answer for getting a single cosine distance here: https://stackoverflow.com/a/46057597/288875 . This is based on tf.losses.cosine_distance .
Here is a solution which does this for matrices:
import tensorflow as tf
import numpy as np

with tf.Session() as sess:

    M = 3

    # input
    input = tf.placeholder(tf.float32, shape = (M, M))

    # normalize each row
    normalized = tf.nn.l2_normalize(input, dim = 1)

    # multiply row i with row j using transpose
    # element wise product
    prod = tf.matmul(normalized, normalized,
                     adjoint_b = True # transpose second matrix
                     )

    dist = 1 - prod

    input_matrix = np.array(
        [[ 1, 1, 1 ],
         [ 0, 1, 1 ],
         [ 0, 0, 1 ],
         ],
        dtype = 'float32')

    print "input_matrix:"
    print input_matrix

    from sklearn.metrics.pairwise import pairwise_distances
    print "sklearn:"
    print pairwise_distances(input_matrix, metric='cosine')

    print "tensorflow:"
    print sess.run(dist, feed_dict = { input : input_matrix })
input_matrix:
[[ 1.  1.  1.]
 [ 0.  1.  1.]
 [ 0.  0.  1.]]
sklearn:
[[ 0.          0.18350345  0.42264974]
 [ 0.18350345  0.          0.29289323]
 [ 0.42264974  0.29289323  0.        ]]
tensorflow:
[[  5.96046448e-08   1.83503449e-01   4.22649741e-01]
 [  1.83503449e-01   5.96046448e-08   2.92893231e-01]
 [  4.22649741e-01   2.92893231e-01   0.00000000e+00]]

Computing the Cosine Similarity of two sets of vectors in Tensorflow


Tag : python , By : user179271
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Hi fellow Stackoverflow users, , You can compute that simply like this:
import tensorflow as tf

# Vectors
a = tf.placeholder(tf.float32, shape=[600, 52])
b = tf.placeholder(tf.float32, shape=[16000, 52])
# Cosine similarity
similarity = tf.reduce_sum(a[:, tf.newaxis] * b, axis=-1)
# Only necessary if vectors are not normalized
similarity /= tf.norm(a[:, tf.newaxis], axis=-1) * tf.norm(b, axis=-1)
# If you prefer the distance measure
distance = 1 - similarity

Tensorflow cosine similarity between each tensor in a list


Tag : python , By : Frank
Date : March 29 2020, 07:55 AM
will help you I have 2 list(array) with tensors and want to calculate cosine similarity of the tensors between two lists. And get output list(tensor) with similarities.
a = tf.placeholder(tf.float32, shape=[None,3], name="input_placeholder_a")
b = tf.placeholder(tf.float32, shape=[None,3], name="input_placeholder_b")
numerator = tf.reduce_sum(tf.multiply(a, b), axis=1)
denominator = tf.multiply(tf.norm(a, axis=1), tf.norm(b, axis=1))
cos_similarity = numerator/denominator

sess=tf.Session()
cos_sim=sess.run(cos_similarity,feed_dict={
  a: np.array([[1, 2, 3], 
               [4, 5, 6]]),

  b: np.array([[1, 2, 3], 
               [8, 7, 9]]),
})
print(cos_sim)
Related Posts Related QUESTIONS :
  • filter dataframe on each value of a samn column have a specific value of another column in Panda\Python
  • Threading with pubsub throwing AssertionError: 'callableObj is not callable' in wxPython
  • Get grouped data from 2 dataframes with condition
  • How can I import all of sklearns regressors
  • How to take all elements except the first k
  • Whats wrong with my iteration list of lists from csv
  • Tensorflow Estimator API save image summary in eval mode
  • How to Pack with PyQt - how to make QFrame/Layout adapt to content
  • How do I get certain Time Range in Python
  • python doubly linked list - insertAfter node
  • Open .h5 file in Python
  • Joining a directory name with a binary file name
  • python, sort list with two arguments in compare function
  • Is it possible to print from Python using non-ANSI colors?
  • Pandas concat historical data using date minus some number of days
  • CV2: Import Error in Python OpenCV
  • Is it possible to do this loop in a one-liner?
  • invalid literal for int() with base 10: - django
  • Why does my code print a value that I have not assigned as yet?
  • the collatz func in automate boring stuff with python
  • How to find all possible combinations of parameters and funtions
  • about backpropagation deep neural network in tensorflow
  • Sort strings in pandas
  • How do access my flask app hosted in docker?
  • Replace the sentence include some text with Python regex
  • Counting the most common element in a 2D List in Python
  • logout a user from the system using a function in python
  • mp4 metadata not found but exists
  • Django: QuerySet with ExpressionWrapper
  • Pandas string search in list of dicts
  • Decryption from RSA encrypted string from sqlite is not the same
  • need of maximum value in int
  • a list of several tuples, how to extract the same of the first two elements in the small tuple in the large tuple
  • Display image of 2D Sinewaves in 3D
  • how to prevent a for loop from overwriting a dictionary?
  • How To Fix: RuntimeError: size mismatch in pyTorch
  • Concatenating two Pandas DataFrames while maintaining index order
  • Why does this not run into an infinite loop?
  • Python Multithreading no current event loop
  • Element Tree - Seaching for specific element value without looping
  • Ignore Nulls in pandas map dictionary
  • How do I get scrap data from web pages using beautifulsoup in python
  • Variable used, golobal or local?
  • I have a regex statement to pull all numbers out of a text file, but it only finds 77 out of the 81 numbers in the file
  • How do I create a dataframe of jobs and companies that includes hyperlinks?
  • Detect if user has clicked the 'maximized' button
  • Does flask_login automatically set the "next" argument?
  • Indents in python 3
  • How to create a pool of threads
  • Pandas giving IndexError on one dataframe but not on another similar dataframe
  • Django Rest Framework - Testing client.login doesn't login user, ret anonymous user
  • Running dag without dag file in airflow
  • Filling across a specified dimension of a numpy array
  • Python populating dataframe in pandas from text files
  • How to interpolate a single ("non-piecewise") cubic spline from a set of data points?
  • Divide 2 integers (leetcode 29) - recursion issue
  • Can someone explain why do I get this output in Python?
  • How do I scrape pdf and html from search results without obvious url
  • Is there a way to automatically make a "collage" of plots with matplotlib?
  • How to combine multiple rows in pandas with shared column values
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com