Ejemplo n.º 1
0
    def _format_as_impl(self, is_numeric, batch, space):
        if isinstance(space, VectorSequenceSpace):
            if self.max_labels == space.dim:
                mode = 'merge'
            elif self.dim * self.max_labels == space.dim:
                mode = 'concatenate'
            else:
                raise ValueError("There is a bug. Couldn't format to a "
                                 "VectorSequenceSpace because it had an "
                                 "incorrect size, but this should've been "
                                 "caught in "
                                 "IndexSequenceSpace._check_sizes().")

            format_func = (self.formatter.format
                           if is_numeric else self.formatter.theano_expr)
            return _cast(format_func(batch, mode=mode), space.dtype)
        elif isinstance(space, IndexSequenceSpace):
            if space.dim != self.dim or space.max_labels != self.max_labels:
                raise ValueError("The two IndexSequenceSpaces' dim and "
                                 "max_labels values don't match. This should "
                                 "have been caught by "
                                 "IndexSequenceSpace._check_sizes().")

            return _cast(batch, space.dtype)
        else:
            raise ValueError("Can't convert %s to %s" % (self, space))
Ejemplo n.º 2
0
    def _format_as_impl(self, is_numeric, batch, space):
        if isinstance(space, VectorSequenceSpace):
            if self.max_labels == space.window_dim:
                mode = 'merge'
            elif self.window_dim * self.max_labels == space.window_dim:
                mode = 'concatenate'
            else:
                raise ValueError("There is a bug. Couldn't format to a "
                                 "VectorSequenceSpace because it had an "
                                 "incorrect size, but this should've been "
                                 "caught in "
                                 "IndexSequenceSpace._check_sizes().")

            format_func = (self.formatter.format if is_numeric else
                           self.formatter.theano_expr)
            return _cast(format_func(batch, mode=mode), space.dtype)
        elif isinstance(space, IndexSequenceSpace):
            if space.window_dim != self.window_dim or space.max_labels != self.max_labels:
                raise ValueError("The two IndexSequenceSpaces' window_dim and "
                                 "max_labels values don't match. This should "
                                 "have been caught by "
                                 "IndexSequenceSpace._check_sizes().")

            return _cast(batch, space.dtype)
        else:
            raise ValueError("Can't convert %s to %s"
                             % (self, space))
Ejemplo n.º 3
0
    def _format_as_impl(self, is_numeric, batch, space):
        if space == self:
            return batch
        else:
            if isinstance(space, SequenceDataSpace):
                if is_numeric:
                    formatted_batch = np.transpose(np.asarray([
                        self.space._format_as_impl(is_numeric, sample,
                                                   space.space)
                        for sample in np.transpose(batch, (1, 0, 2))
                    ]), (1, 0, 2))
                else:
                    formatted_batch, _ = scan(
                        fn=lambda elem: self.space._format_as_impl(
                            is_numeric, elem, space.space),
                        sequences=[batch]
                    )
                return formatted_batch
            elif isinstance(space, space.VectorSpace):
                row = batch.shape[0] * batch.shape[1]
                col = self.dim
                result = tensor.reshape(batch,
                                        newshape=[row, col],
                                        ndim=2)
                return space._cast(result, space.dtype)
            elif isinstance(space, space.Conv2DSpace):
                result = batch.dimshuffle(1, 0, 'x', 2)
                # Newly added part
                b01c_shape = [result.shape[0],
                              space.shape[0],
                              space.shape[1],
                              space.num_channels]
                result = result.flatten()
                result = tensor.reshape(result,
                                        newshape=b01c_shape,
                                        ndim=4)
                # end of newly added part

                return space._cast(result, space.dtype)
            else:
                print('Unexpected space', space)
                raise NotImplementedError
Ejemplo n.º 4
0
    def _format_as_impl(self, is_numeric, batch, space):
        if space == self:
            return batch
        else:
            if isinstance(space, SequenceDataSpace):
                if is_numeric:
                    formatted_batch = np.transpose(np.asarray([
                        self.space._format_as_impl(is_numeric, sample,
                                                   space.space)
                        for sample in np.transpose(batch, (1, 0, 2))
                    ]), (1, 0, 2))
                else:
                    formatted_batch, _ = scan(
                        fn=lambda elem: self.space._format_as_impl(
                            is_numeric, elem, space.space),
                        sequences=[batch]
                    )
                return formatted_batch
            elif isinstance(space, space.VectorSpace):
                row = batch.shape[0] * batch.shape[1]
                col = self.dim
                result = tensor.reshape(batch,
                                        newshape=[row, col],
                                        ndim=2)
                return space._cast(result, space.dtype)
            elif isinstance(space, space.Conv2DSpace):
                result = batch.dimshuffle(1, 0, 'x', 2)
                # Newly added part
                b01c_shape = [result.shape[0],
                              space.shape[0],
                              space.shape[1],
                              space.num_channels]
                result = result.flatten()
                result = tensor.reshape(result,
                                        newshape=b01c_shape,
                                        ndim=4)
                # end of newly added part

                return space._cast(result, space.dtype)
            else:
                print('Unexpected space', space)
                raise NotImplementedError
Ejemplo n.º 5
0
    def _format_as_impl(self, is_numeric, batch, space):
        if isinstance(space, VectorSequenceSpace):
            if space.window_dim != self.window_dim:
                raise ValueError("The two VectorSequenceSpaces' window_dim "
                                 "values don't match. This should have been "
                                 "caught by "
                                 "VectorSequenceSpace._check_sizes().")

            return _cast(batch, space.dtype)
        else:
            raise ValueError("Can't convert %s to %s" % (self, space))
Ejemplo n.º 6
0
    def _format_as_impl(self, is_numeric, batch, space):
        if isinstance(space, VectorSequenceSpace):
            if space.dim != self.dim:
                raise ValueError("The two VectorSequenceSpaces' dim "
                                 "values don't match. This should have been "
                                 "caught by "
                                 "VectorSequenceSpace._check_sizes().")

            return _cast(batch, space.dtype)
        else:
            raise ValueError("Can't convert %s to %s" % (self, space))
Ejemplo n.º 7
0
    def _format_as_impl(self, is_numeric, batch, space):
        if isinstance(space, VectorSpace):
            # We need to ensure that the resulting batch will always be
            # the same in `space`, no matter what the axes of `self` are.
            if self.axes != self.default_axes:
                # The batch index goes on the first axis
                assert self.default_axes[0] == "b"
                batch = batch.transpose(*[self.axes.index(axis) for axis in self.default_axes])
            result = batch.reshape((batch.shape[0], self.get_total_dimension()))
            if space.sparse:
                result = _dense_to_sparse(result)

        elif isinstance(space, Conv3DSpace):
            result = Conv3DSpace.convert(batch, self.axes, space.axes)
        else:
            raise NotImplementedError("%s doesn't know how to format as %s" % (str(self), str(space)))

        return _cast(result, space.dtype)
Ejemplo n.º 8
0
    def _format_as_impl(self, is_numeric, batch, space):
        if isinstance(space, VectorSpace):
            # We need to ensure that the resulting batch will always be
            # the same in `space`, no matter what the axes of `self` are.
            if self.axes != self.default_axes:
                # The batch index goes on the first axis
                assert self.default_axes[0] == 'b'
                batch = batch.transpose(
                    *[self.axes.index(axis) for axis in self.default_axes])
            result = batch.reshape(
                (batch.shape[0], self.get_total_dimension()))
            if space.sparse:
                result = _dense_to_sparse(result)

        elif isinstance(space, Conv3DSpace):
            result = Conv3DSpace.convert(batch, self.axes, space.axes)
        else:
            raise NotImplementedError("%s doesn't know how to format as %s" %
                                      (str(self), str(space)))

        return _cast(result, space.dtype)