backward of Slice Args: dy (CTensor): gradient tensor. Returns: the gradient tensor over input tensor.
(self, dy)
| 4214 | return x |
| 4215 | |
| 4216 | def backward(self, dy): |
| 4217 | """ |
| 4218 | backward of Slice |
| 4219 | Args: |
| 4220 | dy (CTensor): gradient tensor. |
| 4221 | Returns: |
| 4222 | the gradient tensor over input tensor. |
| 4223 | """ |
| 4224 | for axis, shape, start, end, step in self.cache[::-1]: |
| 4225 | data_idxes = tuple(range(shape)[start:end:step]) |
| 4226 | dys = [] |
| 4227 | data_idx = 0 |
| 4228 | for step_idx in range(shape): |
| 4229 | if step_idx in data_idxes: |
| 4230 | tmp_tensor = singa.SliceOn(dy, data_idx, data_idx + 1, axis) |
| 4231 | data_idx += 1 |
| 4232 | else: |
| 4233 | tmp_shape = list(dy.shape()) |
| 4234 | tmp_shape[axis] = 1 |
| 4235 | tmp_tensor = singa.Tensor(tmp_shape, dy.device()) |
| 4236 | tmp_tensor.SetFloatValue(0.) |
| 4237 | dys.append(tmp_tensor) |
| 4238 | dys = singa.VecTensor(dys) |
| 4239 | dy = singa.ConcatOn(dys, axis) |
| 4240 | return dy |
| 4241 | |
| 4242 | |
| 4243 | def slice(x, starts, ends, axes=None, steps=None): |