def test_gradient(self): for kernel_h in range(3, 5): for kernel_w in range(3, 5): channels = 12 height = 3 width = 5 bottom = Array.zeros((5, channels, height, width), np.int32) bottom_diff = Array.zeros_like(bottom) for n in range(5): for c in range(channels): bottom[n, c] = Array.array([[1, 2, 5, 2, 3], [9, 4, 1, 4, 8], [1, 2, 5, 2, 3]]).astype(np.int32) param = self.layer[5] param.pooling_param.kernel_h = kernel_h param.pooling_param.kernel_w = kernel_w param.pooling_param.stride = 2 param.pooling_param.pad = 1 layer = PoolingLayer(param) top_shape = layer.get_top_shape(bottom) top = Array.zeros(top_shape, np.int32) top_diff = Array.zeros_like(top) checker = GradientChecker(1e-4, 1e-2) checker.check_gradient_exhaustive(layer, bottom, bottom_diff, top, top_diff)
def test_gradient(self): for kernel_h in range(3, 5): for kernel_w in range(3, 5): channels = 12 height = 3 width = 5 bottom = Array.zeros((5, channels, height, width), np.int32) bottom_diff = Array.zeros_like(bottom) for n in range(5): for c in range(channels): bottom[n, c] = Array.array( [[1, 2, 5, 2, 3], [9, 4, 1, 4, 8], [1, 2, 5, 2, 3]]).astype(np.int32) param = self.layer[5] param.pooling_param.kernel_h = kernel_h param.pooling_param.kernel_w = kernel_w param.pooling_param.stride = 2 param.pooling_param.pad = 1 layer = PoolingLayer(param) top_shape = layer.get_top_shape(bottom) top = Array.zeros(top_shape, np.int32) top_diff = Array.zeros_like(top) checker = GradientChecker(1e-4, 1e-2) checker.check_gradient_exhaustive(layer, bottom, bottom_diff, top, top_diff)
def test_simple(self): channels = 12 height = 3 width = 5 bottom = Array.zeros((5, channels, height, width), np.int32) for n in range(5): for c in range(channels): bottom[n, c] = Array.array([[1, 2, 5, 2, 3], [9, 4, 1, 4, 8], [1, 2, 5, 2, 3]]).astype(np.int32) param = self.layer[5] param.pooling_param.kernel_size = 2 param.pooling_param.stride = 1 layer = PoolingLayer(param) actual_shape = layer.get_top_shape(bottom) actual = Array.zeros(actual_shape, np.int32) layer.setup(bottom, actual) layer.forward(bottom, actual) for n in range(5): for c in range(channels): np.testing.assert_array_equal( actual[n, c], np.array([[9, 5, 5, 8], [9, 5, 5, 8]]).astype(np.int32)) bottom = Array.zeros_like(bottom) for n in range(5): for c in range(channels): actual[n, c] = Array.array([[1, 1, 1, 1], [1, 1, 1, 1]]).astype(np.int32) layer.backward(bottom, actual) for n in range(5): for c in range(channels): np.testing.assert_array_equal( bottom[n, c], np.array([[0, 0, 2, 0, 0], [2, 0, 0, 0, 2], [0, 0, 2, 0, 0]]).astype(np.int32))
def test_simple(self): bottom = Array.rand(3, 8, 32, 32).astype(np.float32) actual = Array.zeros_like(bottom) layer = LRNLayer(self.layer[4]) param = layer.layer_param.lrn_param alpha = param.alpha size = param.local_size beta = param.beta layer.setup(bottom, actual) layer.forward(bottom, actual) expected = Array.zeros_like(bottom) for n in range(bottom.shape[0]): for c in range(bottom.shape[1]): for h in range(bottom.shape[2]): for w in range(bottom.shape[3]): c_start = c - (size - 1) // 2 c_end = min(c_start + size, bottom.shape[1]) scale = 1 for i in range(c_start, c_end): value = bottom[n, i, h, w] scale += value * value * alpha / size expected = bottom[n, c, h, w] / pow(scale, beta) self.assertTrue( abs(actual[n, c, h, w] - expected) < 1e-4)
def test_simple(self): channels = 12 height = 3 width = 5 bottom = Array.zeros((5, channels, height, width), np.int32) for n in range(5): for c in range(channels): bottom[n, c] = Array.array( [[1, 2, 5, 2, 3], [9, 4, 1, 4, 8], [1, 2, 5, 2, 3]]).astype(np.int32) param = self.layer[5] param.pooling_param.kernel_size = 2 param.pooling_param.stride = 1 layer = PoolingLayer(param) actual_shape = layer.get_top_shape(bottom) actual = Array.zeros(actual_shape, np.int32) layer.setup(bottom, actual) layer.forward(bottom, actual) for n in range(5): for c in range(channels): np.testing.assert_array_equal( actual[n, c], np.array([ [9, 5, 5, 8], [9, 5, 5, 8] ]).astype(np.int32)) bottom = Array.zeros_like(bottom) for n in range(5): for c in range(channels): actual[n, c] = Array.array( [[1, 1, 1, 1], [1, 1, 1, 1]]).astype(np.int32) layer.backward(bottom, actual) for n in range(5): for c in range(channels): np.testing.assert_array_equal( bottom[n, c], np.array([[0, 0, 2, 0, 0], [2, 0, 0, 0, 2], [0, 0, 2, 0, 0]]).astype(np.int32))
def setup(self, bottom, top): self.scale = Array.zeros_like(bottom)
def setup(self, bottom_data, bottom_label, top): self.prob = Array.zeros_like(bottom_data) self.softmax_layer.setup(bottom_data, self.prob)
def setup(self, bottom_data, bottom_label, top): self.prob = Array.zeros_like(bottom_data) self.softmax_layer.setup(bottom_data, self.prob)