使用Theano计算两个矩阵的归一化点积

Calculate the normalized dot product of two matrices using Theano

我想使用 Theano 获得 2 个矩阵的标准化点积。通过 2 个矩阵的归一化点积,我定义 2 个向量的归​​一化内积如下: 从矩阵 A 中获取 v_a,从矩阵 B 中获取向量 v_b。 AB__dot_norm = v_a * v_b / |v_a| |v_b|。

我可以用下面的代码得到v_a和v_b的范数。我不确定如何使用归一化向量对 dot_product 矩阵进行归一化。

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])

我找到了一个解决方案,我认为在这里 post 它可能会有用。

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)