本文整理匯總了Python中blocks.bricks.conv.MaxPooling類的典型用法代碼示例。如果您正苦於以下問題:Python MaxPooling類的具體用法?Python MaxPooling怎麽用?Python MaxPooling使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了MaxPooling類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_max_pooling_old_pickle
def test_max_pooling_old_pickle():
brick = MaxPooling((3, 4))
brick.allocate()
# Simulate old pickle, before #899.
del brick.ignore_border
del brick.mode
del brick.padding
# Pickle in this broken state and re-load.
broken_pickled = pickle.dumps(brick)
loaded = pickle.loads(broken_pickled)
# Same shape, same step.
assert brick.pooling_size == loaded.pooling_size
assert brick.step == loaded.step
# Check that the new attributes were indeed added.
assert hasattr(loaded, "padding") and loaded.padding == (0, 0)
assert hasattr(loaded, "mode") and loaded.mode == "max"
assert hasattr(loaded, "ignore_border") and not loaded.ignore_border
try:
loaded.apply(tensor.tensor4())
except Exception:
raise AssertionError("failed to apply on unpickled MaxPooling")
# Make sure we're not overriding these attributes wrongly.
new_brick = MaxPooling((4, 3), padding=(2, 1))
new_brick_unpickled = pickle.loads(pickle.dumps(new_brick))
assert new_brick_unpickled.padding == (2, 1)
assert new_brick_unpickled.ignore_border
示例2: 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')
示例3: test_max_pooling
def test_max_pooling():
x = tensor.tensor4("x")
num_channels = 4
batch_size = 5
x_size = 17
y_size = 13
pool_size = 3
pool = MaxPooling((pool_size, pool_size))
y = pool.apply(x)
func = function([x], y)
x_val = numpy.ones((batch_size, num_channels, x_size, y_size), dtype=theano.config.floatX)
assert_allclose(func(x_val), numpy.ones((batch_size, num_channels, x_size / pool_size, y_size / pool_size)))
pool.input_dim = (x_size, y_size)
pool.get_dim("output") == (num_channels, x_size / pool_size + 1, y_size / pool_size + 1)
示例4: pool_layer
def pool_layer(self, name, method, pool, pad, stride, image_size):
"""Creates a MaxPooling brick with the given name, pooling size, stride,
and image size. If a string other than 'max' is passed in the 'method'
parameter, the function throws an exception. The 'pad' argument
are ignored. It is instead handled in the conversion through a Padding
brick (see below)."""
# FIX: ignore padding [0 1 0 1]
if method == 'max':
layer = MaxPooling(name=name, pooling_size=pool, step=stride, input_dim=image_size)
else:
raise Exception("Unsupported pooling method: %s" % method)
return (layer, layer.get_dim("output"))
示例5: __init__
def __init__(self, activation, filter_size, num_filters, pooling_size,
num_channels, conv_step=(1, 1), pooling_step=None,
batch_size=None, image_size=None, border_mode='valid',
**kwargs):
self.convolution = ConvolutionalActivation(activation, filter_size, num_filters, num_channels)
self.pooling = MaxPooling()
super(ConvolutionalLayer, self).__init__(
application_methods=[self.convolution.apply,
self.pooling.apply], **kwargs)
self.convolution.name = self.name + '_convolution'
self.pooling.name = self.name + '_pooling'
self.filter_size = filter_size
self.num_filters = num_filters
self.num_channels = num_channels
self.pooling_size = pooling_size
self.conv_step = conv_step
self.pooling_step = pooling_step
self.batch_size = batch_size
self.border_mode = border_mode
self.image_size = image_size
示例6: test_max_pooling_padding
def test_max_pooling_padding():
x = tensor.tensor4("x")
brick = MaxPooling((6, 2), padding=(3, 1), ignore_border=True)
y = brick.apply(x)
out = y.eval({x: numpy.zeros((2, 3, 6, 10), dtype=theano.config.floatX)})
assert out.shape == (2, 3, 2, 6)
示例7: test_max_pooling_ignore_border_false
def test_max_pooling_ignore_border_false():
x = tensor.tensor4("x")
brick = MaxPooling((5, 7), ignore_border=False)
y = brick.apply(x)
out = y.eval({x: numpy.zeros((4, 6, 12, 15), dtype=theano.config.floatX)})
assert out.shape == (4, 6, 3, 3)
示例8: test_max_pooling_ignore_border_true
def test_max_pooling_ignore_border_true():
x = tensor.tensor4("x")
brick = MaxPooling((3, 4), ignore_border=True)
y = brick.apply(x)
out = y.eval({x: numpy.zeros((8, 3, 10, 13), dtype=theano.config.floatX)})
assert out.shape == (8, 3, 3, 3)
示例9: ConvolutionalLayer
class ConvolutionalLayer(Sequence, Initializable):
"""A complete convolutional layer: Convolution, nonlinearity, pooling.
.. todo::
Mean pooling.
Parameters
----------
activation : :class:`.BoundApplication`
The application method to apply in the detector stage (i.e. the
nonlinearity before pooling. Needed for ``__init__``.
See Also
--------
:class:`Convolutional` and :class:`MaxPooling` for the other
parameters.
Notes
-----
Uses max pooling.
"""
@lazy(allocation=['filter_size', 'num_filters', 'num_channels'])
def __init__(self, activation, filter_size, num_filters, pooling_size,
num_channels, conv_step=(1, 1), pooling_step=None,
batch_size=None, image_size=None, border_mode='valid',
**kwargs):
self.convolution = ConvolutionalActivation(activation, filter_size, num_filters, num_channels)
self.pooling = MaxPooling()
super(ConvolutionalLayer, self).__init__(
application_methods=[self.convolution.apply,
self.pooling.apply], **kwargs)
self.convolution.name = self.name + '_convolution'
self.pooling.name = self.name + '_pooling'
self.filter_size = filter_size
self.num_filters = num_filters
self.num_channels = num_channels
self.pooling_size = pooling_size
self.conv_step = conv_step
self.pooling_step = pooling_step
self.batch_size = batch_size
self.border_mode = border_mode
self.image_size = image_size
def _push_allocation_config(self):
for attr in ['filter_size', 'num_filters', 'num_channels',
'batch_size', 'border_mode', 'image_size']:
setattr(self.convolution, attr, getattr(self, attr))
self.convolution.step = self.conv_step
self.convolution._push_allocation_config()
if self.image_size is not None:
pooling_input_dim = self.convolution.get_dim('output')
else:
pooling_input_dim = None
self.pooling.input_dim = pooling_input_dim
self.pooling.pooling_size = self.pooling_size
self.pooling.step = self.pooling_step
self.pooling.batch_size = self.batch_size
def get_dim(self, name):
if name == 'input_':
return self.convolution.get_dim('input_')
if name == 'output':
return self.pooling.get_dim('output')
return super(ConvolutionalLayer, self).get_dim(name)
@property
def num_output_channels(self):
return self.num_filters
示例10: enumerate
cb = []
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 = []