本文整理汇总了Python中blocks.bricks.conv.Convolutional.initialize方法的典型用法代码示例。如果您正苦于以下问题:Python Convolutional.initialize方法的具体用法?Python Convolutional.initialize怎么用?Python Convolutional.initialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类blocks.bricks.conv.Convolutional
的用法示例。
在下文中一共展示了Convolutional.initialize方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_convolutional
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import initialize [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)
示例2: test_tied_biases
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import initialize [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_untied_biases
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import initialize [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)
示例4: conv_layer
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import initialize [as 别名]
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])
示例5: test_no_input_size
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import initialize [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: Convolutional
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import initialize [as 别名]
from blocks.graph import ComputationGraph
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)
示例7: ComputationGraph
# 需要导入模块: from blocks.bricks.conv import Convolutional [as 别名]
# 或者: from blocks.bricks.conv.Convolutional import initialize [as 别名]
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()
hidden_to_output.initialize()
from fuel.datasets import MNIST
mnist = MNIST("train",)
from fuel.streams import DataStream
from fuel.schemes import SequentialScheme
from fuel.transformers import Flatten
data_stream = Flatten(DataStream.default_stream(
mnist,
iteration_scheme=SequentialScheme(mnist.num_examples, batch_size=256)))
from blocks.algorithms import GradientDescent, Scale
algorithm = GradientDescent(step_rule=None,cost=cost,params=cg.parameters)