1

I would like to get the normalized dot product of 2 matrices using Theano. By normalized dot product of 2 matrices, I define the normalized inner product of 2 vectors as the following: Take v_a from matrix A, and vector v_b from matrix B. AB__dot_norm = v_a * v_b / |v_a| |v_b|.

I can get the norm of v_a and v_b with the following code. I am not sure how to normalize the dot_product matrix with the normalized vectors.

import theano
from theano import tensor

dot_product = tensor.dot(in_tensor, w_tensor)    
in_normalized = in_tensor / in_tensor.norm (2, axis = 1).reshape(in_tensor.shape[0],1)
w_normalized = w_tensor / w_tensor.norm (2, axis = 0).reshape(1, w_tensor.shape[1])
Cœur
  • 37,241
  • 25
  • 195
  • 267
RKM
  • 3,151
  • 9
  • 37
  • 50

1 Answers1

0

I have figured out a solution, I thought it might be useful to post it here.

in_norm = in_tensor / in_tensor.norm(2, axis =1 ).reshape((in_tensor.shape[0],1))
w_norm = w_tensor / w_tensor.norm(2,axis = 0).reshape((1,  w_tensor.shape[1]))
output = theano.tensor.dot (in_norm, w_norm)
RKM
  • 3,151
  • 9
  • 37
  • 50