Example #1
0
def expand_dims(input, axis, name=None):
    """
    Inserts a dimension of 1 into a tensor's shape.

    Given a tensor input, this operation inserts a dimension \
    of 1 at the dimension index axis of input's shape. \
    The dimension index axis starts at zero; \
    if you specify a negative number for axis it is counted backward from the end.

    Args:
        input: A Tensor.
    """
    shape = input.shape
    rank = bt.get_rank(shape)
    axis = util.to_axis(axis, rank)[0]

    new_shape = []
    new_shape.extend(shape[:axis])
    new_shape.append(1)
    new_shape.extend(shape[axis:])

    if axis == rank:
        return _lazy_reshape(input, new_shape)

    return bt._View(input, shape=new_shape, name=name)
Example #2
0
def expand_dims(input, axis, name=None):
    shape = input.shape
    rank = len(shape)
    axis = util.to_axis(axis, rank)[0]
    new_shape = list(shape[:axis]) + [1] + list(shape[axis:])

    return input.reshape(new_shape)
Example #3
0
    def __init__(self, input_tensor,
                 axis=None, keep_dims=False, dtype=None, name=None, par=1):

        rank = bt.get_rank(input_tensor.shape)
        axis = util.to_axis(axis, rank)
        shape = util.to_reduce_shape(input_tensor.shape, axis, keep_dims)

        bt._ReductionOperator.__init__(self, input_tensor,
                                       dtype=dtype, shape=shape, name=name,
                                       axis=axis, keep_dims=keep_dims, par=par)
Example #4
0
    def __init__(self, values, axis, dtype=None, name=None):
        rank = bt.get_rank(values[0].shape)
        _dtype = values[0].dtype

        for value in values:
            r = bt.get_rank(value.shape)
            if r != rank:
                raise ValueError('all values must have a same rank: %d != %d' %
                                 (r, rank))
            rank = r
            d = value.dtype
            if d != _dtype:
                raise ValueError(
                    'all values must have a same dtype: %s != %s' %
                    (d, _dtype))
            _dtype = d

        if isinstance(axis, (tuple, list)):
            raise TypeError('axis must be int, not tuple or list.')

        axis = util.to_axis(axis, rank)[0]

        shape = []
        for i in range(rank):
            size = 0
            for value in values:
                if i == axis:
                    size += value.shape[i]
                else:
                    if size != 0 and size != value.shape[i]:
                        raise ValueError(
                            'all values must have a same shape, excluding axis: %d != %d'
                            % (size, value.shape[i]))
                    size = max(size, value.shape[i])

            shape.append(size)

        shape = tuple(shape)

        bt._Operator.__init__(self,
                              *values,
                              dtype=dtype,
                              shape=shape,
                              name=name)
        self.axis = axis