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

Method backward

python/singa/autograd.py:5687–5712  ·  view source on GitHub ↗

backward of embedding Args: dy (CTensor): gradient tensor. Raises: the gradient tensor over input tensor.

(self, dy)

Source from the content-addressed store, hash-verified

5685 return xs
5686
5687 def backward(self, dy):
5688 """
5689 backward of embedding
5690 Args:
5691 dy (CTensor): gradient tensor.
5692 Raises:
5693 the gradient tensor over input tensor.
5694 """
5695 x, w_shape = self.cache
5696 dy_shape = dy.shape()
5697 # construct the dx
5698 dx = tensor.sum(tensor.from_raw_tensor(dy), axis=2)
5699
5700 # construct the dw
5701 dws = []
5702 for idx in range(w_shape[0]):
5703 tmp_tensor = singa.Tensor((1, w_shape[1]), dy.device())
5704 tmp_tensor.SetFloatValue(0.0)
5705 dws.append(tmp_tensor)
5706 dy = singa.Reshape(dy, [dy_shape[0] * dy_shape[1], dy_shape[2]])
5707 x = x.reshape(-1)
5708 for idx, val in enumerate(x):
5709 tmp_tensor = singa.SliceOn(dy, idx, idx + 1, 0)
5710 dws[val] = singa.__add__(dws[val], tmp_tensor)
5711 dws = singa.VecTensor(dws)
5712 return dx.data, singa.ConcatOn(dws, 0)
5713
5714
5715def embedding(x, w):

Callers

nothing calls this directly

Calls 6

shapeMethod · 0.80
TensorMethod · 0.80
deviceMethod · 0.80
appendMethod · 0.80
reshapeMethod · 0.45
__add__Method · 0.45

Tested by

no test coverage detected