コード例 #1
0
ファイル: yaml_parse.py プロジェクト: Alienfeel/pylearn2
 def instantiate(self):
     """
     Instantiate this object with the supplied parameters in `self.kwds`,
     or if already instantiated, return the cached instance.
     """
     if self.instance is None:
         self.instance = checked_call(self.cls, self.kwds)
     #endif
     try:
         self.instance.yaml_src = self.yaml_src
     except AttributeError:
         pass
     return self.instance
コード例 #2
0
ファイル: yaml_parse.py プロジェクト: vibster/pylearn2
 def instantiate(self):
     """
     Instantiate this object with the supplied parameters in `self.kwds`,
     or if already instantiated, return the cached instance.
     """
     if self.instance is None:
         self.instance = checked_call(self.cls, self.kwds)
     #endif
     try:
         self.instance.yaml_src = self.yaml_src
     except AttributeError:
         pass
     return self.instance
コード例 #3
0
    def instantiate(self):
        """
        .. warning::

            Deprecated, to be removed as of January 1, 2015.

        Instantiate this object with the supplied parameters in `self.kwds`,
        or if already instantiated, return the cached instance.
        """
        self._warn("ObjectProxy.instantiate")
        if self.instance is None:
            self.instance = checked_call(self.cls, self.kwds)
        try:
            self.instance.yaml_src = self.yaml_src
        except AttributeError:
            pass
        return self.instance
コード例 #4
0
ファイル: yaml_parse.py プロジェクト: AlexArgus/pylearn2
    def instantiate(self):
        """
        .. warning::

            Deprecated, to be removed as of January 1, 2015.

        Instantiate this object with the supplied parameters in `self.kwds`,
        or if already instantiated, return the cached instance.
        """
        self._warn("ObjectProxy.instantiate")
        if self.instance is None:
            self.instance = checked_call(self.cls, self.kwds)
        try:
            self.instance.yaml_src = self.yaml_src
        except AttributeError:
            pass
        return self.instance
コード例 #5
0
ファイル: yaml_parse.py プロジェクト: AlexArgus/pylearn2
def _instantiate_proxy_tuple(proxy, bindings=None):
    """
    Helper function for `_instantiate` that handles objects of the `Proxy`
    class.

    Parameters
    ----------
    proxy : Proxy object
        A `Proxy` object that.
    bindings : dict, opitonal
        A dictionary mapping previously instantiated `Proxy` objects
        to their instantiated values.

    Returns
    -------
    obj : object
        The result object from recursively instantiating the object DAG.
    """
    if proxy in bindings:
        return bindings[proxy]
    else:
        # Respect do_not_recurse by just un-packing it (same as calling).
        if proxy.callable == do_not_recurse:
            obj = proxy.keywords['value']
        else:
            # TODO: add (requested) support for positionals (needs to be added
            # to checked_call also).
            if len(proxy.positionals) > 0:
                raise NotImplementedError('positional arguments not yet '
                                          'supported in proxy instantiation')
            kwargs = dict((k, _instantiate(v, bindings))
                          for k, v in proxy.keywords.iteritems())
            obj = checked_call(proxy.callable, kwargs)
        try:
            obj.yaml_src = proxy.yaml_src
        except AttributeError:  # Some classes won't allow this.
            pass
        bindings[proxy] = obj
        return bindings[proxy]
コード例 #6
0
def _instantiate_proxy_tuple(proxy, bindings=None):
    """
    Helper function for `_instantiate` that handles objects of the `Proxy`
    class.

    Parameters
    ----------
    proxy : Proxy object
        A `Proxy` object that.
    bindings : dict, opitonal
        A dictionary mapping previously instantiated `Proxy` objects
        to their instantiated values.

    Returns
    -------
    obj : object
        The result object from recursively instantiating the object DAG.
    """
    if proxy in bindings:
        return bindings[proxy]
    else:
        # Respect do_not_recurse by just un-packing it (same as calling).
        if proxy.callable == do_not_recurse:
            obj = proxy.keywords['value']
        else:
            # TODO: add (requested) support for positionals (needs to be added
            # to checked_call also).
            if len(proxy.positionals) > 0:
                raise NotImplementedError('positional arguments not yet '
                                          'supported in proxy instantiation')
            kwargs = dict((k, _instantiate(v, bindings))
                          for k, v in six.iteritems(proxy.keywords))
            obj = checked_call(proxy.callable, kwargs)
        try:
            obj.yaml_src = proxy.yaml_src
        except AttributeError:  # Some classes won't allow this.
            pass
        bindings[proxy] = obj
        return bindings[proxy]
コード例 #7
0
def package_call(to_call, ** kwargs):
    return checked_call(to_call, kwargs)
コード例 #8
0
def package_call(to_call, **kwargs):
    return checked_call(to_call, kwargs)
コード例 #9
0
ファイル: conv2d_c01b.py プロジェクト: klb3713/pylearn2
def setup_detector_layer_c01b(layer, input_space, rng, irange="not specified"):
    """
    .. todo::

        WRITEME properly

    Takes steps to set up an object for use as being some kind of convolutional
    layer. This function sets up only the detector layer.

    Does the following:

    * raises a RuntimeError if cuda is not available
    * sets layer.input_space to input_space
    * sets up addition of dummy channels for compatibility with cuda-convnet:

      - layer.dummy_channels: # of dummy channels that need to be added
        (You might want to check this and raise an Exception if it's not 0)
      - layer.dummy_space: The Conv2DSpace representing the input with dummy
        channels added

    * sets layer.detector_space to the space for the detector layer
    * sets layer.transformer to be a Conv2D instance
    * sets layer.b to the right value

    Parameters
    ----------
    layer : object
        Any python object that allows the modifications described below and
        has the following attributes:

          * pad : int describing amount of zero padding to add
          * kernel_shape : 2-element tuple or list describing spatial shape of
            kernel
          * fix_kernel_shape : bool, if true, will shrink the kernel shape to
            make it feasible, as needed (useful for hyperparameter searchers)
          * detector_channels : The number of channels in the detector layer
          * init_bias : numeric constant added to a tensor of zeros to
            initialize the bias
          * tied_b : If true, biases are shared across all spatial locations
    input_space : WRITEME
        A Conv2DSpace to be used as input to the layer
    rng : WRITEME
        A numpy RandomState or equivalent
    """

    if irange != "not specified":
        raise AssertionError(
            "There was a bug in setup_detector_layer_c01b."
            "It uses layer.irange instead of the irange parameter to the "
            "function. The irange parameter is now disabled by this "
            "AssertionError, so that this error message can alert you that "
            "the bug affected your code and explain why the interface is "
            "changing. The irange parameter to the function and this "
            "error message may be removed after April 21, 2014."
        )

    # Use "self" to refer to layer from now on, so we can pretend we're
    # just running in the set_input_space method of the layer
    self = layer

    # Make sure cuda is available
    check_cuda(str(type(self)))

    # Validate input
    if not isinstance(input_space, Conv2DSpace):
        raise TypeError(
            "The input to a convolutional layer should be a "
            "Conv2DSpace, but layer " + self.layer_name + " got " + str(type(self.input_space))
        )

    if not hasattr(self, "detector_channels"):
        raise ValueError(
            "layer argument must have a 'detector_channels' "
            "attribute specifying how many channels to put in "
            "the convolution kernel stack."
        )

    # Store the input space
    self.input_space = input_space

    # Make sure number of channels is supported by cuda-convnet
    # (multiple of 4 or <= 3)
    # If not supported, pad the input with dummy channels
    ch = self.input_space.num_channels
    rem = ch % 4
    if ch > 3 and rem != 0:
        self.dummy_channels = 4 - rem
    else:
        self.dummy_channels = 0
    self.dummy_space = Conv2DSpace(
        shape=input_space.shape, channels=input_space.num_channels + self.dummy_channels, axes=("c", 0, 1, "b")
    )

    if hasattr(self, "kernel_stride"):
        kernel_stride = self.kernel_stride
    else:
        kernel_stride = [1, 1]

    output_shape = [
        int(np.ceil((i_sh + 2.0 * self.pad - k_sh) / float(k_st))) + 1
        for i_sh, k_sh, k_st in zip(self.input_space.shape, self.kernel_shape, kernel_stride)
    ]

    def handle_kernel_shape(idx):
        if self.kernel_shape[idx] < 1:
            raise ValueError(
                "kernel must have strictly positive size on all " "axes but has shape: " + str(self.kernel_shape)
            )
        if output_shape[idx] <= 0:
            if self.fix_kernel_shape:
                self.kernel_shape[idx] = self.input_space.shape[idx] + 2 * self.pad
                assert self.kernel_shape[idx] != 0
                output_shape[idx] = 1
                warnings.warn("Had to change the kernel shape to make " "network feasible")
            else:
                raise ValueError("kernel too big for input " "(even with zero padding)")

    map(handle_kernel_shape, [0, 1])

    if self.detector_channels < 16:
        raise ValueError("Cuda-convnet requires the detector layer to have " "at least 16 channels.")

    self.detector_space = Conv2DSpace(shape=output_shape, num_channels=self.detector_channels, axes=("c", 0, 1, "b"))

    if hasattr(self, "partial_sum"):
        partial_sum = self.partial_sum
    else:
        partial_sum = 1

    if hasattr(self, "sparse_init") and self.sparse_init is not None:
        self.transformer = checked_call(
            make_sparse_random_conv2D,
            OrderedDict(
                [
                    ("num_nonzero", self.sparse_init),
                    ("input_space", self.input_space),
                    ("output_space", self.detector_space),
                    ("kernel_shape", self.kernel_shape),
                    ("pad", self.pad),
                    ("partial_sum", partial_sum),
                    ("kernel_stride", kernel_stride),
                    ("rng", rng),
                ]
            ),
        )
    else:
        self.transformer = make_random_conv2D(
            irange=self.irange,
            input_axes=self.input_space.axes,
            output_axes=self.detector_space.axes,
            input_channels=self.dummy_space.num_channels,
            output_channels=self.detector_space.num_channels,
            kernel_shape=self.kernel_shape,
            pad=self.pad,
            partial_sum=partial_sum,
            kernel_stride=kernel_stride,
            rng=rng,
        )

    W, = self.transformer.get_params()
    W.name = self.layer_name + "_W"

    if self.tied_b:
        self.b = sharedX(np.zeros(self.detector_space.num_channels) + self.init_bias)
    else:
        self.b = sharedX(self.detector_space.get_origin() + self.init_bias)
    self.b.name = self.layer_name + "_b"

    logger.info("Input shape: {0}".format(self.input_space.shape))
    logger.info("Detector space: {0}".format(self.detector_space.shape))
コード例 #10
0
def setup_detector_layer_c01b(layer, input_space, rng):
    """
    .. todo::

        WRITEME properly

    Takes steps to set up an object for use as being some kind of convolutional
    layer. This function sets up only the detector layer.

    Does the following:

    * raises a RuntimeError if cuda is not available
    * sets layer.input_space to input_space
    * sets up addition of dummy channels for compatibility with cuda-convnet:

      - layer.dummy_channels: # of dummy channels that need to be added
        (You might want to check this and raise an Exception if it's not 0)
      - layer.dummy_space: The Conv2DSpace representing the input with dummy
        channels added

    * sets layer.detector_space to the space for the detector layer
    * sets layer.transformer to be a Conv2D instance
    * sets layer.b to the right value

    Parameters
    ----------
    layer : object
        Any python object that allows the modifications described below and
        has the following attributes:

          * pad : int describing amount of zero padding to add
          * kernel_shape : 2-element tuple or list describing spatial shape of
            kernel
          * fix_kernel_shape : bool, if true, will shrink the kernel shape to
            make it feasible, as needed (useful for hyperparameter searchers)
          * detector_channels : The number of channels in the detector layer
          * init_bias : numeric constant added to a tensor of zeros to
            initialize the bias
          * tied_b : If true, biases are shared across all spatial locations
    input_space : WRITEME
        A Conv2DSpace to be used as input to the layer
    rng : WRITEME
        A numpy RandomState or equivalent
    """

    # Use "self" to refer to layer from now on, so we can pretend we're
    # just running in the set_input_space method of the layer
    self = layer

    # Make sure cuda is available
    check_cuda(str(type(self)))

    # Validate input
    if not isinstance(input_space, Conv2DSpace):
        raise TypeError("The input to a convolutional layer should be a "
                        "Conv2DSpace, but layer " + self.layer_name + " got " +
                        str(type(self.input_space)))

    if not hasattr(self, 'detector_channels'):
        raise ValueError("layer argument must have a 'detector_channels' "
                         "attribute specifying how many channels to put in "
                         "the convolution kernel stack.")

    # Store the input space
    self.input_space = input_space

    # Make sure number of channels is supported by cuda-convnet
    # (multiple of 4 or <= 3)
    # If not supported, pad the input with dummy channels
    ch = self.input_space.num_channels
    rem = ch % 4
    if ch > 3 and rem != 0:
        self.dummy_channels = 4 - rem
    else:
        self.dummy_channels = 0
    self.dummy_space = Conv2DSpace(
        shape=input_space.shape,
        channels=input_space.num_channels + self.dummy_channels,
        axes=('c', 0, 1, 'b')
    )

    if hasattr(self, 'kernel_stride'):
        kernel_stride = self.kernel_stride
    else:
        kernel_stride = [1, 1]

    output_shape = \
        [int(np.ceil((i_sh + 2. * self.pad - k_sh) / float(k_st))) + 1
         for i_sh, k_sh, k_st in zip(self.input_space.shape,
                                     self.kernel_shape, kernel_stride)]

    def handle_kernel_shape(idx):
        if self.kernel_shape[idx] < 1:
            raise ValueError("kernel must have strictly positive size on all "
                             "axes but has shape: " + str(self.kernel_shape))
        if output_shape[idx] <= 0:
            if self.fix_kernel_shape:
                self.kernel_shape[idx] = \
                    self.input_space.shape[idx] + 2 * self.pad
                assert self.kernel_shape[idx] != 0
                output_shape[idx] = 1
                warnings.warn("Had to change the kernel shape to make "
                              "network feasible")
            else:
                raise ValueError("kernel too big for input "
                                 "(even with zero padding)")

    map(handle_kernel_shape, [0, 1])

    if self.detector_channels < 16:
        raise ValueError("Cuda-convnet requires the detector layer to have "
                         "at least 16 channels.")

    self.detector_space = Conv2DSpace(shape=output_shape,
                                      num_channels=self.detector_channels,
                                      axes=('c', 0, 1, 'b'))

    if hasattr(self, 'partial_sum'):
        partial_sum = self.partial_sum
    else:
        partial_sum = 1

    if hasattr(self, 'sparse_init') and self.sparse_init is not None:
        self.transformer = \
            checked_call(make_sparse_random_conv2D,
                         OrderedDict([('num_nonzero', self.sparse_init),
                                      ('input_space', self.input_space),
                                      ('output_space', self.detector_space),
                                      ('kernel_shape', self.kernel_shape),
                                      ('pad', self.pad),
                                      ('partial_sum', partial_sum),
                                      ('kernel_stride', kernel_stride),
                                      ('rng', rng)]))
    else:
        self.transformer = make_random_conv2D(
            irange=self.irange,
            input_axes=self.input_space.axes,
            output_axes=self.detector_space.axes,
            input_channels=self.dummy_space.num_channels,
            output_channels=self.detector_space.num_channels,
            kernel_shape=self.kernel_shape,
            pad=self.pad,
            partial_sum=partial_sum,
            kernel_stride=kernel_stride,
            rng=rng
        )

    W, = self.transformer.get_params()
    W.name = self.layer_name + '_W'

    if self.tied_b:
        self.b = sharedX(np.zeros(self.detector_space.num_channels) +
                         self.init_bias)
    else:
        self.b = sharedX(self.detector_space.get_origin() + self.init_bias)
    self.b.name = self.layer_name + '_b'

    logger.info('Input shape: {0}'.format(self.input_space.shape))
    logger.info('Detector space: {0}'.format(self.detector_space.shape))
コード例 #11
0
ファイル: make_template_s3c.py プロジェクト: vd114/galatea
for i in xrange(len(means)):
    W[:,i] =  means[i]/norms[i]

init_mu = np.asarray(norms)

model = checked_call(S3C,
        dict(nvis = X.shape[1],
        nhid = len(means),
        init_bias_hid = inverse_sigmoid_numpy(np.asarray([ (y==i).mean() for i in xrange(y.max()+1)])),
        irange = 0.,
        min_B = .1,
        max_B = 1e6,
        min_alpha = .1,
        max_alpha = 1e6,
        m_step = checked_call(Grad_M_Step, dict(learning_rate = 1.)),
        init_mu = init_mu,
        init_alpha = init_mu * 10.,
        init_B = init_beta,
        e_step = E_Step_Scan(
                h_new_coeff_schedule = [ .1 ] * 50,
                s_new_coeff_schedule = [ .1 ] * 50,
                clip_reflections = 1,
                rho = 0.5
            ))
    )

model.W.set_value(W)

model.dataset_yaml_src = '!obj:pylearn2.datasets.mnist.MNIST { which_set : "train", center : 0 }'

serial.save('/u/goodfeli/galatea/pddbm/config/mnist/s3c_hack.pkl',model)