Python chainer.testing.product() Examples

The following are 20 code examples of chainer.testing.product(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chainer.testing , or try the search function .
Example #1
Source File: test_l2_normalization.py    From chainer with MIT License 6 votes vote down vote up
def check_forward(self, x_data, axis):
        eps = self.eps
        x = chainer.Variable(x_data)

        y = functions.normalize(x, eps=eps, axis=axis)
        self.assertEqual(y.data.dtype, self.dtype)
        y_data = cuda.to_cpu(y.data)

        y_expect = numpy.empty_like(self.x)
        shape = self.x.shape
        indices = []
        axis_tuple = axis if isinstance(axis, tuple) else (axis,)
        for i in six.moves.range(len(shape)):
            if i not in axis_tuple:
                indices.append(six.moves.range(shape[i]))
            else:
                indices.append([slice(None)])
        indices_tuple = list(itertools.product(*indices))
        for index in indices_tuple:
            # Note: Casting back the result of `numpy.linalg.norm` to `x.dtype`
            # because old NumPy casts it to float32 when a float16 value is
            # given.
            numerator = numpy.linalg.norm(self.x[index]).astype(x.dtype) + eps
            y_expect[index] = self.x[index] / numerator
        testing.assert_allclose(y_expect, y_data, **self.check_forward_options) 
Example #2
Source File: test_read_image.py    From chainercv with MIT License 6 votes vote down vote up
def _create_parameters():
    params = testing.product({
        'file_obj': [False, True],
        'size': [(48, 32)],
        'dtype': [np.float32, np.uint8, bool]})
    no_color_params = testing.product({
        'format': ['bmp', 'jpeg', 'png'],
        'color': [False],
        'alpha': [None]})
    no_alpha_params = testing.product({
        'format': ['bmp', 'jpeg', 'png'],
        'color': [True],
        'alpha': [None]})
    alpha_params = testing.product({
        # writing alpha image with jpeg encoding didn't work
        'format': ['png'],
        'color': [True],
        'alpha': ['ignore', 'blend_with_white', 'blend_with_black']})
    params = testing.product_dict(
        params,
        no_color_params + no_alpha_params + alpha_params)
    return params 
Example #3
Source File: test_split_axis.py    From chainer with MIT License 6 votes vote down vote up
def inject_backend_tests():
    decorator = backend.inject_backend_tests(
        None,
        # CPU tests
        testing.product({
            'use_cuda': [False],
            'use_ideep': ['never', 'always'],
        })
        # GPU tests
        + [{'use_cuda': True}]
        # ChainerX tests
        + [
            {'use_chainerx': True, 'chainerx_device': 'native:0'},
            {'use_chainerx': True, 'chainerx_device': 'cuda:0'},
            {'use_chainerx': True, 'chainerx_device': 'cuda:1'},
        ])
    return decorator 
Example #4
Source File: test_snapshot.py    From chainer with MIT License 6 votes vote down vote up
def test_find_snapshot_files(self):
        noise = ('tmpsnapshot_iter_{}.{}'.format(i, j)
                 for i, j in zip(range(10, 304), range(10, 200)))

        for file in itertools.chain(noise, self.files):
            file = os.path.join(self.path, file)
            open(file, 'w').close()

        snapshot_files = _find_snapshot_files(self.fmt, self.path)

        expected = [self.fmt.format(i*10, j*10)
                    for i, j in itertools.product(range(0, 10), range(0, 10))]

        timestamps, snapshot_files = zip(*snapshot_files)
        expected.sort()
        snapshot_files = sorted(list(snapshot_files))
        assert expected == snapshot_files 
Example #5
Source File: test_unpooling_nd.py    From chainer with MIT License 5 votes vote down vote up
def xs_iter(dims):
    return itertools.product(*[range(d) for d in dims]) 
Example #6
Source File: test_function_node.py    From chainer with MIT License 5 votes vote down vote up
def make_array(start, shape, dtype, device):
    size = numpy.product(shape, dtype='i')
    a = numpy.arange(start, start + size)
    a = a.reshape(shape)
    a = a.astype(dtype, copy=False)
    return device.send(a) 
Example #7
Source File: test_coco_bbox_dataset.py    From chainercv with MIT License 5 votes vote down vote up
def _create_paramters():
    split_years = testing.product({
        'split': ['train', 'val'],
        'year': ['2014', '2017']})
    split_years += [{'split': 'minival', 'year': '2014'},
                    {'split': 'valminusminival', 'year': '2014'}]
    use_and_return_args = testing.product({
        'use_crowded': [False, True],
        'return_crowded': [False, True],
        'return_area': [False, True]})
    params = testing.product_dict(
        split_years,
        use_and_return_args)
    return params 
Example #8
Source File: test_coco_instance_segmentation_dataset.py    From chainercv with MIT License 5 votes vote down vote up
def _create_paramters():
    split_years = testing.product({
        'split': ['train', 'val'],
        'year': ['2014', '2017']})
    split_years += [{'split': 'minival', 'year': '2014'},
                    {'split': 'valminusminival', 'year': '2014'}]
    use_and_return_args = testing.product({
        'use_crowded': [False, True],
        'return_crowded': [False, True],
        'return_area': [False, True],
        'return_bbox': [False, True]})
    params = testing.product_dict(
        split_years,
        use_and_return_args)
    return params 
Example #9
Source File: test_voc_bbox_dataset.py    From chainercv with MIT License 5 votes vote down vote up
def _create_paramters():
    split_years = testing.product({
        'split': ['train', 'trainval', 'val'],
        'year': ['2007', '2012']})
    split_years += [{'split': 'test', 'year': '2007'}]
    params = testing.product_dict(
        split_years,
        [{'use_difficult': True, 'return_difficult': True},
         {'use_difficult': True, 'return_difficult': False},
         {'use_difficult': False, 'return_difficult': True},
         {'use_difficult': False, 'return_difficult': False}])
    return params 
Example #10
Source File: test_parameterized.py    From chainer with MIT License 5 votes vote down vote up
def test_product(self):
        self.assertListEqual(testing.product(self.actual), self.expect) 
Example #11
Source File: test_conv_nd.py    From chainer with MIT License 5 votes vote down vote up
def check_col2im_nd(self, ksize, stride, pad, gpu):
        dims = self.dims
        outs = tuple(conv_nd.get_conv_outsize(d, k, s, p)
                     for (d, k, s, p) in zip(dims, ksize, stride, pad))
        col_shape = (2, 3) + ksize + outs
        col = numpy.random.uniform(-1, 1, col_shape).astype(numpy.float32)

        if gpu:
            col_data = cuda.to_gpu(col)
        else:
            col_data = col

        img = conv_nd.col2im_nd(col_data, stride, pad, dims)
        img = cuda.to_cpu(img)
        img_shape = (2, 3) + dims
        self.assertEqual(img.shape, img_shape)
        for n in moves.range(2):
            for c in moves.range(3):
                for xs in itertools.product(
                        *[moves.range(d) for d in dims]):
                    v = numpy.float32(0.0)
                    for dxs in itertools.product(
                            *[moves.range(k) for k in ksize]):
                        oxs = tuple((x + p - dx) // s
                                    for (x, p, dx, s)
                                    in zip(xs, pad, dxs, stride))
                        if all((x + p - dx) % s == 0
                               for (x, p, dx, s)
                               in zip(xs, pad, dxs, stride)) and \
                            all(0 <= ox < out
                                for (ox, out) in zip(oxs, outs)):
                            col_index = (n, c) + dxs + oxs
                            v += col[col_index]
                    img_index = (n, c) + xs
                    self.assertAlmostEqual(img[img_index], v) 
Example #12
Source File: test_conv_nd.py    From chainer with MIT License 5 votes vote down vote up
def check_im2col_nd(self, ksize, stride, pad, gpu):
        dims = self.dims
        if gpu:
            img = cuda.to_gpu(self.img)
        else:
            img = self.img

        col = conv_nd.im2col_nd(img, ksize, stride, pad)
        outs = tuple(conv_nd.get_conv_outsize(d, k, s, p)
                     for (d, k, s, p) in zip(dims, ksize, stride, pad))
        expected_shape = (2, 3) + ksize + outs
        self.assertEqual(col.shape, expected_shape)

        col = cuda.to_cpu(col)

        for n in moves.range(2):
            for c in moves.range(3):
                for xs in itertools.product(
                        *[moves.range(out) for out in outs]):
                    for dxs in itertools.product(
                            *[moves.range(k) for k in ksize]):
                        oxs = tuple(x * s - p + dx
                                    for (x, s, p, dx)
                                    in zip(xs, stride, pad, dxs))
                        if all(0 <= ox < d for (ox, d) in zip(oxs, dims)):
                            col_index = (n, c) + dxs + xs
                            img_index = (n, c) + oxs
                            self.assertEqual(
                                col[col_index], self.img[img_index])
                        else:
                            col_index = (n, c) + dxs + xs
                            self.assertEqual(col[col_index], 0) 
Example #13
Source File: test_unpooling_nd.py    From chainer with MIT License 5 votes vote down vote up
def kxs_iter(x, outs, ksize, stride, pad):
    return itertools.product(
        *[range(max(0, -p + s * _x), min(-p + s * _x + k, out))
          for (_x, out, k, s, p) in zip(x, outs, ksize, stride, pad)]) 
Example #14
Source File: test_trpo.py    From chainerrl with MIT License 5 votes vote down vote up
def test_first_order(self):
        # First order, so its Hessian will contain None
        params, y = self._generate_params_and_first_order_output()

        old_style_funcs = trpo._find_old_style_function([y])
        if old_style_funcs:
            self.skipTest("\
Chainer v{} does not support double backprop of these functions: {}.".format(
                chainer.__version__, old_style_funcs))

        vec = np.random.rand(4).astype(np.float32)
        # Hessian-vector product computation should raise an error due to None
        with self.assertRaises(AssertionError):
            compute_hessian_vector_product(y, params, vec) 
Example #15
Source File: test_function_slstm.py    From chainer with MIT License 5 votes vote down vote up
def inject_backend_tests(method_names):
    decorator = backend.inject_backend_tests(
        method_names,
        # CPU tests
        testing.product({
            'use_cuda': [False],
            'use_ideep': ['never', 'always'],
        }) +
        # GPU tests
        [{'use_cuda': True}])
    return decorator 
Example #16
Source File: test_function_lstm.py    From chainer with MIT License 5 votes vote down vote up
def inject_backend_tests(method_names):
    decorator = backend.inject_backend_tests(
        method_names,
        # CPU tests
        testing.product({
            'use_cuda': [False],
            'use_ideep': ['never', 'always'],
        })
        # GPU tests
        + [{'use_cuda': True}])
    return decorator 
Example #17
Source File: test_snapshot.py    From chainer with MIT License 5 votes vote down vote up
def setUp(self):
        self.path = tempfile.mkdtemp()
        self.files = (self.fmt.format(i*10, j*10) for i, j
                      in itertools.product(range(0, 10), range(0, 10))) 
Example #18
Source File: test_independent.py    From chainer with MIT License 5 votes vote down vote up
def _generate_test_parameter(
        parameter_list, inner_shape, inner_event_shape,
        reinterpreted_batch_ndims):
    shape_pattern = _generate_valid_shape_pattern(
        inner_shape, inner_event_shape, reinterpreted_batch_ndims)
    return [
        dict(dicts[0], **dicts[1])
        for dicts in itertools.product(parameter_list, shape_pattern)
    ] 
Example #19
Source File: test_crf1d.py    From chainer with MIT License 5 votes vote down vote up
def _crf1d(self, cost_data, xs_data, ys_data):
        z = numpy.zeros((self.batches[0],), numpy.float32)
        for b, length in enumerate(self.lengths):
            for ys in itertools.product(range(self.n_label), repeat=length):
                z[b] += numpy.exp(chainer.cuda.to_cpu(self._calc_score(b, ys)))

        score = numpy.zeros((self.batches[0],), numpy.float32)
        for b, length in enumerate(self.lengths):
            ys = [self.ys[i][b] for i in range(length)]
            score[b] = self._calc_score(b, ys)

        loss = -(score - numpy.log(z))
        return numpy.sum(loss) / self.batches[0] 
Example #20
Source File: test_function.py    From chainer with MIT License 5 votes vote down vote up
def make_array(start, shape, dtype):
    size = numpy.product(shape, dtype='i')
    a = numpy.arange(start, start + size)
    a = a.reshape(shape)
    a = a.astype(dtype, copy=False)
    return a