Python numpy.ndindex() Examples

The following are 30 code examples of numpy.ndindex(). 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 numpy , or try the search function .
Example #1
Source File: tfdeploy.py    From tfdeploy with MIT License 6 votes vote down vote up
def _pool_patches(a, k, strides, padding):
    f = np.ones(k[1:] + [a.shape[-1]])

    out_shape, src = _prepare_patches(a, f, strides, padding, "edge")

    patches = np.empty(tuple(out_shape) + f.shape).astype(a.dtype)

    s = (slice(None),)
    e = (Ellipsis,)
    en = (Ellipsis, np.newaxis)
    for coord in np.ndindex(*out_shape[1:]):
        pos = np.array(strides[1:]) * coord
        patches[s + coord + e] = \
            src[s + tuple(slice(*tpl) for tpl in zip(pos, pos + f.shape[:-1]))][en] * f

    return patches 
Example #2
Source File: test_upsampling_2d.py    From chainer with MIT License 6 votes vote down vote up
def check_forward(self, y):
        y = F.upsampling_2d(
            self.pooled_y, self.indices, ksize=self.ksize,
            stride=self.stride, outsize=self.in_shape[2:])
        if isinstance(y.array, numpy.ndarray):
            y = conv.im2col_cpu(
                y.array, self.ksize, self.ksize, self.stride, self.stride,
                0, 0)
        else:
            y = conv.im2col_gpu(
                y.array, self.ksize, self.ksize, self.stride, self.stride,
                0, 0)
        for i in numpy.ndindex(y.shape):
            n, c, ky, kx, oy, ox = i
            up_y = y[n, c, ky, kx, oy, ox]
            if ky * y.shape[3] + kx == self.indices[n, c, oy, ox]:
                in_y = self.pooled_y.array[n, c, oy, ox]
                testing.assert_allclose(in_y, up_y)
            else:
                testing.assert_allclose(up_y, 0) 
Example #3
Source File: test_index_tricks.py    From Computable with MIT License 6 votes vote down vote up
def test_ndindex():
    x = list(np.ndindex(1, 2, 3))
    expected = [ix for ix, e in np.ndenumerate(np.zeros((1, 2, 3)))]
    assert_array_equal(x, expected)

    x = list(np.ndindex((1, 2, 3)))
    assert_array_equal(x, expected)

    # Test use of scalars and tuples
    x = list(np.ndindex((3,)))
    assert_array_equal(x, list(np.ndindex(3)))

    # Make sure size argument is optional
    x = list(np.ndindex())
    assert_equal(x, [()])

    x = list(np.ndindex(()))
    assert_equal(x, [()])

    # Make sure 0-sized ndindex works correctly
    x = list(np.ndindex(*[0]))
    assert_equal(x, []) 
Example #4
Source File: ngram.py    From BERT with Apache License 2.0 6 votes vote down vote up
def call(self, inputs):
    batch_shape = tf.shape(inputs)[:-1]
    length = tf.shape(inputs)[-1]
    ngram_range_counts = []
    for n in range(self.minval, self.maxval):
      # Reshape inputs from [..., length] to [..., 1, length // n, n], dropping
      # remainder elements. Each n-vector is an ngram.
      reshaped_inputs = tf.reshape(
          inputs[..., :(n * (length // n))],
          tf.concat([batch_shape, [1], (length // n)[tf.newaxis], [n]], 0))
      # Count the number of times each ngram appears in the input. We do so by
      # checking whether each n-vector in the input is equal to each n-vector
      # in a Tensor of all possible ngrams. The comparison is batched between
      # the input Tensor of shape [..., 1, length // n, n] and the ngrams Tensor
      # of shape [..., input_dim**n, 1, n].
      ngrams = tf.reshape(
          list(np.ndindex((self.input_dim,) * n)),
          [1] * (len(inputs.shape)-1) + [self.input_dim**n, 1, n])
      is_ngram = tf.equal(
          tf.reduce_sum(tf.cast(tf.equal(reshaped_inputs, ngrams), tf.int32),
                        axis=-1),
          n)
      ngram_counts = tf.reduce_sum(tf.cast(is_ngram, tf.float32), axis=-1)
      ngram_range_counts.append(ngram_counts)
    return tf.concat(ngram_range_counts, axis=-1) 
Example #5
Source File: video.py    From MachineLearning with Apache License 2.0 6 votes vote down vote up
def __init__(self, **kw):
        super(Chess, self).__init__(**kw)

        w, h = self.frame_size

        self.grid_size = sx, sy = 10, 7
        white_quads = []
        black_quads = []
        for i, j in np.ndindex(sy, sx):
            q = [[j, i, 0], [j + 1, i, 0], [j + 1, i + 1, 0], [j, i + 1, 0]]
            [white_quads, black_quads][(i + j) % 2].append(q)
        self.white_quads = np.float32(white_quads)
        self.black_quads = np.float32(black_quads)

        fx = 0.9
        self.K = np.float64([[fx * w, 0, 0.5 * (w - 1)],
                             [0, fx * w, 0.5 * (h - 1)],
                             [0.0, 0.0, 1.0]])

        self.dist_coef = np.float64([-0.2, 0.1, 0, 0])
        self.t = 0 
Example #6
Source File: test_softmax_cross_entropy.py    From chainer with MIT License 6 votes vote down vote up
def expected_forward_without_reduce(self, x_data, t_data, class_weight):
        x = numpy.rollaxis(x_data, 1, x_data.ndim).reshape(
            (t_data.size, x_data.shape[1]))
        t = t_data.ravel()

        loss_shape = x_data.shape[0:1] + x_data.shape[2:]
        loss_expect = numpy.zeros(loss_shape, x_data.dtype)
        for i, (ti, loss_idx) in enumerate(zip(t, numpy.ndindex(*loss_shape))):
            xi = x[i]
            if ti == -1:
                continue
            log_z = numpy.ufunc.reduce(numpy.logaddexp, xi)
            if class_weight is None:
                loss_expect[loss_idx] = -(xi - log_z)[ti]
            else:
                loss_expect[loss_idx] = -(xi - log_z)[ti] * class_weight[ti]
        return numpy.asarray(loss_expect, dtype=x.dtype) 
Example #7
Source File: ngram.py    From training_results_v0.5 with Apache License 2.0 6 votes vote down vote up
def call(self, inputs):
    batch_shape = tf.shape(inputs)[:-1]
    length = tf.shape(inputs)[-1]
    ngram_range_counts = []
    for n in range(self.minval, self.maxval):
      # Reshape inputs from [..., length] to [..., 1, length // n, n], dropping
      # remainder elements. Each n-vector is an ngram.
      reshaped_inputs = tf.reshape(
          inputs[..., :(n * (length // n))],
          tf.concat([batch_shape, [1], (length // n)[tf.newaxis], [n]], 0))
      # Count the number of times each ngram appears in the input. We do so by
      # checking whether each n-vector in the input is equal to each n-vector
      # in a Tensor of all possible ngrams. The comparison is batched between
      # the input Tensor of shape [..., 1, length // n, n] and the ngrams Tensor
      # of shape [..., input_dim**n, 1, n].
      ngrams = tf.reshape(
          list(np.ndindex((self.input_dim,) * n)),
          [1] * (len(inputs.shape)-1) + [self.input_dim**n, 1, n])
      is_ngram = tf.equal(
          tf.reduce_sum(tf.cast(tf.equal(reshaped_inputs, ngrams), tf.int32),
                        axis=-1),
          n)
      ngram_counts = tf.reduce_sum(tf.cast(is_ngram, tf.float32), axis=-1)
      ngram_range_counts.append(ngram_counts)
    return tf.concat(ngram_range_counts, axis=-1) 
Example #8
Source File: ngram.py    From tensor2tensor with Apache License 2.0 6 votes vote down vote up
def call(self, inputs):
    batch_shape = tf.shape(inputs)[:-1]
    length = tf.shape(inputs)[-1]
    ngram_range_counts = []
    for n in range(self.minval, self.maxval):
      # Reshape inputs from [..., length] to [..., 1, length // n, n], dropping
      # remainder elements. Each n-vector is an ngram.
      reshaped_inputs = tf.reshape(
          inputs[..., :(n * (length // n))],
          tf.concat([batch_shape, [1], (length // n)[tf.newaxis], [n]], 0))
      # Count the number of times each ngram appears in the input. We do so by
      # checking whether each n-vector in the input is equal to each n-vector
      # in a Tensor of all possible ngrams. The comparison is batched between
      # the input Tensor of shape [..., 1, length // n, n] and the ngrams Tensor
      # of shape [..., input_dim**n, 1, n].
      ngrams = tf.reshape(
          list(np.ndindex((self.input_dim,) * n)),
          [1] * (len(inputs.shape)-1) + [self.input_dim**n, 1, n])
      is_ngram = tf.equal(
          tf.reduce_sum(tf.cast(tf.equal(reshaped_inputs, ngrams), tf.int32),
                        axis=-1),
          n)
      ngram_counts = tf.reduce_sum(tf.cast(is_ngram, tf.float32), axis=-1)
      ngram_range_counts.append(ngram_counts)
    return tf.concat(ngram_range_counts, axis=-1) 
Example #9
Source File: video.py    From OpenCV-Python-Tutorial with MIT License 6 votes vote down vote up
def __init__(self, **kw):
        super(Chess, self).__init__(**kw)

        w, h = self.frame_size

        self.grid_size = sx, sy = 10, 7
        white_quads = []
        black_quads = []
        for i, j in np.ndindex(sy, sx):
            q = [[j, i, 0], [j+1, i, 0], [j+1, i+1, 0], [j, i+1, 0]]
            [white_quads, black_quads][(i + j) % 2].append(q)
        self.white_quads = np.float32(white_quads)
        self.black_quads = np.float32(black_quads)

        fx = 0.9
        self.K = np.float64([[fx*w, 0, 0.5*(w-1)],
                        [0, fx*w, 0.5*(h-1)],
                        [0.0,0.0,      1.0]])

        self.dist_coef = np.float64([-0.2, 0.1, 0, 0])
        self.t = 0 
Example #10
Source File: test_utils.py    From mne-features with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_idxiter():
    n_channels = data.shape[0]
    # Upper-triangular part, including diag
    idx0, idx1 = np.triu_indices(n_channels)
    triu_indices = np.array([np.arange(idx0.size), idx0, idx1])
    triu_indices2 = np.array(list(_idxiter(n_channels, include_diag=True)))
    # Upper-triangular part, without diag
    idx2, idx3 = np.triu_indices(n_channels, 1)
    triu_indices_nodiag = np.array([np.arange(idx2.size), idx2, idx3])
    triu_indices2_nodiag = np.array(list(_idxiter(n_channels,
                                                  include_diag=False)))
    assert_almost_equal(triu_indices, triu_indices2.transpose())
    assert_almost_equal(triu_indices_nodiag, triu_indices2_nodiag.transpose())
    # Upper and lower-triangular parts, without diag
    expected = [(i, j) for _, (i, j) in
                enumerate(np.ndindex((n_channels, n_channels))) if i != j]
    assert_equal(np.array([(i, j) for _, i, j in _idxiter(n_channels,
                                                          triu=False)]),
                 expected) 
Example #11
Source File: _quadratic_hamiltonian_test.py    From OpenFermion with Apache License 2.0 6 votes vote down vote up
def test_add_chemical_potential(self):
        """Test adding a chemical potential."""
        self.quad_ham_npc.add_chemical_potential(2.4)

        combined_hermitian_part = self.quad_ham_npc.combined_hermitian_part
        hermitian_part = self.quad_ham_npc.hermitian_part

        want_combined = (self.combined_hermitian -
                         2.4 * numpy.eye(self.n_qubits))
        want_hermitian = self.hermitian_mat

        for i in numpy.ndindex(combined_hermitian_part.shape):
            self.assertAlmostEqual(combined_hermitian_part[i],
                                   want_combined[i])

        for i in numpy.ndindex(hermitian_part.shape):
            self.assertAlmostEqual(hermitian_part[i], want_hermitian[i])

        self.assertAlmostEqual(2.4 + self.chemical_potential,
                               self.quad_ham_npc.chemical_potential) 
Example #12
Source File: test_lambdify.py    From symengine.py with MIT License 6 votes vote down vote up
def _get_Ndim_args_exprs_funcs(order):
    args = x, y = se.symbols('x y')

    # Higher dimensional inputs
    def f_a(index, _x, _y):
        a, b, c, d = index
        return _x**a + _y**b + (_x+_y)**-d

    nd_exprs_a = np.zeros((3, 5, 1, 4), dtype=object, order=order)
    for index in np.ndindex(*nd_exprs_a.shape):
        nd_exprs_a[index] = f_a(index, x, y)

    def f_b(index, _x, _y):
        a, b, c = index
        return b/(_x + _y)

    nd_exprs_b = np.zeros((1, 7, 1), dtype=object, order=order)
    for index in np.ndindex(*nd_exprs_b.shape):
        nd_exprs_b[index] = f_b(index, x, y)
    return args, nd_exprs_a, nd_exprs_b, f_a, f_b 
Example #13
Source File: test_lambdify.py    From symengine.py with MIT License 6 votes vote down vote up
def test_Lambdify_Ndimensional_order_C():
    args, nd_exprs_a, nd_exprs_b, f_a, f_b = _get_Ndim_args_exprs_funcs(order='C')
    lmb4 = se.Lambdify(args, nd_exprs_a, nd_exprs_b, order='C')
    nargs = len(args)

    inp_extra_shape = (3, 5, 4)
    inp_shape = inp_extra_shape + (nargs,)
    inp4 = np.arange(reduce(mul, inp_shape)*1.0).reshape(inp_shape, order='C')
    out4a, out4b = lmb4(inp4)
    assert out4a.ndim == 7
    assert out4a.shape == inp_extra_shape + nd_exprs_a.shape
    assert out4b.ndim == 6
    assert out4b.shape == inp_extra_shape + nd_exprs_b.shape
    raises(ValueError, lambda: (lmb4(inp4.T)))
    for b, c, d in np.ndindex(inp_extra_shape):
        _x, _y = inp4[b, c, d, :]
        for index in np.ndindex(*nd_exprs_a.shape):
            assert np.isclose(out4a[(b, c, d) + index], f_a(index, _x, _y))
        for index in np.ndindex(*nd_exprs_b.shape):
            assert np.isclose(out4b[(b, c, d) + index], f_b(index, _x, _y)) 
Example #14
Source File: test_lambdify.py    From symengine.py with MIT License 6 votes vote down vote up
def test_Lambdify_Ndimensional_order_F():
    args, nd_exprs_a, nd_exprs_b, f_a, f_b = _get_Ndim_args_exprs_funcs(order='F')
    lmb4 = se.Lambdify(args, nd_exprs_a, nd_exprs_b, order='F')
    nargs = len(args)

    inp_extra_shape = (3, 5, 4)
    inp_shape = (nargs,)+inp_extra_shape
    inp4 = np.arange(reduce(mul, inp_shape)*1.0).reshape(inp_shape, order='F')
    out4a, out4b = lmb4(inp4)
    assert out4a.ndim == 7
    assert out4a.shape == nd_exprs_a.shape + inp_extra_shape
    assert out4b.ndim == 6
    assert out4b.shape == nd_exprs_b.shape + inp_extra_shape
    raises(ValueError, lambda: (lmb4(inp4.T)))
    for b, c, d in np.ndindex(inp_extra_shape):
        _x, _y = inp4[:, b, c, d]
        for index in np.ndindex(*nd_exprs_a.shape):
            assert np.isclose(out4a[index + (b, c, d)], f_a(index, _x, _y))
        for index in np.ndindex(*nd_exprs_b.shape):
            assert np.isclose(out4b[index + (b, c, d)], f_b(index, _x, _y)) 
Example #15
Source File: assign.py    From westpa with MIT License 6 votes vote down vote up
def boundaries(self, boundaries):
        del self._boundaries, self.labels
        self._boundaries = []
        self.labels = labels = []            
        for boundset in boundaries:
            boundarray = numpy.asarray(boundset,dtype=coord_dtype, order='C')
            db = numpy.diff(boundarray)
            if (db <= 0).any():
                raise ValueError('boundary set must be strictly monotonically increasing')
            self._boundaries.append(boundarray)
        self._boundlens = numpy.array([len(boundset) for boundset in self._boundaries], dtype=index_dtype)
        self.ndim = len(self._boundaries)
        self.nbins = numpy.multiply.accumulate([1] + [len(bounds)-1 for bounds in self._boundaries])[-1]

        _boundaries = self._boundaries
        binspace_shape = tuple(self._boundlens[:]-1)
        for index in numpy.ndindex(binspace_shape):
            bounds = [(_boundaries[idim][index[idim]], boundaries[idim][index[idim]+1]) for idim in range(len(_boundaries))]
            labels.append(repr(bounds)) 
Example #16
Source File: image.py    From aetros-cli with MIT License 6 votes vote down vote up
def upscale(image, ratio):
    """
    return upscaled image array
    Arguments:
    image -- a (H,W,C) numpy.ndarray
    ratio -- scaling factor (>1)
    """
    if not isinstance(image, np.ndarray):
        raise ValueError('Expected ndarray')
    if ratio < 1:
        raise ValueError('Ratio must be greater than 1 (ratio=%f)' % ratio)
    width = int(math.floor(image.shape[1] * ratio))
    height = int(math.floor(image.shape[0] * ratio))
    channels = image.shape[2]
    out = np.ndarray((height, width, channels), dtype=np.uint8)
    for x, y in np.ndindex((width, height)):
        out[y, x] = image[int(math.floor(y / ratio)), int(math.floor(x / ratio))]
    return out 
Example #17
Source File: compute_dark_channel.py    From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_dark_channel(I, w=15):
    """Get the dark channel prior in the (RGB) image data.
    Parameters
    -----------
    I:  an M * N * 3 numpy array containing data ([0, 255]) in the image where
        M is the height, N is the width, 3 represents R/G/B channels.
    w:  window size
    Return
    -----------
    An M * N array for the dark channel prior ([0, 255]).
    """
    M, N, _ = I.shape
    padded = np.pad(I, ((w // 2, w // 2), (w // 2, w // 2), (0, 0)), 'edge')
    darkch = np.zeros((M, N), dtype=I.dtype)
    for i, j in np.ndindex(darkch.shape):
        # This is from equation 5 in the above mentioned dark channel paper
        darkch[i, j] = np.min(padded[i:i + w, j:j + w, :])
    return darkch 
Example #18
Source File: test_shape_base.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_with_iterable_object(self):
        # from issue 5248
        d = np.array([
            [set([1, 11]), set([2, 22]), set([3, 33])],
            [set([4, 44]), set([5, 55]), set([6, 66])]
        ])
        actual = np.apply_along_axis(lambda a: set.union(*a), 0, d)
        expected = np.array([{1, 11, 4, 44}, {2, 22, 5, 55}, {3, 33, 6, 66}])

        assert_equal(actual, expected)

        # issue 8642 - assert_equal doesn't detect this!
        for i in np.ndindex(actual.shape):
            assert_equal(type(actual[i]), type(expected[i])) 
Example #19
Source File: test_scale.py    From chainer with MIT License 5 votes vote down vote up
def forward_expected(self, inputs):
        x1, x2 = inputs
        y_expected = numpy.copy(x1)
        for i, j, k in numpy.ndindex(y_expected.shape):
            y_expected[i, j, k] *= x2[j]
        return y_expected, 
Example #20
Source File: test_elu.py    From chainer with MIT License 5 votes vote down vote up
def forward_expected(self, inputs):
        x, = inputs
        expected = x.astype(numpy.float64, copy=True)
        for i in numpy.ndindex(x.shape):
            if x[i] < 0:
                expected[i] = self.alpha * numpy.expm1(expected[i])
        return expected.astype(x.dtype), 
Example #21
Source File: test_bias.py    From chainer with MIT License 5 votes vote down vote up
def setUp(self):
        self.x = numpy.random.uniform(-1, 1, (3, 2, 3)).astype(numpy.float32)
        self.b = numpy.random.uniform(-1, 1, (2)).astype(numpy.float32)
        self.y_expected = numpy.copy(self.x)
        for i, j, k in numpy.ndindex(self.y_expected.shape):
            self.y_expected[i, j, k] += self.b[j]
        self.gy = numpy.random.uniform(-1, 1, (3, 2, 3)).astype(numpy.float32)

        axis = 1
        if self.learn_b:
            self.link = links.Bias(axis, self.b.shape)
            self.link.b.data = self.b
        else:
            self.link = links.Bias(axis, None)
        self.link.cleargrads() 
Example #22
Source File: test_clip.py    From chainer with MIT License 5 votes vote down vote up
def check_forward(self, x_data):
        x_min, x_max = self.x_min_max
        x = chainer.Variable(x_data)
        y = functions.clip(x, x_min, x_max)
        self.assertEqual(y.data.dtype, self.dtype)

        y_expect = self.x.copy()
        for i in numpy.ndindex(self.x.shape):
            if (x_min is not None) and (self.x[i] < x_min):
                y_expect[i] = x_min
            elif (x_max is not None) and (self.x[i] > x_max):
                y_expect[i] = x_max

        testing.assert_allclose(y_expect, y.data) 
Example #23
Source File: test_clip.py    From chainer with MIT License 5 votes vote down vote up
def setUp(self):
        self.x = numpy.random.uniform(-3, 3, self.shape).astype(self.dtype)
        # Avoid values around x_min and x_max for stability of numerical
        # gradient
        x_min, x_max = self.x_min_max
        x_min = float(x_min) if x_min is not None else self.x.min()
        x_max = float(x_max) if x_max is not None else self.x.max()
        eps = 0.01
        for ind in numpy.ndindex(self.x.shape):
            if x_min - eps < self.x[ind] < x_min + eps:
                self.x[ind] = -0.5
            elif x_max - eps < self.x[ind] < x_max + eps:
                self.x[ind] = 0.5
        self.gy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
        self.ggx = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype) 
Example #24
Source File: test_fmod.py    From chainer with MIT License 5 votes vote down vote up
def generate_inputs(self):
        x = numpy.random.uniform(-1.0, 1.0, self.shape).astype(self.dtype)
        divisor = numpy.random.uniform(-1.0, 1.0,
                                       self.shape).astype(self.dtype)
        # division with too small divisor is unstable.
        for i in numpy.ndindex(self.shape):
            if math.fabs(divisor[i]) < 0.1:
                divisor[i] += 1.0
        # make enough margin
        for i in numpy.ndindex(self.shape):
            m = math.fabs(x[i] % divisor[i])
            if m < 0.01 or m > (divisor[i] - 0.01):
                x[i] = 0.5
                divisor[i] = 0.3
        return x, divisor 
Example #25
Source File: test_bias.py    From chainer with MIT License 5 votes vote down vote up
def forward_expected(self, inputs):
        x1, x2 = inputs
        expected = numpy.copy(x1)
        for i, j, k in numpy.ndindex(expected.shape):
            expected[i, j, k] += x2[j]
        expected = utils.force_array(expected)
        return expected, 
Example #26
Source File: test_prelu.py    From chainer with MIT License 5 votes vote down vote up
def check_forward(self, x_data):
        x = chainer.Variable(x_data)
        y = self.link(x)
        self.assertEqual(y.data.dtype, numpy.float32)

        y_expect = self.x.copy()
        for i in numpy.ndindex(self.x.shape):
            if self.x[i] < 0:
                y_expect[i] *= self.W

        testing.assert_allclose(y_expect, y.data) 
Example #27
Source File: test_prelu.py    From chainer with MIT License 5 votes vote down vote up
def setUp(self):
        self.link = links.PReLU()
        W = self.link.W.data
        W[...] = numpy.random.uniform(-1, 1, W.shape)
        self.link.cleargrads()

        self.W = W.copy()  # fixed on CPU

        # Avoid unstability of numerical gradient
        self.x = numpy.random.uniform(-1, 1, (4, 3, 2)).astype(numpy.float32)
        for i in numpy.ndindex(self.x.shape):
            if -0.01 < self.x[i] < 0.01:
                self.x[i] = 0.5
        self.gy = numpy.random.uniform(-1, 1, (4, 3, 2)).astype(numpy.float32) 
Example #28
Source File: test_scale.py    From chainer with MIT License 5 votes vote down vote up
def setUp(self):
        self.x = numpy.random.uniform(-1, 1, (3, 2, 3)).astype(numpy.float32)
        self.W = numpy.random.uniform(-1, 1, (2)).astype(numpy.float32)
        self.b = numpy.random.uniform(-1, 1, (2)).astype(numpy.float32)
        self.y_expected = numpy.copy(self.x)
        for i, j, k in numpy.ndindex(self.y_expected.shape):
            self.y_expected[i, j, k] *= self.W[j]
            if self.bias_term:
                self.y_expected[i, j, k] += self.b[j]
        self.gy = numpy.random.uniform(-1, 1, (3, 2, 3)).astype(numpy.float32)

        bias_term = self.bias_term
        bias_shape = self.bias_shape
        axis = 1
        if self.learn_W:
            self.link = links.Scale(
                axis, self.W.shape, bias_term, bias_shape)
            self.link.W.data = self.W
            if bias_term:
                self.link.bias.b.data = self.b
        else:
            self.link = links.Scale(
                axis, None, bias_term, bias_shape)
            if bias_term:
                self.link.bias.b.data = self.b
        self.link.cleargrads() 
Example #29
Source File: test_embed_id.py    From chainer with MIT License 5 votes vote down vote up
def check_forward(self, x_data):
        x = chainer.Variable(x_data)
        y = self.link(x)
        self.assertEqual(y.data.dtype, numpy.float32)

        y_expect = numpy.empty_like(self.gy)
        for i in numpy.ndindex(self.x.shape):
            if self.x[i] == -1:
                y_expect[i] = 0
            else:
                y_expect[i] = self.W[int(self.x[i])]

        testing.assert_allclose(y_expect, y.data, atol=0, rtol=0) 
Example #30
Source File: test_shape_base.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_with_iterable_object(self):
        # from issue 5248
        d = np.array([
            [{1, 11}, {2, 22}, {3, 33}],
            [{4, 44}, {5, 55}, {6, 66}]
        ])
        actual = np.apply_along_axis(lambda a: set.union(*a), 0, d)
        expected = np.array([{1, 11, 4, 44}, {2, 22, 5, 55}, {3, 33, 6, 66}])

        assert_equal(actual, expected)

        # issue 8642 - assert_equal doesn't detect this!
        for i in np.ndindex(actual.shape):
            assert_equal(type(actual[i]), type(expected[i]))