Exemple #1
0
    def testPadForward(self):
        x = type('', (), {})()
        x.value = np.ndarray([3, 5, 6, 2])
        x.value.fill(0)
        x.value[0, :, :, 0] = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12],
                               [13, 14, 15, 16, 17, 18],
                               [19, 20, 21, 22, 23, 24],
                               [25, 26, 27, 28, 29, 30]]
        x.value[0, :, :, 1] = [[1, 2, 3, 3, 2, 1], [1, 1, 2, 2, 1, 1],
                               [1, 1, 1, 1, 1, 1], [1, 2, 3, 3, 2, 1],
                               [1, 1, 2, 2, 1, 1]]

        x.grad = x.value
        f = type('', (), {})()
        f.value = np.ndarray([2, 3, 2, 1])
        f.value[:, :, 0, 0] = [[1, 1, 1], [1, 1, 1]]
        f.value[:, :, 1, 0] = [[1, 0, -1], [1, 0, -1]]
        f.grad = f.value

        conv = Conv(f, x, 3, 1)
        conv.forward()
        self.assertEqual((3, 2, 2, 1), conv.value.shape)

        self.assertEqual(edf.DT(1), conv.value[0, 0, 0, 0])
        self.assertEqual(edf.DT(13), conv.value[0, 0, 1, 0])
        self.assertEqual(edf.DT(63), conv.value[0, 1, 0, 0])
        self.assertEqual(edf.DT(115), conv.value[0, 1, 1, 0])
Exemple #2
0
    def testForward(self):
        x = type('', (), {})()
        x.value = np.ndarray([2, 4, 6, 2], np.dtype(np.float64))
        x.value.fill(0)
        x.value[1, :, :, 0] = np.transpose([[1, 6, 2, 5], [0, 3, 2, 2],
                                            [7, 9, 5, 0], [0, 2, 0, 3],
                                            [0, 0, 6, 0], [3, 1, 4, 9]])
        x.grad = x.value

        ave = AvePool(x, 3, 2)

        ave.forward()

        self.assertEqual((2, 1, 2, 2), ave.value.shape)
        self.assertEqual(edf.DT(35) / 9, ave.value[1, 0, 0, 0])
        self.assertEqual(edf.DT(29) / 9, ave.value[1, 0, 1, 0])

        ave2 = AvePool(x, 2, 2)
        ave2.forward()
        self.assertEqual((2, 2, 3, 2), ave2.value.shape)
        self.assertEqual(edf.DT(10) / 4, ave2.value[1, 0, 0, 0])
        self.assertEqual(edf.DT(18) / 4, ave2.value[1, 0, 1, 0])
        self.assertEqual(edf.DT(4) / 4, ave2.value[1, 0, 2, 0])
        self.assertEqual(edf.DT(11) / 4, ave2.value[1, 1, 0, 0])
        self.assertEqual(edf.DT(8) / 4, ave2.value[1, 1, 1, 0])
        self.assertEqual(edf.DT(19) / 4, ave2.value[1, 1, 2, 0])
Exemple #3
0
    def __init__(self, x, ksz=2, stride=None):
        edf.components.append(self)
        self.x = x
        self.ksz = ksz
        if stride is None:
            self.stride = ksz
        else:
            self.stride = stride
        self.grad = None if x.grad is None else edf.DT(0)

        self.square = np.ndarray([self.ksz, self.ksz])
        self.square.fill(1)
Exemple #4
0
    def __init__(self, f, k, stride=1, pad=0):
        edf.components.append(self)
        self.f = f
        self.k = k
        pad = np.array(pad)
        if pad.shape == ():
            self.xpad = self.ypad = pad
        else:
            self.ypad = pad[0]
            self.xpad = pad[1]

        self.stride = stride
        self.grad = None if f.grad is None and k.grad is None else edf.DT(0)
Exemple #5
0
    def testBackward(self):
        x = type('', (), {})()
        x.value = np.ndarray([2, 4, 6, 2], np.dtype(np.float64))
        x.value.fill(0)
        x.grad = np.ndarray([2, 4, 6, 2], np.dtype(np.float64))
        x.grad.fill(0)

        ave = AvePool(x, 3, 1)
        ave.value = np.ndarray([2, 2, 4, 2], np.dtype(np.float64))
        ave.value.fill(0)
        ave.grad = np.ndarray([2, 2, 4, 2], np.dtype(np.float64))
        ave.grad.fill(0)

        ave.grad[0, :, :, 0] = [[1, 3, 5, 7], [2, 4, 6, 8]]

        ave.backward()

        self.assertEqual(edf.DT(1) / 9, x.grad[0, 0, 0, 0])
        self.assertEqual(edf.DT(4) / 9, x.grad[0, 0, 1, 0])
        self.assertEqual(edf.DT(9) / 9, x.grad[0, 0, 2, 0])
        self.assertEqual(edf.DT(15) / 9, x.grad[0, 0, 3, 0])
        self.assertEqual(edf.DT(12) / 9, x.grad[0, 0, 4, 0])
        self.assertEqual(edf.DT(7) / 9, x.grad[0, 0, 5, 0])

        self.assertEqual(edf.DT(3) / 9, x.grad[0, 1, 0, 0])
        self.assertEqual(edf.DT(10) / 9, x.grad[0, 1, 1, 0])
        self.assertEqual(edf.DT(21) / 9, x.grad[0, 1, 2, 0])
        self.assertEqual(edf.DT(33) / 9, x.grad[0, 1, 3, 0])
        self.assertEqual(edf.DT(26) / 9, x.grad[0, 1, 4, 0])
        self.assertEqual(edf.DT(15) / 9, x.grad[0, 1, 5, 0])

        self.assertEqual(edf.DT(3) / 9, x.grad[0, 2, 0, 0])
        self.assertEqual(edf.DT(10) / 9, x.grad[0, 2, 1, 0])
        self.assertEqual(edf.DT(21) / 9, x.grad[0, 2, 2, 0])
        self.assertEqual(edf.DT(33) / 9, x.grad[0, 2, 3, 0])
        self.assertEqual(edf.DT(26) / 9, x.grad[0, 2, 4, 0])
        self.assertEqual(edf.DT(15) / 9, x.grad[0, 2, 5, 0])

        self.assertEqual(edf.DT(2) / 9, x.grad[0, 3, 0, 0])
        self.assertEqual(edf.DT(6) / 9, x.grad[0, 3, 1, 0])
        self.assertEqual(edf.DT(12) / 9, x.grad[0, 3, 2, 0])
        self.assertEqual(edf.DT(18) / 9, x.grad[0, 3, 3, 0])
        self.assertEqual(edf.DT(14) / 9, x.grad[0, 3, 4, 0])
        self.assertEqual(edf.DT(8) / 9, x.grad[0, 3, 5, 0])
Exemple #6
0
    def testBackward2(self):
        x = type('', (), {})()
        x.value = np.ndarray([2, 4, 6, 2], np.dtype(np.float64))
        x.value.fill(0)
        x.value[0, :, :, 0] = np.transpose([[1, 4, 5, 5], [6, 3, 2, 5],
                                            [6, 5, 4, 4], [0, 2, 0, 3],
                                            [3, 3, 0, 6], [3, 1, 2, 9]])

        x.grad = np.ndarray([2, 4, 6, 2], np.dtype(np.float64))
        x.grad.fill(0)

        maxp = MaxPool(x, 3, 1)
        maxp.forward()
        self.assertEqual((2, 2, 4, 2), maxp.value.shape, '')
        maxp.grad = np.ndarray([2, 2, 4, 2], np.dtype(np.float64))
        maxp.grad.fill(0)
        maxp.grad[0, :, :, 0] = [[1, 2, 3, 4], [5, 6, 7, 8]]
        maxp.backward()

        self.assertEqual(edf.DT(0), x.grad[0, 0, 0, 0])
        self.assertEqual(edf.DT(3), x.grad[0, 0, 1, 0])
        self.assertEqual(edf.DT(6), x.grad[0, 0, 2, 0])
        self.assertEqual(edf.DT(0), x.grad[0, 0, 3, 0])
        self.assertEqual(edf.DT(4), x.grad[0, 0, 4, 0])
        self.assertEqual(edf.DT(4), x.grad[0, 0, 5, 0])

        self.assertEqual(edf.DT(0), x.grad[0, 1, 0, 0])
        self.assertEqual(edf.DT(0), x.grad[0, 1, 1, 0])
        self.assertEqual(edf.DT(11), x.grad[0, 1, 2, 0])
        self.assertEqual(edf.DT(0), x.grad[0, 1, 3, 0])
        self.assertEqual(edf.DT(4), x.grad[0, 1, 4, 0])
        self.assertEqual(edf.DT(0), x.grad[0, 1, 5, 0])

        self.assertEqual(edf.DT(5), x.grad[0, 2, 0, 0])
        self.assertEqual(edf.DT(0), x.grad[0, 2, 1, 0])
        self.assertEqual(edf.DT(0), x.grad[0, 2, 2, 0])
        self.assertEqual(edf.DT(0), x.grad[0, 2, 3, 0])
        self.assertEqual(edf.DT(0), x.grad[0, 2, 4, 0])
        self.assertEqual(edf.DT(0), x.grad[0, 2, 5, 0])

        self.assertEqual(edf.DT(5), x.grad[0, 3, 0, 0])
        self.assertEqual(edf.DT(11), x.grad[0, 3, 1, 0])
        self.assertEqual(edf.DT(0), x.grad[0, 3, 2, 0])
        self.assertEqual(edf.DT(0), x.grad[0, 3, 3, 0])
        self.assertEqual(edf.DT(7), x.grad[0, 3, 4, 0])
        self.assertEqual(edf.DT(8), x.grad[0, 3, 5, 0])
Exemple #7
0
    def testBackward1(self):
        x = type('', (), {})()
        x.value = np.ndarray([2, 4, 6, 2], np.dtype(np.float64))
        x.value.fill(0)
        x.value[1, :, :, 0] = np.transpose([[1, 6, 2, 5], [0, 3, 2, 5],
                                            [7, 9, 4, 4], [9, 2, 0, 3],
                                            [3, 3, 6, 0], [3, 1, 4, 9]])

        x.grad = np.ndarray([2, 4, 6, 2], np.dtype(np.float64))
        x.grad.fill(0)

        maxp = MaxPool(x, 2, 2)
        maxp.forward()

        maxp.grad = np.ndarray([2, 2, 3, 2], np.dtype(np.float64))
        maxp.grad.fill(0)
        maxp.grad[1, :, :, 0] = [[1, 3, 5], [2, 4, 6]]
        maxp.backward()

        self.assertEqual(edf.DT(0), x.grad[1, 0, 0, 0])
        self.assertEqual(edf.DT(0), x.grad[1, 0, 1, 0])
        self.assertEqual(edf.DT(0), x.grad[1, 0, 2, 0])
        self.assertEqual(edf.DT(3), x.grad[1, 0, 3, 0])
        self.assertEqual(edf.DT(5), x.grad[1, 0, 4, 0])
        self.assertEqual(edf.DT(5), x.grad[1, 0, 5, 0])

        self.assertEqual(edf.DT(1), x.grad[1, 1, 0, 0])
        self.assertEqual(edf.DT(0), x.grad[1, 1, 1, 0])
        self.assertEqual(edf.DT(3), x.grad[1, 1, 2, 0])
        self.assertEqual(edf.DT(0), x.grad[1, 1, 3, 0])
        self.assertEqual(edf.DT(5), x.grad[1, 1, 4, 0])
        self.assertEqual(edf.DT(0), x.grad[1, 1, 5, 0])

        self.assertEqual(edf.DT(0), x.grad[1, 2, 0, 0])
        self.assertEqual(edf.DT(0), x.grad[1, 2, 1, 0])
        self.assertEqual(edf.DT(4), x.grad[1, 2, 2, 0])
        self.assertEqual(edf.DT(0), x.grad[1, 2, 3, 0])
        self.assertEqual(edf.DT(0), x.grad[1, 2, 4, 0])
        self.assertEqual(edf.DT(0), x.grad[1, 2, 5, 0])

        self.assertEqual(edf.DT(2), x.grad[1, 3, 0, 0])
        self.assertEqual(edf.DT(2), x.grad[1, 3, 1, 0])
        self.assertEqual(edf.DT(4), x.grad[1, 3, 2, 0])
        self.assertEqual(edf.DT(0), x.grad[1, 3, 3, 0])
        self.assertEqual(edf.DT(0), x.grad[1, 3, 4, 0])
        self.assertEqual(edf.DT(6), x.grad[1, 3, 5, 0])
Exemple #8
0
    def testForward(self):
        x = type('', (), {})()
        x.value = np.ndarray([2, 4, 6, 2], np.dtype(np.float64))
        x.value.fill(0)
        x.value[1, :, :, 0] = np.transpose([[1, 6, 2, 5], [0, 3, 2, 2],
                                            [7, 9, 5, 0], [0, 2, 0, 3],
                                            [0, 0, 6, 0], [3, 1, 4, 9]])
        x.grad = x.value

        maxp = MaxPool(x, 2, 2)
        maxp.forward()
        self.assertEqual((2, 2, 3, 2), maxp.value.shape)

        self.assertEqual(edf.DT(6), maxp.value[1, 0, 0, 0])
        self.assertEqual(edf.DT(9), maxp.value[1, 0, 1, 0])
        self.assertEqual(edf.DT(3), maxp.value[1, 0, 2, 0])
        self.assertEqual(edf.DT(5), maxp.value[1, 1, 0, 0])
        self.assertEqual(edf.DT(5), maxp.value[1, 1, 1, 0])
        self.assertEqual(edf.DT(9), maxp.value[1, 1, 2, 0])

        maxp2 = MaxPool(x, 3, 1)
        maxp2.forward()
        self.assertEqual((2, 2, 4, 2), maxp2.value.shape)

        self.assertEqual(edf.DT(9), maxp2.value[1, 0, 0, 0])
        self.assertEqual(edf.DT(9), maxp2.value[1, 0, 1, 0])
        self.assertEqual(edf.DT(9), maxp2.value[1, 0, 2, 0])
        self.assertEqual(edf.DT(6), maxp2.value[1, 0, 3, 0])
        self.assertEqual(edf.DT(9), maxp2.value[1, 1, 0, 0])
        self.assertEqual(edf.DT(9), maxp2.value[1, 1, 1, 0])
        self.assertEqual(edf.DT(9), maxp2.value[1, 1, 2, 0])
        self.assertEqual(edf.DT(9), maxp2.value[1, 1, 3, 0])