forward of CosSim Args: a (CTensor): input tensor. b (CTensor): input tensor. Returns: the output CTensor.
(self, a, b)
| 4960 | return singa.Reshape(ret, [ret.shape()[0]]) # b |
| 4961 | |
| 4962 | def forward(self, a, b): |
| 4963 | """ |
| 4964 | forward of CosSim |
| 4965 | Args: |
| 4966 | a (CTensor): input tensor. |
| 4967 | b (CTensor): input tensor. |
| 4968 | Returns: |
| 4969 | the output CTensor. |
| 4970 | """ |
| 4971 | ad = CosSim.dot(a, a) |
| 4972 | bd = CosSim.dot(b, b) |
| 4973 | ap = singa.PowFloat(ad, 0.5) |
| 4974 | bp = singa.PowFloat(bd, 0.5) |
| 4975 | ret = singa.__div__(CosSim.dot(a, b), singa.__mul__(ap, bp)) |
| 4976 | if training: |
| 4977 | self.cache = (a, b, ad, bd, ap, bp, ret) |
| 4978 | return ret |
| 4979 | |
| 4980 | def backward(self, dy): |
| 4981 | """ |