Python numpy.broadcast_to() Examples

The following are 30 code examples of numpy.broadcast_to(). 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: utils.py    From tangent with Apache License 2.0 7 votes vote down vote up
def unreduce_array(array, shape, axis, keepdims):
  """Reverse summing over a dimension, NumPy implementation.

  Args:
    array: The array that was reduced.
    shape: The original shape of the array before reduction.
    axis: The axis or axes that were summed.
    keepdims: Whether these axes were kept as singleton axes.

  Returns:
    An array with axes broadcast to match the shape of the original array.
  """
  # NumPy uses a special default value for keepdims, which is equivalent to
  # False.
  if axis is not None and (not keepdims or keepdims is numpy._NoValue):  # pylint: disable=protected-access
    if isinstance(axis, int):
      axis = axis,
    for ax in sorted(axis):
      array = numpy.expand_dims(array, ax)
  return numpy.broadcast_to(array, shape)


# The values are unary functions. 
Example #2
Source File: stride_tricks.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _broadcast_shape(*args):
    """Returns the shape of the arrays that would result from broadcasting the
    supplied arrays against each other.
    """
    if not args:
        return ()
    # use the old-iterator because np.nditer does not handle size 0 arrays
    # consistently
    b = np.broadcast(*args[:32])
    # unfortunately, it cannot handle 32 or more arguments directly
    for pos in range(32, len(args), 31):
        # ironically, np.broadcast does not properly handle np.broadcast
        # objects (it treats them as scalars)
        # use broadcasting to avoid allocating the full array
        b = broadcast_to(0, b.shape)
        b = np.broadcast(b, *args[pos:(pos + 31)])
    return b.shape 
Example #3
Source File: test_action_value.py    From chainerrl with MIT License 6 votes vote down vote up
def test_max_unbounded(self):
        n_batch = 7
        ndim_action = 3
        mu = np.random.randn(n_batch, ndim_action).astype(np.float32)
        mat = np.broadcast_to(
            np.eye(ndim_action, dtype=np.float32)[None],
            (n_batch, ndim_action, ndim_action))
        v = np.random.randn(n_batch).astype(np.float32)
        q_out = action_value.QuadraticActionValue(
            chainer.Variable(mu),
            chainer.Variable(mat),
            chainer.Variable(v))

        v_out = q_out.max
        self.assertIsInstance(v_out, chainer.Variable)
        v_out = v_out.array

        np.testing.assert_almost_equal(v_out, v) 
Example #4
Source File: image_augmentation.py    From youtube-video-face-swap with MIT License 6 votes vote down vote up
def random_warp( image ):
    assert image.shape == (256,256,3)
    range_ = numpy.linspace( 128-80, 128+80, 5 )
    mapx = numpy.broadcast_to( range_, (5,5) )
    mapy = mapx.T

    mapx = mapx + numpy.random.normal( size=(5,5), scale=5 )
    mapy = mapy + numpy.random.normal( size=(5,5), scale=5 )

    interp_mapx = cv2.resize( mapx, (80,80) )[8:72,8:72].astype('float32')
    interp_mapy = cv2.resize( mapy, (80,80) )[8:72,8:72].astype('float32')

    warped_image = cv2.remap( image, interp_mapx, interp_mapy, cv2.INTER_LINEAR )

    src_points = numpy.stack( [ mapx.ravel(), mapy.ravel() ], axis=-1 )
    dst_points = numpy.mgrid[0:65:16,0:65:16].T.reshape(-1,2)
    mat = umeyama( src_points, dst_points, True )[0:2]

    target_image = cv2.warpAffine( image, mat, (64,64) )

    return warped_image, target_image 
Example #5
Source File: functional.py    From opencv_transforms with MIT License 6 votes vote down vote up
def to_grayscale(img, num_output_channels=1):
    """Convert image to grayscale version of image.
    Args:
        img (numpy ndarray): Image to be converted to grayscale.
    Returns:
        numpy ndarray: Grayscale version of the image.
            if num_output_channels = 1 : returned image is single channel
            if num_output_channels = 3 : returned image is 3 channel with r = g = b
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be numpy ndarray. Got {}'.format(type(img)))

    if num_output_channels==1:
        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis]
    elif num_output_channels==3:
        # much faster than doing cvtColor to go back to gray
        img = np.broadcast_to(cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis], img.shape) 
    return img 
Example #6
Source File: test_action_value.py    From chainerrl with MIT License 6 votes vote down vote up
def test_getitem(self):
        n_batch = 7
        ndim_action = 3
        mu = np.random.randn(n_batch, ndim_action).astype(np.float32)
        mat = np.broadcast_to(
            np.eye(ndim_action, dtype=np.float32)[None],
            (n_batch, ndim_action, ndim_action))
        v = np.random.randn(n_batch).astype(np.float32)
        min_action, max_action = -1, 1
        qout = action_value.QuadraticActionValue(
            chainer.Variable(mu),
            chainer.Variable(mat),
            chainer.Variable(v),
            min_action,
            max_action,
        )
        sliced = qout[:3]
        np.testing.assert_equal(sliced.mu.array, mu[:3])
        np.testing.assert_equal(sliced.mat.array, mat[:3])
        np.testing.assert_equal(sliced.v.array, v[:3])
        np.testing.assert_equal(sliced.min_action, min_action)
        np.testing.assert_equal(sliced.max_action, max_action) 
Example #7
Source File: ndarray.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def _prepare_value_nd(self, value, vshape):
        """Given value and vshape, create an `NDArray` from value with the same
        context and dtype as the current one and broadcast it to vshape."""
        if isinstance(value, numeric_types):
            value_nd = full(shape=vshape, val=value, ctx=self.context, dtype=self.dtype)
        elif isinstance(value, NDArray):
            value_nd = value.as_in_context(self.context)
            if value_nd.dtype != self.dtype:
                value_nd = value_nd.astype(self.dtype)
        else:
            try:
                value_nd = array(value, ctx=self.context, dtype=self.dtype)
            except:
                raise TypeError('NDArray does not support assignment with non-array-like'
                                ' object %s of type %s' % (str(value), str(type(value))))
        if value_nd.shape != vshape:
            value_nd = value_nd.broadcast_to(vshape)
        return value_nd 
Example #8
Source File: utils.py    From BrainSpace with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _dominant_set_sparse(s, k, is_thresh=False, norm=False):
    """Compute dominant set for a sparse matrix."""
    if is_thresh:
        mask = s > k
        idx, data = np.where(mask), s[mask]
        s = ssp.coo_matrix((data, idx), shape=s.shape)

    else:  # keep top k
        nr, nc = s.shape
        idx = np.argpartition(s, nc - k, axis=1)
        col = idx[:, -k:].ravel()  # idx largest
        row = np.broadcast_to(np.arange(nr)[:, None], (nr, k)).ravel()
        data = s[row, col].ravel()
        s = ssp.coo_matrix((data, (row, col)), shape=s.shape)

    if norm:
        s.data /= s.sum(axis=1).A1[s.row]

    return s.tocsr(copy=False) 
Example #9
Source File: opencv_functional.py    From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def to_grayscale(img, num_output_channels=1):
    """Convert image to grayscale version of image.
    Args:
        img (numpy ndarray): Image to be converted to grayscale.
    Returns:
        numpy ndarray: Grayscale version of the image.
            if num_output_channels = 1 : returned image is single channel
            if num_output_channels = 3 : returned image is 3 channel with r = g = b
    """
    if not _is_numpy_image(img):
        raise TypeError('img should be numpy ndarray. Got {}'.format(type(img)))

    if num_output_channels==1:
        img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis]
    elif num_output_channels==3:
        # much faster than doing cvtColor to go back to gray
        img = np.broadcast_to(cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)[:,:,np.newaxis], img.shape)
    return img 
Example #10
Source File: utils.py    From knmt with GNU General Public License v3.0 6 votes vote down vote up
def make_batch_mask(mb_size, n_head, max_length_1, max_length_2, 
                    key_seq_lengths=None,
                    future_mask=False,
                    mask_value=-10000):
    
    if future_mask:
        assert max_length_1 == max_length_2
        mask = np.array(
                np.broadcast_to(( (-mask_value) * (np.tri(max_length_1, dtype = np.float32)-1))[None,None,:,:], 
                                (mb_size, n_head, max_length_1, max_length_2))
                )
    else:
        mask = np.zeros((mb_size, n_head, max_length_1, max_length_2), dtype = np.float32)
        
    if key_seq_lengths is not None:
        assert mb_size == len(key_seq_lengths)
        assert min(key_seq_lengths) > 0
        assert max(key_seq_lengths) <= max_length_2
        for num_batch, length in enumerate(key_seq_lengths):
            mask[num_batch, :, :, length:] = mask_value

    return mask 
Example #11
Source File: builder_lib_test.py    From lingvo with Apache License 2.0 6 votes vote down vote up
def testBroadcastConcat(self):
    b = builder_lib.ModelBuilderBase()
    p = b._BroadcastConcat('p', b._ArgIdx('arg0', [0]), b._ArgIdx('arg1', [1]))
    l = p.Instantiate()

    # 2 x 1 x 3
    x1 = np.asarray([[[1, 2, 3]], [[4, 5, 6]]])
    self.assertEqual(x1.shape, (2, 1, 3))
    # 1 x 4 x 2
    x2 = np.asarray([[[7, 8], [9, 10], [11, 12], [13, 14]]])
    self.assertEqual(x2.shape, (1, 4, 2))

    y = l.FPropDefaultTheta(tf.constant(x1), tf.constant(x2))

    expected_y = np.concatenate([
        np.broadcast_to(x1, [2, 4, 3]),
        np.broadcast_to(x2, [2, 4, 2]),
    ], axis=-1)  # pyformat: disable
    with self.session():
      actual_y = self.evaluate(y)
      # x1 will be broadcasted to [2, 4, 3]
      # x2 will be broadcasted to [2, 4, 2]
      # Concatenation on axis=-1 should result in a tensor of shape [2, 4, 5]
      self.assertEqual(actual_y.shape, (2, 4, 5))
      self.assertAllEqual(actual_y, expected_y) 
Example #12
Source File: stride_tricks.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def _broadcast_shape(*args):
    """Returns the shape of the arrays that would result from broadcasting the
    supplied arrays against each other.
    """
    if not args:
        return ()
    # use the old-iterator because np.nditer does not handle size 0 arrays
    # consistently
    b = np.broadcast(*args[:32])
    # unfortunately, it cannot handle 32 or more arguments directly
    for pos in range(32, len(args), 31):
        # ironically, np.broadcast does not properly handle np.broadcast
        # objects (it treats them as scalars)
        # use broadcasting to avoid allocating the full array
        b = broadcast_to(0, b.shape)
        b = np.broadcast(b, *args[pos:(pos + 31)])
    return b.shape 
Example #13
Source File: stride_tricks.py    From lambda-packs with MIT License 6 votes vote down vote up
def _broadcast_shape(*args):
    """Returns the shape of the arrays that would result from broadcasting the
    supplied arrays against each other.
    """
    if not args:
        return ()
    # use the old-iterator because np.nditer does not handle size 0 arrays
    # consistently
    b = np.broadcast(*args[:32])
    # unfortunately, it cannot handle 32 or more arguments directly
    for pos in range(32, len(args), 31):
        # ironically, np.broadcast does not properly handle np.broadcast
        # objects (it treats them as scalars)
        # use broadcasting to avoid allocating the full array
        b = broadcast_to(0, b.shape)
        b = np.broadcast(b, *args[pos:(pos + 31)])
    return b.shape 
Example #14
Source File: helper_utils.py    From safelife with Apache License 2.0 6 votes vote down vote up
def __getitem__(self, items):
        # To make life easier, we only wrap when `items` is a tuple of slices.
        # It'd be nifty if they could be integers or work on arbitrary arrays,
        # but that's more work and it won't be used.
        if not (isinstance(items, tuple) and len(items) == 2 and
                isinstance(items[0], slice) and isinstance(items[1], slice)):
            return super().__getitem__(items)
        rows, cols = items
        rows = np.arange(
            rows.start or 0,
            rows.stop if rows.stop is not None else self.shape[0],
            rows.step or 1
        ) % self.shape[0]
        cols = np.arange(
            cols.start or 0,
            cols.stop if cols.stop is not None else self.shape[0],
            cols.step or 1
        ) % self.shape[1]
        nrows = len(rows)
        ncols = len(cols)
        rows = np.broadcast_to(rows[:,None], (nrows, ncols))
        cols = np.broadcast_to(cols[None,:], (nrows, ncols))
        return super().__getitem__((rows, cols)) 
Example #15
Source File: normalizer.py    From tf2rl with MIT License 6 votes vote down vote up
def __call__(self, x, update=True):
        """Normalize mean and variance of values based on emprical values.
        Args:
            x (ndarray or Variable): Input values
            update (bool): Flag to learn the input values
        Returns:
            ndarray or Variable: Normalized output values
        """
        if self.count == 0:
            return x

        mean = np.broadcast_to(self._mean, x.shape)
        std_inv = np.broadcast_to(self._std_inverse, x.shape)

        if update:
            self.experience(x)

        normalized = (x - mean) * std_inv
        if self.clip_threshold is not None:
            normalized = np.clip(
                normalized, -self.clip_threshold, self.clip_threshold)
        return normalized 
Example #16
Source File: stride_tricks.py    From auto-alt-text-lambda-api with MIT License 6 votes vote down vote up
def _broadcast_shape(*args):
    """Returns the shape of the ararys that would result from broadcasting the
    supplied arrays against each other.
    """
    if not args:
        raise ValueError('must provide at least one argument')
    # use the old-iterator because np.nditer does not handle size 0 arrays
    # consistently
    b = np.broadcast(*args[:32])
    # unfortunately, it cannot handle 32 or more arguments directly
    for pos in range(32, len(args), 31):
        # ironically, np.broadcast does not properly handle np.broadcast
        # objects (it treats them as scalars)
        # use broadcasting to avoid allocating the full array
        b = broadcast_to(0, b.shape)
        b = np.broadcast(b, *args[pos:(pos + 31)])
    return b.shape 
Example #17
Source File: render_text.py    From safelife with Apache License 2.0 6 votes vote down vote up
def render_board(board, goals=0, orientation=0, edit_loc=None, edit_color=0):
    """
    Just render the board itself. Doesn't require game state.
    """
    if edit_loc and (edit_loc[0] >= board.shape[0] or edit_loc[1] >= board.shape[1]):
        edit_loc = None
    goals = np.broadcast_to(goals, board.shape)

    screen = np.empty((board.shape[0]+2, board.shape[1]+3,), dtype=object)
    screen[:] = ''
    screen[0] = screen[-1] = ' -'
    screen[:,0] = screen[:,-2] = ' |'
    screen[:,-1] = '\n'
    screen[0,0] = screen[0,-2] = screen[-1,0] = screen[-1,-2] = ' +'
    screen[1:-1,1:-2] = render_cell(board, goals, orientation)

    if edit_loc:
        x1, y1 = edit_loc
        val = render_cell(board[y1, x1], goals[y1, x1], orientation, edit_color)
        screen[y1+1, x1+1] = str(val)
    return ''.join(screen.ravel()) 
Example #18
Source File: dsl.py    From rasa_wechat with Apache License 2.0 5 votes vote down vote up
def _deduplicate_training_data(X, y):
        # type: (ndarray, ndarray) -> Tuple[ndarray, ndarray]
        """Make sure every training example in X occurs exactly once."""

        # we need to concat X and y to make sure that
        # we do NOT throw out contradicting examples
        # (same featurization but different labels).
        # appends y to X so it appears to be just another feature
        casted_y = np.broadcast_to(
                np.reshape(y, (y.shape[0], 1, 1)), (y.shape[0], X.shape[1], 1))
        concatenated = np.concatenate((X, casted_y), axis=2)
        t_data = np.unique(concatenated, axis=0)
        X_unique = t_data[:, :, :-1]
        y_unique = np.array(t_data[:, 0, -1], dtype=casted_y.dtype)
        return X_unique, y_unique 
Example #19
Source File: gym_utils.py    From tensor2tensor with Apache License 2.0 5 votes vote down vote up
def _discretize_env(self, env):
    """Generates a discrete bounded spec and a linspace for the given limits.

    Args:
      env: An array to discretize.

    Returns:
      Tuple with the discrete_spec along with a list of lists mapping actions.
    Raises:
      ValueError: If not all limits value are >=2 or maximum or minimum of boxes
      is equal to +- infinity.
    """
    if not np.all(self._num_actions >= 2):
      raise ValueError("num_actions should all be at least size 2.")

    if (math.isinf(np.min(env.action_space.low)) or
        math.isinf(np.max(env.action_space.high))):
      raise ValueError(
          """Minimum of boxes is {} and maximum of boxes is {},
          but we expect that finite values are provided.""".
          format(np.min(env.action_space.low),
                 np.max(env.action_space.high)))

    limits = np.broadcast_to(self._num_actions,
                             env.action_space.shape)
    minimum = np.broadcast_to(np.min(env.action_space.low),
                              env.action_space.shape)
    maximum = np.broadcast_to(np.max(env.action_space.high),
                              env.action_space.shape)

    action_map = [
        np.linspace(env_min, env_max, num=n_actions)
        for env_min, env_max, n_actions in zip(
            np.nditer(minimum), np.nditer(maximum), np.nditer(limits))
    ]

    return action_map 
Example #20
Source File: raster_manipulation.py    From pyeo with GNU General Public License v3.0 5 votes vote down vote up
def apply_array_image_mask(array, mask, fill_value=0):
    """Applies a mask of (y,x) to an image array of (bands, y, x). Replaces any masked pixels with fill_value
    Mask is an a 2 dimensional array of 1 ( unmasked) and 0 (masked)"""
    stacked_mask = np.broadcast_to(mask, array.shape)
    return np.where(stacked_mask == 1, array, fill_value) 
Example #21
Source File: stride_tricks.py    From lambda-packs with MIT License 5 votes vote down vote up
def broadcast_to(array, shape, subok=False):
    """Broadcast an array to a new shape.

    Parameters
    ----------
    array : array_like
        The array to broadcast.
    shape : tuple
        The shape of the desired array.
    subok : bool, optional
        If True, then sub-classes will be passed-through, otherwise
        the returned array will be forced to be a base-class array (default).

    Returns
    -------
    broadcast : array
        A readonly view on the original array with the given shape. It is
        typically not contiguous. Furthermore, more than one element of a
        broadcasted array may refer to a single memory location.

    Raises
    ------
    ValueError
        If the array is not compatible with the new shape according to NumPy's
        broadcasting rules.

    Notes
    -----
    .. versionadded:: 1.10.0

    Examples
    --------
    >>> x = np.array([1, 2, 3])
    >>> np.broadcast_to(x, (3, 3))
    array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]])
    """
    return _broadcast_to(array, shape, subok=subok, readonly=True) 
Example #22
Source File: _numpy_compat.py    From lambda-packs with MIT License 5 votes vote down vote up
def broadcast_to(array, shape, subok=False):
        return _broadcast_to(array, shape, subok=subok, readonly=True) 
Example #23
Source File: stride_tricks.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def broadcast_to(array, shape, subok=False):
    """Broadcast an array to a new shape.

    Parameters
    ----------
    array : array_like
        The array to broadcast.
    shape : tuple
        The shape of the desired array.
    subok : bool, optional
        If True, then sub-classes will be passed-through, otherwise
        the returned array will be forced to be a base-class array (default).

    Returns
    -------
    broadcast : array
        A readonly view on the original array with the given shape. It is
        typically not contiguous. Furthermore, more than one element of a
        broadcasted array may refer to a single memory location.

    Raises
    ------
    ValueError
        If the array is not compatible with the new shape according to NumPy's
        broadcasting rules.

    Notes
    -----
    .. versionadded:: 1.10.0

    Examples
    --------
    >>> x = np.array([1, 2, 3])
    >>> np.broadcast_to(x, (3, 3))
    array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]])
    """
    return _broadcast_to(array, shape, subok=subok, readonly=True) 
Example #24
Source File: stride_tricks.py    From lambda-packs with MIT License 5 votes vote down vote up
def broadcast_to(array, shape, subok=False):
    """Broadcast an array to a new shape.

    Parameters
    ----------
    array : array_like
        The array to broadcast.
    shape : tuple
        The shape of the desired array.
    subok : bool, optional
        If True, then sub-classes will be passed-through, otherwise
        the returned array will be forced to be a base-class array (default).

    Returns
    -------
    broadcast : array
        A readonly view on the original array with the given shape. It is
        typically not contiguous. Furthermore, more than one element of a
        broadcasted array may refer to a single memory location.

    Raises
    ------
    ValueError
        If the array is not compatible with the new shape according to NumPy's
        broadcasting rules.

    Notes
    -----
    .. versionadded:: 1.10.0

    Examples
    --------
    >>> x = np.array([1, 2, 3])
    >>> np.broadcast_to(x, (3, 3))
    array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]])
    """
    return _broadcast_to(array, shape, subok=subok, readonly=True) 
Example #25
Source File: test_action_value.py    From chainerrl with MIT License 5 votes vote down vote up
def test_max_bounded(self):
        n_batch = 20
        ndim_action = 3
        mu = np.random.randn(n_batch, ndim_action).astype(np.float32)
        mat = np.broadcast_to(
            np.eye(ndim_action, dtype=np.float32)[None],
            (n_batch, ndim_action, ndim_action))
        v = np.random.randn(n_batch).astype(np.float32)
        min_action, max_action = -1.3, 1.3
        q_out = action_value.QuadraticActionValue(
            chainer.Variable(mu),
            chainer.Variable(mat),
            chainer.Variable(v),
            min_action, max_action)

        v_out = q_out.max
        self.assertIsInstance(v_out, chainer.Variable)
        v_out = v_out.array

        # If mu[i] is an valid action, v_out[i] should be v[i]
        mu_is_allowed = np.all(
            (min_action < mu) * (mu < max_action),
            axis=1)
        np.testing.assert_almost_equal(v_out[mu_is_allowed], v[mu_is_allowed])

        # Otherwise, v_out[i] should be less than v[i]
        mu_is_not_allowed = ~np.all(
            (min_action - 1e-2 < mu) * (mu < max_action + 1e-2),
            axis=1)
        np.testing.assert_array_less(
            v_out[mu_is_not_allowed],
            v[mu_is_not_allowed]) 
Example #26
Source File: test_indexing.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_indexing_array_weird_strides(self):
        # See also gh-6221
        # the shapes used here come from the issue and create the correct
        # size for the iterator buffering size.
        x = np.ones(10)
        x2 = np.ones((10, 2))
        ind = np.arange(10)[:, None, None, None]
        ind = np.broadcast_to(ind, (10, 55, 4, 4))

        # single advanced index case
        assert_array_equal(x[ind], x[ind.copy()])
        # higher dimensional advanced index
        zind = np.zeros(4, dtype=np.intp)
        assert_array_equal(x2[ind, zind], x2[ind.copy(), zind]) 
Example #27
Source File: batch_major_attention_test.py    From lingvo with Apache License 2.0 5 votes vote down vote up
def AttenProbs(self, key, query, paddings):
    assert query.ndim == 4
    assert paddings.ndim == 2
    assert key.shape == query.shape

    batch, seqlen = query.shape[:2]
    tgtlen, srclen = seqlen, seqlen
    assert query.shape[2] == self._num_heads
    assert query.shape[3] == self._atten_dim
    assert paddings.shape == query.shape[:2]

    # [B, N, T, T]
    logits = np.zeros((batch, self._num_heads, tgtlen, srclen))
    # [B, N, T, T]
    probs = np.zeros((batch, self._num_heads, tgtlen, srclen))

    paddings = np.broadcast_to(
        np.reshape(paddings, (batch, 1, 1, seqlen)),
        (batch, self._num_heads, seqlen, seqlen))

    def Normalize(vec):
      expx = np.exp(vec)
      expxsum = np.sum(expx, axis=-1)
      return expx / expxsum

    for b in range(batch):
      for h in range(self._num_heads):
        for i in range(tgtlen):
          for j in range(srclen):
            logits[b][h][i][j] = np.dot(query[b][i][h],
                                        key[b][j][h] + self.GetKeyEmb(i, j, h))
          logits[b][h][i] = np.where(paddings[b][h][i] > 0,
                                     np.finfo(np.float32).max * (-0.7),
                                     logits[b][h][i])
          probs[b][h][i] = Normalize(logits[b][h][i])
    return probs 
Example #28
Source File: agent.py    From cwcf with MIT License 5 votes vote down vote up
def step(self):
        s = self.S[all_agents, self.idx]
        na = self.NA[all_agents, self.idx]

        a, u = self.act(s, na)
        s_, r, na_, done, info = self.env.step(a)

        self.A[all_agents, self.idx] = a
        self.R[all_agents, self.idx] = r
        self.U[all_agents, self.idx] = u

        for i in np.where(done)[0]:     # truncate & store the finished episode i
            idx = self.idx[i]+1

            _s = self.S[i, :idx].copy()
            _a = self.A[i, :idx].copy()
            _r = self.R[i, :idx].copy()
            _u = self.U[i, :idx].copy()
            _na = self.NA[i, :idx].copy()

            # extract the true state
            _x = np.broadcast_to(self.env.x[i].copy(), (idx, config.FEATURE_DIM))
            _y = np.repeat(self.env.y[i], idx)

            self.pool.put( (_s, _a, _r, _u, _na, _x, _y) )

        self.idx = (done == 0) * (self.idx + 1)     # advance idx by 1 and reset to 0 for finished episodes

        self.NA[all_agents, self.idx] = na_     # unavailable actions
        self.S[all_agents,  self.idx] = s_

        return s, a, r, s_, done, info 
Example #29
Source File: dataset.py    From chainer-PGGAN with MIT License 5 votes vote down vote up
def get_example(self, i):
        img = Image.open(os.path.join(self.directory, self.files[i]))
        img = img.crop(self.random_box(img.size, 16))

        size = 2**(2+self.depth)
        img = img.resize((size, size))
        
        img = np.array(img, dtype=np.float32) / 256
        if len(img.shape) == 2:
            img = np.broadcast_to(img, (3, img.shape[0], img.shape[1]))
        else:
            img = np.transpose(img, (2, 0, 1))
        
        return img 
Example #30
Source File: test_ops_convpool.py    From ngraph-onnx with Apache License 2.0 5 votes vote down vote up
def test_pool_global_average_3d(ndarray_1x1x4x4):
    x = np.broadcast_to(ndarray_1x1x4x4, (1, 1, 4, 4, 4))

    node = onnx.helper.make_node('GlobalAveragePool', inputs=['x'], outputs=['y'])
    y = np.array([18.5], dtype=np.float32).reshape(1, 1, 1, 1, 1)
    ng_results = run_node(node, [x])
    assert np.array_equal(ng_results, [y])