当前位置: 首页>>代码示例>>Python>>正文


Python conv.Convolutional类代码示例

本文整理汇总了Python中blocks.bricks.conv.Convolutional的典型用法代码示例。如果您正苦于以下问题:Python Convolutional类的具体用法?Python Convolutional怎么用?Python Convolutional使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Convolutional类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_untied_biases

def test_untied_biases():
    x = tensor.tensor4('x')
    num_channels = 4
    num_filters = 3
    batch_size = 5
    filter_size = (3, 3)
    conv = Convolutional(filter_size, num_filters, num_channels,
                         weights_init=Constant(1.), biases_init=Constant(2.),
                         image_size=(28, 30), tied_biases=False)
    conv.initialize()

    y = conv.apply(x)
    func = function([x], y)

    # Untied biases provide a bias for every individual output
    assert_allclose(conv.b.eval().shape, (3, 26, 28))

    # Untied biases require images of a specific size
    x_val_1 = numpy.ones((batch_size, num_channels, 28, 30),
                         dtype=theano.config.floatX)

    assert_allclose(func(x_val_1),
                    numpy.prod(filter_size) * num_channels *
                    numpy.ones((batch_size, num_filters, 26, 28)) + 2)

    x_val_2 = numpy.ones((batch_size, num_channels, 23, 19),
                         dtype=theano.config.floatX)

    def wrongsize():
        func(x_val_2)

    assert_raises_regexp(AssertionError, 'AbstractConv shape mismatch',
                         wrongsize)
开发者ID:SwordYork,项目名称:blocks,代码行数:33,代码来源:test_conv.py

示例2: test_tied_biases

def test_tied_biases():
    x = tensor.tensor4('x')
    num_channels = 4
    num_filters = 3
    batch_size = 5
    filter_size = (3, 3)
    conv = Convolutional(filter_size, num_filters, num_channels,
                         weights_init=Constant(1.), biases_init=Constant(2.),
                         tied_biases=True)
    conv.initialize()
    y = conv.apply(x)
    func = function([x], y)

    # Tied biases allows to pass images of different sizes
    x_val_1 = numpy.ones((batch_size, num_channels, 10,
                          12), dtype=theano.config.floatX)
    x_val_2 = numpy.ones((batch_size, num_channels, 23,
                          19), dtype=theano.config.floatX)

    assert_allclose(func(x_val_1),
                    numpy.prod(filter_size) * num_channels *
                    numpy.ones((batch_size, num_filters, 8, 10)) + 2)
    assert_allclose(func(x_val_2),
                    numpy.prod(filter_size) * num_channels *
                    numpy.ones((batch_size, num_filters, 21, 17)) + 2)
开发者ID:xuanhan863,项目名称:blocks,代码行数:25,代码来源:test_conv.py

示例3: build_conv_layers

    def build_conv_layers(self, image=None) :

        if image is None :
            image = T.ftensor4('spectrogram')
        else :
            image = image

        conv_list = []
        for layer in range(self.layers) :
            layer_param = self.params[layer]
            conv_layer = Convolutional(layer_param[0], layer_param[1], layer_param[2])
            pool_layer = MaxPooling(layer_param[3])

            conv_layer.name = "convolution"+str(layer)
            pool_layer.name = "maxpooling"+str(layer)

            conv_list.append(conv_layer)
            conv_list.append(pool_layer)
            conv_list.append(Rectifier())

        conv_seq = ConvolutionalSequence(
            conv_list,
            self.params[0][2],
            image_size=self.image_size,
            weights_init=IsotropicGaussian(std=0.5, mean=0),
            biases_init=Constant(0))

        conv_seq._push_allocation_config()
        conv_seq.initialize()
        out = conv_seq.apply(image)

        return out, conv_seq.get_dim('output')
开发者ID:olimastro,项目名称:ift6266,代码行数:32,代码来源:convolution.py

示例4: conv_layer

    def conv_layer(self, name, wt, bias, image_size):
        """Creates a Convolutional brick with the given name, weights,
        bias, and image_size."""

        layer = Convolutional(
            name=name,
            filter_size=wt.shape[0:2],
            num_channels=wt.shape[2],  # in
            num_filters=wt.shape[3],  # out
            weights_init=Constant(0),  # does not matter
            biases_init=Constant(0),  # does not matter
            tied_biases=True,
            border_mode="valid",
        )

        if image_size:
            layer.image_size = image_size

        layer.initialize()

        weights = self.to_bc01(wt)
        layer.parameters[0].set_value(weights.astype("float32"))  # W
        layer.parameters[1].set_value(bias.squeeze().astype("float32"))  # b

        return (layer, layer.get_dim("output")[1:3])
开发者ID:Rene90,项目名称:dl4nlp,代码行数:25,代码来源:imagenet.py

示例5: test_convolutional_sequence

def test_convolutional_sequence():
    x = tensor.tensor4('x')
    num_channels = 4
    pooling_size = 3
    batch_size = 5
    act = Rectifier()

    conv = Convolutional((3, 3), 5, weights_init=Constant(1.),
                         biases_init=Constant(5.))
    pooling = MaxPooling(pooling_size=(pooling_size, pooling_size))
    conv2 = Convolutional((2, 2), 4, weights_init=Constant(1.))

    seq = ConvolutionalSequence([conv, act, pooling.apply, conv2.apply, act],
                                num_channels, image_size=(17, 13))
    seq.push_allocation_config()
    assert conv.num_channels == 4
    assert conv2.num_channels == 5
    conv2.use_bias = False
    y = seq.apply(x)
    seq.initialize()
    func = function([x], y)

    x_val = numpy.ones((batch_size, 4, 17, 13), dtype=theano.config.floatX)
    y_val = (numpy.ones((batch_size, 4, 4, 2)) *
             (9 * 4 + 5) * 4 * 5)
    assert_allclose(func(x_val), y_val)
开发者ID:SwordYork,项目名称:blocks,代码行数:26,代码来源:test_conv.py

示例6: ConvolutionalActivation

class ConvolutionalActivation(Initializable):
    """A convolution followed by an activation function.
    Parameters
    ----------
    activation : :class:`.BoundApplication`
        The application method to apply after convolution (i.e.
        the nonlinear activation function)
    See Also
    --------
    :class:`Convolutional` : For the documentation of other parameters.
    """
    @lazy(allocation=['filter_size', 'num_filters', 'num_channels'])
    def __init__(self, activation, filter_size, num_filters, num_channels,
                 batch_size=None, image_size=None, step=(1, 1),
                 border_mode='valid', tied_biases=False, **kwargs):
        self.convolution = Convolutional(name='conv'+ kwargs['name'])
        self.bn = BatchNorm(name='bn'+ kwargs['name'])
        self.activation = activation
        self.filter_size = filter_size
        self.num_filters = num_filters
        self.num_channels = num_channels
        self.batch_size = batch_size
        self.image_size = image_size
        self.step = step
        self.border_mode = border_mode
        self.tied_biases = tied_biases
        super(ConvolutionalActivation, self).__init__(**kwargs)
        self.children = [self.convolution, self.bn, self.activation]
        

    def _push_allocation_config(self):
        for attr in ['filter_size', 'num_filters', 'step', 'border_mode',
                     'batch_size', 'num_channels', 'image_size',
                     'tied_biases']:
            setattr(self.convolution, attr, getattr(self, attr))
        setattr(self.bn, 'input_dim', self.num_filters)
        
    def get_dim(self, name):
        # TODO The name of the activation output doesn't need to be `output`
        return self.convolution.get_dim(name)
        
    def apply(self, input_):
        out = self.convolution.apply(input_)
        out = self.bn.apply(out)
        out = self.activation.apply(out)
        return out
        
    def inference(self, input_):
        out = self.convolution.apply(input_)
        out = self.bn.inference(out)
        out = self.activation.apply(out)
        return out
开发者ID:harmdevries89,项目名称:lvq,代码行数:52,代码来源:conv.py

示例7: test_convolutional

def test_convolutional():
    x = tensor.tensor4("x")
    num_channels = 4
    num_filters = 3
    batch_size = 5
    filter_size = (3, 3)
    conv = Convolutional(
        filter_size,
        num_filters,
        num_channels,
        image_size=(17, 13),
        weights_init=Constant(1.0),
        biases_init=Constant(5.0),
    )
    conv.initialize()
    y = conv.apply(x)
    func = function([x], y)

    x_val = numpy.ones((batch_size, num_channels, 17, 13), dtype=theano.config.floatX)
    assert_allclose(
        func(x_val), numpy.prod(filter_size) * num_channels * numpy.ones((batch_size, num_filters, 15, 11)) + 5
    )
    conv.image_size = (17, 13)
    conv.batch_size = 2  # This should have effect on get_dim
    assert conv.get_dim("output") == (num_filters, 15, 11)
开发者ID:piergiaj,项目名称:blocks,代码行数:25,代码来源:test_conv.py

示例8: __init__

 def __init__(self, filter_size, num_filters, num_channels, noise_batch_size,
              image_size=(None, None), step=(1, 1), border_mode='valid',
              tied_biases=True,
              prior_mean=0, prior_noise_level=0, **kwargs):
     self.convolution = Convolutional()
     self.mask = Convolutional(name='mask')
     children = [self.convolution, self.mask]
     kwargs.setdefault('children', []).extend(children)
     super(NoisyConvolutional, self).__init__(**kwargs)
     self.filter_size = filter_size
     self.num_filters = num_filters
     self.num_channels = num_channels
     self.noise_batch_size = noise_batch_size
     self.image_size = image_size
     self.step = step
     self.border_mode = border_mode
     self.tied_biases = tied_biases
     self.prior_mean = prior_mean
     self.prior_noise_level = prior_noise_level
开发者ID:davidbau,项目名称:net-intent,代码行数:19,代码来源:noisy.py

示例9: __init__

    def __init__(self, filter_size, num_filters, num_channels,
                 batch_size=None,
                 mid_noise=False,
                 out_noise=False,
                 tied_noise=False,
                 tied_sigma=False,
                 noise_rate=None,
                 noise_batch_size=None,
                 prior_noise_level=None,
                 image_size=(None, None), step=(1, 1),
                 **kwargs):
        self.filter_size = filter_size
        self.num_filters = num_filters
        self.batch_size = batch_size
        self.num_channels = num_channels
        self.image_size = image_size
        self.mid_noise = mid_noise
        self.noise_batch_size = noise_batch_size
        self.noise_rate = noise_rate
        self.step = step
        self.border_mode = 'half'
        self.tied_biases = True
        depth = 2

        self.b0 = SpatialBatchNormalization(name='b0')
        self.r0 = Rectifier(name='r0')
        self.n0 = (SpatialNoise(name='n0', noise_rate=self.noise_rate,
                tied_noise=tied_noise, tied_sigma=tied_sigma,
                prior_noise_level=prior_noise_level) if mid_noise else None)
        self.c0 = Convolutional(name='c0')
        self.b1 = SpatialBatchNormalization(name='b1')
        self.r1 = Rectifier(name='r1')
        self.n1 = (SpatialNoise(name='n1', noise_rate=self.noise_rate,
                tied_noise=tied_noise, tied_sigma=tied_sigma,
                prior_noise_level=prior_noise_level) if out_noise else None)
        self.c1 = Convolutional(name='c1')
        kwargs.setdefault('children', []).extend([c for c in [
            self.c0, self.b0, self.r0, self.n0,
            self.c1, self.b1, self.r1, self.n1] if c is not None])
        super(ResidualConvolutional, self).__init__(**kwargs)
开发者ID:davidbau,项目名称:net-intent,代码行数:40,代码来源:resnet.py

示例10: __init__

 def __init__(self, activation, filter_size, num_filters, num_channels,
              batch_size=None, image_size=None, step=(1, 1),
              border_mode='valid', tied_biases=False, **kwargs):
     self.convolution = Convolutional(name='conv'+ kwargs['name'])
     self.bn = BatchNorm(name='bn'+ kwargs['name'])
     self.activation = activation
     self.filter_size = filter_size
     self.num_filters = num_filters
     self.num_channels = num_channels
     self.batch_size = batch_size
     self.image_size = image_size
     self.step = step
     self.border_mode = border_mode
     self.tied_biases = tied_biases
     super(ConvolutionalActivation, self).__init__(**kwargs)
     self.children = [self.convolution, self.bn, self.activation]
开发者ID:harmdevries89,项目名称:lvq,代码行数:16,代码来源:conv.py

示例11: __init__

    def __init__(self, activation, filter_size, num_filters, num_channels,
                 batch_size=None, image_size=None, step=(1, 1),
                 border_mode='valid', **kwargs):
        self.convolution = Convolutional()

        self.filter_size = filter_size
        self.num_filters = num_filters
        self.num_channels = num_channels
        self.batch_size = batch_size
        self.image_size = image_size
        self.step = step
        self.border_mode = border_mode

        super(ConvolutionalActivation, self).__init__(
            application_methods=[self.convolution.apply, activation],
            **kwargs)
开发者ID:jpilaul,项目名称:IFT6266_project,代码行数:16,代码来源:blocks_bricks_conv.py

示例12: ConvolutionalActivation

class ConvolutionalActivation(Sequence, Initializable):
    """A convolution followed by an activation function.
    Parameters
    ----------
    activation : :class:`.BoundApplication`
        The application method to apply after convolution (i.e.
        the nonlinear activation function)
    See Also
    --------
    :class:`Convolutional` for the other parameters.
    """
    
    @lazy(allocation=['filter_size', 'num_filters', 'num_channels'])
    def __init__(self, activation, filter_size, num_filters, num_channels,
                 batch_size=None, image_size=None, step=(1, 1),
                 border_mode='valid', **kwargs):
        self.convolution = Convolutional()

        self.filter_size = filter_size
        self.num_filters = num_filters
        self.num_channels = num_channels
        self.batch_size = batch_size
        self.image_size = image_size
        self.step = step
        self.border_mode = border_mode

        super(ConvolutionalActivation, self).__init__(
            application_methods=[self.convolution.apply, activation],
            **kwargs)

    def _push_allocation_config(self):
        for attr in ['filter_size', 'num_filters', 'step', 'border_mode',
                     'batch_size', 'num_channels', 'image_size']:
            setattr(self.convolution, attr, getattr(self, attr))

    def get_dim(self, name):
        # TODO The name of the activation output doesn't need to be `output`
        return self.convolution.get_dim(name)
开发者ID:jpilaul,项目名称:IFT6266_project,代码行数:38,代码来源:blocks_bricks_conv.py

示例13: test_no_input_size

def test_no_input_size():
    # suppose x is outputted by some RNN
    x = tensor.tensor4('x')
    filter_size = (1, 3)
    num_filters = 2
    num_channels = 5
    c = Convolutional(filter_size, num_filters, num_channels, tied_biases=True,
                      weights_init=Constant(1.), biases_init=Constant(1.))
    c.initialize()
    out = c.apply(x)
    assert c.get_dim('output') == (2, None, None)
    assert out.ndim == 4

    c = Convolutional(filter_size, num_filters, num_channels,
                      tied_biases=False, weights_init=Constant(1.),
                      biases_init=Constant(1.))
    assert_raises_regexp(ValueError, 'Cannot infer bias size \S+',
                         c.initialize)
开发者ID:xuanhan863,项目名称:blocks,代码行数:18,代码来源:test_conv.py

示例14: ResidualConvolutional

class ResidualConvolutional(Initializable):
    @lazy(allocation=['filter_size', 'num_filters', 'num_channels'])
    def __init__(self, filter_size, num_filters, num_channels,
                 batch_size=None,
                 mid_noise=False,
                 out_noise=False,
                 tied_noise=False,
                 tied_sigma=False,
                 noise_rate=None,
                 noise_batch_size=None,
                 prior_noise_level=None,
                 image_size=(None, None), step=(1, 1),
                 **kwargs):
        self.filter_size = filter_size
        self.num_filters = num_filters
        self.batch_size = batch_size
        self.num_channels = num_channels
        self.image_size = image_size
        self.mid_noise = mid_noise
        self.noise_batch_size = noise_batch_size
        self.noise_rate = noise_rate
        self.step = step
        self.border_mode = 'half'
        self.tied_biases = True
        depth = 2

        self.b0 = SpatialBatchNormalization(name='b0')
        self.r0 = Rectifier(name='r0')
        self.n0 = (SpatialNoise(name='n0', noise_rate=self.noise_rate,
                tied_noise=tied_noise, tied_sigma=tied_sigma,
                prior_noise_level=prior_noise_level) if mid_noise else None)
        self.c0 = Convolutional(name='c0')
        self.b1 = SpatialBatchNormalization(name='b1')
        self.r1 = Rectifier(name='r1')
        self.n1 = (SpatialNoise(name='n1', noise_rate=self.noise_rate,
                tied_noise=tied_noise, tied_sigma=tied_sigma,
                prior_noise_level=prior_noise_level) if out_noise else None)
        self.c1 = Convolutional(name='c1')
        kwargs.setdefault('children', []).extend([c for c in [
            self.c0, self.b0, self.r0, self.n0,
            self.c1, self.b1, self.r1, self.n1] if c is not None])
        super(ResidualConvolutional, self).__init__(**kwargs)

    def get_dim(self, name):
        if name == 'input_':
            return ((self.num_channels,) + self.image_size)
        if name == 'output':
            return self.c1.get_dim(name)
        return super(ResidualConvolutionalUnit, self).get_dim(name)

    @property
    def num_output_channels(self):
        return self.num_filters

    def _push_allocation_config(self):
        self.b0.input_dim = self.get_dim('input_')
        self.b0.push_allocation_config()
        if self.r0:
            self.r0.push_allocation_config()
        if self.n0:
            self.n0.noise_batch_size = self.noise_batch_size
            self.n0.num_channels = self.num_channels
            self.n0.image_size = self.image_size
        self.c0.filter_size = self.filter_size
        self.c0.batch_size = self.batch_size
        self.c0.num_channels = self.num_channels
        self.c0.num_filters = self.num_filters
        self.c0.border_mode = self.border_mode
        self.c0.image_size = self.image_size
        self.c0.step = self.step
        self.c0.use_bias = False
        self.c0.push_allocation_config()
        c0_shape = self.c0.get_dim('output')
        self.b1.input_dim = c0_shape
        self.b1.push_allocation_config()
        self.r1.push_allocation_config()
        if self.n1:
            self.n1.noise_batch_size = self.noise_batch_size
            self.n1.num_channels = self.num_filters
            self.n1.image_size = c0_shape[1:]
        self.c1.filter_size = self.filter_size
        self.c1.batch_size = self.batch_size
        self.c1.num_channels = self.num_filters
        self.c1.num_filters = self.num_filters
        self.c1.border_mode = self.border_mode
        self.c1.image_size = c0_shape[1:]
        self.c1.step = (1, 1)
        self.c1.use_bias = False
        self.c1.push_allocation_config()

    @application(inputs=['input_'], outputs=['output'])
    def apply(self, input_):
        shortcut = input_
        # Batchnorm, then Relu, then Convolution
        first_conv = self.b0.apply(input_)
        first_conv = self.r0.apply(first_conv)
        if self.n0:
            first_conv = self.n0.apply(first_conv)
        first_conv = self.c0.apply(first_conv)
        # Batchnorm, then Relu, then Convolution (second time)
#.........这里部分代码省略.........
开发者ID:davidbau,项目名称:net-intent,代码行数:101,代码来源:resnet.py

示例15: Convolutional

from theano import tensor
x = tensor.matrix('features')

from blocks.bricks import Linear, Rectifier, Softmax
from blocks.bricks.conv import Convolutional, ConvolutionalActivation
input_to_hidden = Convolutional((5,5), 32, 1,border_mode='same')
h = Rectifier().apply(input_to_hidden.apply(x))


hidden_to_output = Linear(name='hidden_to_output', input_dim=100, output_dim=10)
y_hat = Softmax().apply(hidden_to_output.apply(h))

y = tensor.lmatrix('targets')
from blocks.bricks.cost import CategoricalCrossEntropy
cost = CategoricalCrossEntropy().apply(y.flatten(), y_hat)

from blocks.bricks import WEIGHT
from blocks.graph import ComputationGraph
from blocks.filter import VariableFilter
cg = ComputationGraph(cost)
W1, W2 = VariableFilter(roles=[WEIGHT])(cg.variables)
cost = cost + 0.005 * (W1 ** 2).sum() + 0.005 * (W2 ** 2).sum()
cost.name = 'cost_with_regularization'

from blocks.bricks import MLP
mlp = MLP(activations=[Rectifier(), Softmax()], dims=[784, 100, 10]).apply(x)

from blocks.initialization import IsotropicGaussian, Constant
input_to_hidden.weights_init = hidden_to_output.weights_init = IsotropicGaussian(0.01)
input_to_hidden.biases_init = hidden_to_output.biases_init = Constant(0)
input_to_hidden.initialize()
开发者ID:mwoodson1,项目名称:MNIST,代码行数:31,代码来源:MNIST_blocks.py


注:本文中的blocks.bricks.conv.Convolutional类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。