backward of embedding Args: dy (CTensor): gradient tensor. Raises: the gradient tensor over input tensor.
(self, dy)
| 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 | |
| 5715 | def embedding(x, w): |