| 36 | |
| 37 | ### from autograd.py |
| 38 | class SumError(Operator): |
| 39 | |
| 40 | def __init__(self): |
| 41 | super(SumError, self).__init__() |
| 42 | # self.t = t.data |
| 43 | |
| 44 | def forward(self, x): |
| 45 | # self.err = singa.__sub__(x, self.t) |
| 46 | self.data_x = x |
| 47 | # sqr = singa.Square(self.err) |
| 48 | # loss = singa.SumAll(sqr) |
| 49 | loss = singa.SumAll(x) |
| 50 | # self.n = 1 |
| 51 | # for s in x.shape(): |
| 52 | # self.n *= s |
| 53 | # loss /= self.n |
| 54 | return loss |
| 55 | |
| 56 | def backward(self, dy=1.0): |
| 57 | # dx = self.err |
| 58 | dev = device.get_default_device() |
| 59 | dx = tensor.Tensor(self.data_x.shape, dev, singa_dtype['float32']) |
| 60 | dx.copy_from_numpy(np.ones(self.data_x.shape)) |
| 61 | # dx *= float(2 / self.n) |
| 62 | dx *= dy |
| 63 | return dx |
| 64 | |
| 65 | def se_loss(x): |
| 66 | # assert x.shape == t.shape, "input and target shape different: %s, %s" % ( |