Crate caffe2op_distance
source ·Structs
- | This op takes two input float tensors of the same | size, $X$ and $Y$, and produces one output float | tensor , $Z$, calculated as the cosine similarity | between $X$ and $Y$. | | Recall, the cosine similarity between two tensors | $X$ and $Y$ is defined as: | | $$\mathbf{Z}=CosineSimilarity(\mathbf{X},\mathbf{Y}) = | \frac{\mathbf{X}\cdot\mathbf{Y}}{|\mathbf{X}||\mathbf{Y}|} = | \frac{\sum_n^{i=1}X_iY_i}{\sqrt{\sum_n^{i=1}X_i^2}\sqrt{\sum_n^{i=1}Y_i^2}}$$ | | Github Links: | - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/distance_op.h | - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/distance_op.cc | || |
Example
|
- | Computes and outputs the dot product of the two | input float tensors
X
andY
. | | Note thatX
andY
must be either 1D or 2D, and | they must be the same shape. | | The output tensor is 1D, which represents either | the product of each element in a respective | dimension if the inputs are 1D, or the sum of the | products in a given dimension if the inputs are 2D | matrices. Note that the actual dot product is | a scalar value, which is effectively the sum of | the elements in the 1D output tensor. | | For 1D inputs: | Given two vectors | $X = [x_0, x_1, x_2]$ | and $Y = [y_0, y_1, y_2]$; | $Z = [x_0 * y_0, x_1 * y_1, x_2 * y_2]$ | | For 2D inputs: | Given two matrices: | $$X = [ | [x_0^0, x_1^0, x_2^0], | \ [x_0^1, x_1^1, x_2^1], | \ [x_0^2, x_1^2, x_2^2], | \ …, | \ [x_0^n, x_1^n, x_2^n] | ]$$ | | and | | $$Y = [ | [y_0^0, y_1^0, y_2^0], | \ [y_0^1, y_1^1, y_2^1], | \ [y_0^2, y_1^2, y_2^2], | \ …, | \ [y_0^n, y_1^n, y_2^n] | ]$$ | | then | | $$Z = \biggl[ | \Big((x_0^0 * y_0^0) + (x_1^0 * y_1^0) + (x_2^0 * y_2^0)\Big), | \ \Big((x_0^1 * y_0^1) + (x_1^1 * y_1^1) + (x_2^1 * y_2^1)\Big), | \ \Big((x_0^2 * y_0^2) + (x_1^2 * y_1^2) + (x_2^2 * y_2^2)\Big), | \ …, | \ \Big((x_0^n * y_0^n) + (x_1^n * y_1^n) + (x_2^n * y_2^n)\Big)\biggr]$$ | | Github Link: | - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/distance_op.cc |
- | Given two input float tensors X, Y with | different shapes and produces one output | float tensor of the dot product between | X and Y. | | We currently support two kinds of strategies | to achieve this. | | Before doing normal dot_product | | 1) pad the smaller tensor (using pad_value) | to the same shape as the other one. | | 2) replicate the smaller tensor to the | same shape as the other one. | | Note the first dimension of X, Y must | be equal. Only the second dimension | of X or Y can be padded. |
- | Computes the row-wise L1 Distance between | the two input tensors $X$ and $Y$, which | is defined as | | $$L1Distance(\mathbf{x},\mathbf{y}) | = \sum_{i}\mid x_i - y_i\mid$$ | | Note, both inputs must either be 1-dimensional | or 2-dimensional and both must have | the same shape. | | The output $Z$ will be 1-dimensional | regardless and its length will equal | the number of rows in the inputs. | | Github Links: | | - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/distance_op.h | | - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/distance_op.cc |
- | Given two input float tensors X, Y, and | produces one output float tensor of | the L2 difference between X and Y that | is computed as ||(X - Y)^2 / 2||. |