Пример #1
0
    def __init__(self, tensor, slice_indices):
        View.__init__(self, tensor)
        self._slice = slice_indices
        self._tensor_ndim = len(slice_indices)

        # Compute the slice's dimensionality and shape.
        ndim = 0
        shape = []
        offset = numpy.zeros(self._tensor_ndim)
        interval = numpy.zeros(self._tensor_ndim)
        dim_map = {}
        base_indices = numpy.zeros(self._tensor_ndim)
        for i in xrange(0, self._tensor_ndim):
            index = slice_indices[i]
            if hasattr(index, 'indices'):
                start, end, step = index.indices(tensor.shape[i])
                shape.append(int(ceil(float(end - start) / step)))
                offset[ndim] = start
                interval[ndim] = step
                dim_map[ndim] = i
                ndim += 1
            else:
                base_indices[i] = index

        self.ndim = ndim
        self.shape = tuple(shape)
        # These vars help perform the mapping from slice indices -> tensor indices
        self._offset = offset
        self._interval = interval
        self._dim_map = dim_map
        self._base_indices = base_indices
Пример #2
0
    def __init__(self, tensor, slice_indices):
        View.__init__(self, tensor)
        self._slice = slice_indices
        self._tensor_ndim = len(slice_indices)

        # Compute the slice's dimensionality and shape.
        ndim = 0
        shape = []
        offset = numpy.zeros(self._tensor_ndim)
        interval = numpy.zeros(self._tensor_ndim)
        dim_map = {}
        base_indices = numpy.zeros(self._tensor_ndim)
        for i in xrange(0, self._tensor_ndim):
            index = slice_indices[i]
            if hasattr(index, 'indices'):
                start, end, step = index.indices(tensor.shape[i])
                shape.append(int(ceil(float(end - start)/step)))
                offset[ndim] = start
                interval[ndim] = step
                dim_map[ndim] = i
                ndim += 1
            else:
                base_indices[i] = index

        self.ndim = ndim
        self.shape = tuple(shape)
        # These vars help perform the mapping from slice indices -> tensor indices
        self._offset = offset
        self._interval = interval
        self._dim_map = dim_map
        self._base_indices = base_indices
Пример #3
0
 def __init__(self, tensor, dim):
     View.__init__(self, tensor)
     if dim >= tensor.ndim:
         raise IndexError('Unfolding index (%s) out of bounds (%s dims)' %
                          (dim, tensor.ndim))
     self.dim = dim
     self.otherdims = [x for x in range(self.tensor.ndim) if x != dim]
Пример #4
0
 def __init__(self, tensor, dim):
     View.__init__(self, tensor)
     if dim >= tensor.ndim:
         raise IndexError('Unfolding index (%s) out of bounds (%s dims)' %
                          (dim, tensor.ndim))
     self.dim = dim
     self.otherdims = [x for x in range(self.tensor.ndim) if x != dim]
Пример #5
0
    def __init__(self, tensor, label_lists=None):
        View.__init__(self, tensor)
        shape = tensor.shape
        if label_lists is None:
            label_lists = [None]*self.tensor.ndim

        if tensor.ndim != len(label_lists):
            raise IndexError('Number of label lists (%d) does not match number of dimensions (%s)' % (len(label_lists), tensor.ndim))

        self._labels = [indexable_set(l, shape[mode])
                        for mode, l in enumerate(label_lists)]

        self.tensor.ensure_index_is_valid(tuple(len(label_list)-1 for label_list in self._labels))
Пример #6
0
    def __init__(self, tensor, label_lists=None):
        View.__init__(self, tensor)
        shape = tensor.shape
        if label_lists is None:
            label_lists = [None] * self.tensor.ndim

        if tensor.ndim != len(label_lists):
            raise IndexError(
                "Number of label lists (%d) does not match number of dimensions (%s)" % (len(label_lists), tensor.ndim)
            )

        self._labels = [indexable_set(l, shape[mode]) for mode, l in enumerate(label_lists)]

        self.tensor.ensure_index_is_valid(tuple(len(label_list) - 1 for label_list in self._labels))
Пример #7
0
    def __init__(self, tensor, mode=None):
        '''
        * tensor: tensor to view.
        * mode: which mode to normalize over (0 = rows, 1 = columns, etc.)
        '''
        if len(tensor) == 0:
            raise ValueError('Tensor ' + repr(tensor) + ' is empty.')

        # If this isn't a MeanSubtractedView, the underlying tensor can't be a normalized view.
        if not isinstance(self, MeanSubtractedView) and tensor.stack_contains(BaseNormalizedView):
            raise TypeError('Already normalized; .unnormalized() first.')

        # The underlying tensor must be numeric.
        for part in tensor.example_key():
            if not isinstance(part, (int, long)):
                raise InvalidLayeringException

        View.__init__(self, tensor)
        self.base = 0.0
        self._normalize_mode = mode

        self.refresh_norms()
Пример #8
0
    def __init__(self, tensor, mode=None):
        '''
        * tensor: tensor to view.
        * mode: which mode to normalize over (0 = rows, 1 = columns, etc.)
        '''
        if len(tensor) == 0:
            raise ValueError('Tensor ' + repr(tensor) + ' is empty.')

        # If this isn't a MeanSubtractedView, the underlying tensor can't be a normalized view.
        if not isinstance(self, MeanSubtractedView) and tensor.stack_contains(
                BaseNormalizedView):
            raise TypeError('Already normalized; .unnormalized() first.')

        # The underlying tensor must be numeric.
        for part in tensor.example_key():
            if not isinstance(part, (int, long)):
                raise InvalidLayeringException

        View.__init__(self, tensor)
        self.base = 0.0
        self._normalize_mode = mode

        self.refresh_norms()