MCPcopy Create free account
hub / github.com/apache/singa / backward

Method backward

python/singa/autograd.py:4980–5002  ·  view source on GitHub ↗

backward of CosSim follow https://math.stackexchange.com/a/1923705 Args: dy (CTensor): gradient tensor. Return: the gradient tensor over input tensor.

(self, dy)

Source from the content-addressed store, hash-verified

4978 return ret
4979
4980 def backward(self, dy):
4981 """
4982 backward of CosSim
4983 follow https://math.stackexchange.com/a/1923705
4984 Args:
4985 dy (CTensor): gradient tensor.
4986 Return:
4987 the gradient tensor over input tensor.
4988 """
4989 a, b, ad, bd, ap, bp, ret = self.cache
4990 ab = singa.__mul__(ap, bp)
4991 ab = singa.Reshape(ab, list(ab.shape()) + [1]) # b * 1
4992 ad = singa.Reshape(ad, list(ad.shape()) + [1]) # b * 1
4993 bd = singa.Reshape(bd, list(bd.shape()) + [1]) # b * 1
4994 ret = singa.Reshape(ret, list(ret.shape()) + [1]) # b * 1
4995 dy = singa.Reshape(dy, list(dy.shape()) + [1]) # boardcast
4996 da = singa.__sub__(singa.__div__(b, ab),
4997 singa.__div__(singa.__mul__(ret, a), ad))
4998 db = singa.__sub__(singa.__div__(a, ab),
4999 singa.__div__(singa.__mul__(ret, b), bd))
5000 da = singa.__mul__(dy, da)
5001 db = singa.__mul__(dy, db)
5002 return da, db
5003
5004
5005def cossim(a, b):

Callers

nothing calls this directly

Calls 4

shapeMethod · 0.80
__mul__Method · 0.45
__sub__Method · 0.45
__div__Method · 0.45

Tested by

no test coverage detected