本文整理汇总了Python中blocks.bricks.conv.Convolutional.apply方法的典型用法代码示例。如果您正苦于以下问题:Python Convolutional.apply方法的具体用法?Python Convolutional.apply怎么用?Python Convolutional.apply使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类blocks.bricks.conv.Convolutional
的用法示例。
在下文中一共展示了Convolutional.apply方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ConvolutionalActivation
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import apply [as 别名]
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
示例2: test_tied_biases
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import apply [as 别名]
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)
示例3: test_convolutional
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import apply [as 别名]
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)
示例4: test_untied_biases
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import apply [as 别名]
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)
示例5: test_no_input_size
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import apply [as 别名]
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)
示例6: ResidualConvolutional
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import apply [as 别名]
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)
#.........这里部分代码省略.........
示例7: enumerate
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import apply [as 别名]
for i, p in enumerate(convs):
# Convolution bricks
conv = Convolutional(
filter_size=(p["filter_size"], 1),
# step=(p['stride'],1),
num_filters=p["nfilter"],
num_channels=conv_in_channels,
batch_size=batch_size,
border_mode="valid",
tied_biases=True,
name="conv%d" % i,
)
cb.append(conv)
maxpool = MaxPooling(pooling_size=(p["pool_stride"], 1), name="mp%d" % i)
conv_out = conv.apply(conv_in)[:, :, :: p["stride"], :]
conv_out = maxpool.apply(conv_out)
if p["normalize"]:
conv_out_mean = conv_out.mean(axis=2).mean(axis=0)
conv_out_var = ((conv_out - conv_out_mean[None, :, None, :]) ** 2).mean(axis=2).mean(axis=0).sqrt()
conv_out = (conv_out - conv_out_mean[None, :, None, :]) / conv_out_var[None, :, None, :]
if p["activation"] is not None:
conv_out = p["activation"].apply(conv_out)
if p["dropout"] > 0:
b = [p["activation"] if p["activation"] is not None else conv]
dropout_locs.append((VariableFilter(bricks=b, name="output"), p["dropout"]))
if p["skip"] is not None and len(p["skip"]) > 0:
maxpooladd = MaxPooling(pooling_size=(p["stride"] * p["pool_stride"], 1), name="Mp%d" % i)
skip = []
if "max" in p["skip"]:
skip.append(maxpooladd.apply(conv_in)[:, :, : conv_out.shape[2], :])
示例8: Convolutional
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import apply [as 别名]
X = T.matrix("features")
o = X.reshape((X.shape[0], 1, 28, 28))
l = Convolutional(filter_size=(5, 5),
num_filters=32,
num_channels=1,
image_size=(28,28),
weights_init=IsotropicGaussian(std=0.01),
biases_init=IsotropicGaussian(std=0.01, mean=1.0),
use_bias=True,
border_mode="valid",
step=(1,1))
l.initialize()
o = l.apply(o)
l = BatchNormalizationConv(input_shape=l.get_dim("output"),
B_init=IsotropicGaussian(std=0.01),
Y_init=IsotropicGaussian(std=0.01))
l.initialize()
o = l.apply(o)
o = Rectifier().apply(o)
l = MaxPooling(pooling_size=(2, 2),
step=(2, 2),
input_dim=l.get_dim("output"))
l.initialize()
o = l.apply(o)
示例9: Convolutional
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import apply [as 别名]
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()
示例10: SpatialNoise
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import apply [as 别名]
class SpatialNoise(NoiseLayer, Initializable, Random):
"""A learned noise layer.
"""
@lazy(allocation=['input_dim'])
def __init__(self, input_dim, noise_batch_size=None, noise_rate=None,
tied_noise=False, tied_sigma=False,
prior_mean=0, prior_noise_level=0, **kwargs):
self.mask = Convolutional(name='mask')
self.flatten = GlobalAverageFlattener() if tied_sigma else None
children = list(p for p in [self.mask, self.flatten] if p is not None)
kwargs.setdefault('children', []).extend(children)
super(SpatialNoise, self).__init__(**kwargs)
self.input_dim = input_dim
self.tied_noise = tied_noise
self.tied_sigma = tied_sigma
self.noise_batch_size = noise_batch_size
self.noise_rate = noise_rate if noise_rate is not None else 1.0
self.prior_mean = prior_mean
self.prior_noise_level = prior_noise_level
self._training_mode = []
def _push_allocation_config(self):
self.mask.filter_size = (1, 1)
self.mask.num_filters = self.num_channels
self.mask.num_channels = self.num_channels
self.mask.image_size = self.image_size
def _allocate(self):
if self.noise_batch_size is not None:
if self.tied_noise:
N = shared_floatx_zeros(
(self.noise_batch_size, self.input_dim[0]), name='N')
else:
N = shared_floatx_zeros(
(self.noise_batch_size,) + self.input_dim, name='N')
add_role(N, NOISE)
self.parameters.append(N)
@application(inputs=['input_'], outputs=['output'])
def apply(self, input_, application_call):
"""Apply the linear transformation followed by masking with noise.
Parameters
----------
input_ : :class:`~tensor.TensorVariable`
The input on which to apply the transformations
Returns
-------
output : :class:`~tensor.TensorVariable`
The transformed input
"""
# When not in training mode, turn off noise
if not self._training_mode:
return input_
if self.tied_sigma:
average = tensor.shape_padright(self.flatten.apply(input_), 2)
noise_level = (self.prior_noise_level -
tensor.clip(self.mask.apply(average), -16, 16))
noise_level = tensor.patternbroadcast(noise_level,
(False, False, True, True))
noise_level = copy_and_tag_noise(
noise_level, self, LOG_SIGMA, 'log_sigma')
else:
average = input_
noise_level = (self.prior_noise_level -
tensor.clip(self.mask.apply(input_), -16, 16))
noise_level = copy_and_tag_noise(
noise_level, self, LOG_SIGMA, 'log_sigma')
# Allow incomplete batches by just taking the noise that is needed
if self.tied_noise:
if self.noise_batch_size is not None:
noise = self.parameters[0][:input_.shape[0], :]
else:
noise = self.theano_rng.normal(input_.shape[0:2])
noise = tensor.shape_padright(2)
else:
if self.noise_batch_size is not None:
noise = self.parameters[0][:input_.shape[0], :, :, :]
else:
noise = self.theano_rng.normal(input_.shape)
kl = (
self.prior_noise_level - noise_level
+ 0.5 * (
tensor.exp(2 * noise_level)
+ (average - self.prior_mean) ** 2
) / tensor.exp(2 * self.prior_noise_level)
- 0.5
)
application_call.add_auxiliary_variable(kl, roles=[NITS], name='nits')
return input_ + self.noise_rate * tensor.exp(noise_level) * noise
# Needed for the Feedforward interface.
@property
def output_dim(self):
return self.input_dim
# The following properties allow for BatchNormalization bricks
# to be used directly inside of a ConvolutionalSequence.
@property
#.........这里部分代码省略.........
示例11: NoisyConvolutional
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import apply [as 别名]
class NoisyConvolutional(Initializable, Feedforward, Random):
"""Convolutional transformation sent through a learned noisy channel.
Parameters (same as Convolutional)
"""
@lazy(allocation=[
'filter_size', 'num_filters', 'num_channels', 'noise_batch_size'])
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
def _push_allocation_config(self):
self.convolution.filter_size = self.filter_size
self.convolution.num_filters = self.num_filters
self.convolution.num_channels = self.num_channels
# self.convolution.batch_size = self.batch_size
self.convolution.image_size = self.image_size
self.convolution.step = self.step
self.convolution.border_mode = self.border_mode
self.convolution.tied_biases = self.tied_biases
self.mask.filter_size = (1, 1)
self.mask.num_filters = self.num_filters
self.mask.num_channels = self.num_filters
# self.mask.batch_size = self.batch_size
self.mask.image_size = self.convolution.get_dim('output')[1:]
# self.mask.step = self.step
# self.mask.border_mode = self.border_mode
self.mask.tied_biases = self.tied_biases
def _allocate(self):
out_shape = self.convolution.get_dim('output')
N = shared_floatx_zeros((self.noise_batch_size,) + out_shape, name='N')
add_role(N, NOISE)
self.parameters.append(N)
@application(inputs=['input_'], outputs=['output'])
def apply(self, input_, application_call):
"""Apply the linear transformation followed by masking with noise.
Parameters
----------
input_ : :class:`~tensor.TensorVariable`
The input on which to apply the transformations
Returns
-------
output : :class:`~tensor.TensorVariable`
The transformed input
"""
from theano.printing import Print
pre_noise = self.convolution.apply(input_)
# noise_level = self.mask.apply(input_)
noise_level = (self.prior_noise_level -
tensor.clip(self.mask.apply(pre_noise), -16, 16))
noise_level = copy_and_tag_noise(
noise_level, self, LOG_SIGMA, 'log_sigma')
# Allow incomplete batches by just taking the noise that is needed
noise = self.parameters[0][:noise_level.shape[0], :, :, :]
# noise = self.theano_rng.normal(noise_level.shape)
kl = (
self.prior_noise_level - noise_level
+ 0.5 * (
tensor.exp(2 * noise_level)
+ (pre_noise - self.prior_mean) ** 2
) / tensor.exp(2 * self.prior_noise_level)
- 0.5
)
application_call.add_auxiliary_variable(kl, roles=[NITS], name='nits')
return pre_noise + tensor.exp(noise_level) * noise
def get_dim(self, name):
if name == 'input_':
return self.convolution.get_dim(name)
if name == 'output':
return self.convolution.get_dim(name)
if name == 'nits':
return self.convolution.get_dim('output')
return super(NoisyConvolutional, self).get_dim(name)
@property
def num_output_channels(self):
return self.num_filters