本文整理汇总了Python中blocks.initialization.Constant方法的典型用法代码示例。如果您正苦于以下问题:Python initialization.Constant方法的具体用法?Python initialization.Constant怎么用?Python initialization.Constant使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类blocks.initialization
的用法示例。
在下文中一共展示了initialization.Constant方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_convolutional
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [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.),
biases_init=Constant(5.))
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_convolutional_transpose
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def test_convolutional_transpose():
x = tensor.tensor4('x')
num_channels = 4
num_filters = 3
image_size = (8, 6)
original_image_size = (17, 13)
batch_size = 5
filter_size = (3, 3)
step = (2, 2)
conv = ConvolutionalTranspose(
original_image_size, filter_size, num_filters, num_channels, step=step,
image_size=image_size, weights_init=Constant(1.),
biases_init=Constant(5.))
conv.initialize()
y = conv.apply(x)
func = function([x], y)
x_val = numpy.ones((batch_size, num_channels) + image_size,
dtype=theano.config.floatX)
expected_value = num_channels * numpy.ones(
(batch_size, num_filters) + original_image_size)
expected_value[:, :, 2:-2:2, :] += num_channels
expected_value[:, :, :, 2:-2:2] += num_channels
expected_value[:, :, 2:-2:2, 2:-2:2] += num_channels
assert_allclose(func(x_val), expected_value + 5)
示例3: test_no_input_size
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [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)
示例4: test_linear
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def test_linear():
x = tensor.matrix()
linear = Linear(input_dim=16, output_dim=8, weights_init=Constant(2),
biases_init=Constant(1))
y = linear.apply(x)
linear.initialize()
x_val = numpy.ones((4, 16), dtype=theano.config.floatX)
assert_allclose(
y.eval({x: x_val}),
x_val.dot(2 * numpy.ones((16, 8))) + numpy.ones((4, 8)))
linear = Linear(input_dim=16, output_dim=8, weights_init=Constant(2),
use_bias=False)
y = linear.apply(x)
linear.initialize()
x_val = numpy.ones((4, 16), dtype=theano.config.floatX)
assert_allclose(y.eval({x: x_val}), x_val.dot(2 * numpy.ones((16, 8))))
示例5: test_mlp
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def test_mlp():
x = tensor.matrix()
x_val = numpy.random.rand(2, 16).astype(theano.config.floatX)
mlp = MLP(activations=[Tanh(), None], dims=[16, 8, 4],
weights_init=Constant(1), biases_init=Constant(1))
y = mlp.apply(x)
mlp.initialize()
assert_allclose(
numpy.tanh(x_val.dot(numpy.ones((16, 8))) + numpy.ones((2, 8))).dot(
numpy.ones((8, 4))) + numpy.ones((2, 4)),
y.eval({x: x_val}), rtol=1e-06)
mlp = MLP(activations=[None], weights_init=Constant(1), use_bias=False)
mlp.dims = [16, 8]
y = mlp.apply(x)
mlp.initialize()
assert_allclose(x_val.dot(numpy.ones((16, 8))),
y.eval({x: x_val}), rtol=1e-06)
assert mlp.rng == mlp.linear_transformations[0].rng
示例6: test_mlp_apply
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def test_mlp_apply():
x = tensor.matrix()
x_val = numpy.random.rand(2, 16).astype(theano.config.floatX)
mlp = MLP(activations=[Tanh().apply, None], dims=[16, 8, 4],
weights_init=Constant(1), biases_init=Constant(1))
y = mlp.apply(x)
mlp.initialize()
assert_allclose(
numpy.tanh(x_val.dot(numpy.ones((16, 8))) + numpy.ones((2, 8))).dot(
numpy.ones((8, 4))) + numpy.ones((2, 4)),
y.eval({x: x_val}), rtol=1e-06)
mlp = MLP(activations=[None], weights_init=Constant(1), use_bias=False)
mlp.dims = [16, 8]
y = mlp.apply(x)
mlp.initialize()
assert_allclose(x_val.dot(numpy.ones((16, 8))),
y.eval({x: x_val}), rtol=1e-06)
assert mlp.rng == mlp.linear_transformations[0].rng
示例7: test_sequence_variable_outputs
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def test_sequence_variable_outputs():
x = tensor.matrix()
linear_1 = Linear(input_dim=16, output_dim=8, weights_init=Constant(2),
biases_init=Constant(1))
fork = Fork(input_dim=8, output_names=['linear_2_1', 'linear_2_2'],
output_dims=[4, 5], prototype=Linear(),
weights_init=Constant(3), biases_init=Constant(4))
sequence = Sequence([linear_1.apply, fork.apply])
sequence.initialize()
y_1, y_2 = sequence.apply(x)
x_val = numpy.ones((4, 16), dtype=theano.config.floatX)
assert_allclose(
y_1.eval({x: x_val}),
(x_val.dot(2 * numpy.ones((16, 8))) + numpy.ones((4, 8))).dot(
3 * numpy.ones((8, 4))) + 4 * numpy.ones((4, 4)))
assert_allclose(
y_2.eval({x: x_val}),
(x_val.dot(2 * numpy.ones((16, 8))) + numpy.ones((4, 8))).dot(
3 * numpy.ones((8, 5))) + 4 * numpy.ones((4, 5)))
示例8: test_sequence_variable_inputs
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def test_sequence_variable_inputs():
x, y = tensor.matrix(), tensor.matrix()
parallel_1 = Parallel(input_names=['input_1', 'input_2'],
input_dims=[4, 5], output_dims=[3, 2],
prototype=Linear(), weights_init=Constant(2),
biases_init=Constant(1))
parallel_2 = Parallel(input_names=['input_1', 'input_2'],
input_dims=[3, 2], output_dims=[5, 4],
prototype=Linear(), weights_init=Constant(2),
biases_init=Constant(1))
sequence = Sequence([parallel_1.apply, parallel_2.apply])
sequence.initialize()
new_x, new_y = sequence.apply(x, y)
x_val = numpy.ones((4, 4), dtype=theano.config.floatX)
y_val = numpy.ones((4, 5), dtype=theano.config.floatX)
assert_allclose(
new_x.eval({x: x_val}),
(x_val.dot(2 * numpy.ones((4, 3))) + numpy.ones((4, 3))).dot(
2 * numpy.ones((3, 5))) + numpy.ones((4, 5)))
assert_allclose(
new_y.eval({y: y_val}),
(y_val.dot(2 * numpy.ones((5, 2))) + numpy.ones((4, 2))).dot(
2 * numpy.ones((2, 4))) + numpy.ones((4, 4)))
示例9: test_compute_weights_with_zero_mask
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def test_compute_weights_with_zero_mask():
state_dim = 2
attended_dim = 3
match_dim = 4
attended_length = 5
batch_size = 6
attention = SequenceContentAttention(
state_names=["states"], state_dims=[state_dim],
attended_dim=attended_dim, match_dim=match_dim,
weights_init=IsotropicGaussian(0.5),
biases_init=Constant(0))
attention.initialize()
energies = tensor.as_tensor_variable(
numpy.random.rand(attended_length, batch_size))
mask = tensor.as_tensor_variable(
numpy.zeros((attended_length, batch_size)))
weights = attention.compute_weights(energies, mask).eval()
assert numpy.all(numpy.isfinite(weights))
示例10: test_constant
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def test_constant():
def check_constant(const, shape, ground_truth):
# rng unused, so pass None.
init = Constant(const).generate(None, ground_truth.shape)
assert ground_truth.dtype == theano.config.floatX
assert ground_truth.shape == init.shape
assert_equal(ground_truth, init)
# Test scalar init.
yield (check_constant, 5, (5, 5),
5 * numpy.ones((5, 5), dtype=theano.config.floatX))
# Test broadcasting.
yield (check_constant, [1, 2, 3], (7, 3),
numpy.array([[1, 2, 3]] * 7, dtype=theano.config.floatX))
yield (check_constant, numpy.array([[1], [2], [3]]), (3, 2),
numpy.array([[1, 1], [2, 2], [3, 3]], dtype=theano.config.floatX))
示例11: test_sparse
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def test_sparse():
rng = numpy.random.RandomState(1)
def check_sparse(rng, num_init, weights_init, sparse_init, shape, total):
weights = Sparse(num_init=num_init, weights_init=weights_init,
sparse_init=sparse_init).generate(rng, shape)
assert weights.shape == shape
assert weights.dtype == theano.config.floatX
if sparse_init is None:
if isinstance(num_init, numbers.Integral):
assert (numpy.count_nonzero(weights) <=
weights.size - num_init * weights.shape[0])
else:
assert (numpy.count_nonzero(weights) <=
weights.size - num_init * weights.shape[1])
if total is not None:
assert numpy.sum(weights) == total
yield check_sparse, rng, 5, Constant(1.), None, (10, 10), None
yield check_sparse, rng, 0.5, Constant(1.), None, (10, 10), None
yield check_sparse, rng, 0.5, Constant(1.), Constant(1.), (10, 10), None
yield check_sparse, rng, 3, Constant(1.), None, (10, 10), 30
yield check_sparse, rng, 3, Constant(0.), Constant(1.), (10, 10), 70
yield check_sparse, rng, 0.3, Constant(1.), None, (10, 10), 30
yield check_sparse, rng, 0.3, Constant(0.), Constant(1.), (10, 10), 70
示例12: test_apply_batch_normalization_nested
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def test_apply_batch_normalization_nested():
x = tensor.matrix()
eps = 1e-8
batch_dims = (3, 9)
bn = BatchNormalization(input_dim=5, epsilon=eps)
mlp = MLP([Sequence([bn.apply, Tanh().apply])], [9, 5],
weights_init=Constant(0.4), biases_init=Constant(1))
mlp.initialize()
y = mlp.apply(x)
cg = apply_batch_normalization(ComputationGraph([y]))
y_bn = cg.outputs[0]
rng = numpy.random.RandomState((2016, 1, 18))
x_ = rng.uniform(size=batch_dims).astype(theano.config.floatX)
y_ = y_bn.eval({x: x_})
W_, b_ = map(lambda s: (getattr(mlp.linear_transformations[0], s)
.get_value(borrow=True)), ['W', 'b'])
z_ = numpy.dot(x_, W_) + b_
y_expected = numpy.tanh((z_ - z_.mean(axis=0)) /
numpy.sqrt(z_.var(axis=0) + eps))
assert_allclose(y_, y_expected, rtol=1e-3)
示例13: initialize
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def initialize(to_init):
for bricks in to_init:
bricks.weights_init = initialization.Uniform(width=0.08)
bricks.biases_init = initialization.Constant(0)
bricks.initialize()
示例14: __init__
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def __init__(self, num_channels, num_filters, spatial_width, num_scales, filter_size, downsample_method='meanout', name=""):
"""
A brick implementing a single layer in a multi-scale convolutional network.
"""
super(MultiScaleConvolution, self).__init__()
self.num_scales = num_scales
self.filter_size = filter_size
self.num_filters = num_filters
self.spatial_width = spatial_width
self.downsample_method = downsample_method
self.children = []
print "adding MultiScaleConvolution layer"
# for scale in range(self.num_scales-1, -1, -1):
for scale in range(self.num_scales):
print "scale %d"%scale
conv_layer = ConvolutionalActivation(activation=conv_nonlinearity.apply,
filter_size=(filter_size,filter_size), num_filters=num_filters,
num_channels=num_channels, image_size=(spatial_width/2**scale, spatial_width/2**scale),
# assume images are spatially smooth -- in which case output magnitude scales with
# # filter pixels rather than square root of # filter pixels, so initialize
# accordingly.
weights_init=IsotropicGaussian(std=np.sqrt(1./(num_filters))/filter_size**2),
biases_init=Constant(0), border_mode='full', name=name+"scale%d"%scale)
self.children.append(conv_layer)
示例15: test_batch_normalization_inside_convolutional_sequence
# 需要导入模块: from blocks import initialization [as 别名]
# 或者: from blocks.initialization import Constant [as 别名]
def test_batch_normalization_inside_convolutional_sequence():
"""Test that BN bricks work in ConvolutionalSequences."""
conv_seq = ConvolutionalSequence(
[Convolutional(filter_size=(3, 3), num_filters=4),
BatchNormalization(broadcastable=(False, True, True)),
AveragePooling(pooling_size=(2, 2)),
BatchNormalization(broadcastable=(False, False, False)),
MaxPooling(pooling_size=(2, 2), step=(1, 1))],
weights_init=Constant(1.),
biases_init=Constant(2.),
image_size=(10, 8), num_channels=9)
conv_seq_no_bn = ConvolutionalSequence(
[Convolutional(filter_size=(3, 3), num_filters=4),
AveragePooling(pooling_size=(2, 2)),
MaxPooling(pooling_size=(2, 2), step=(1, 1))],
weights_init=Constant(1.),
biases_init=Constant(2.),
image_size=(10, 8), num_channels=9)
conv_seq.initialize()
conv_seq_no_bn.initialize()
rng = numpy.random.RandomState((2015, 12, 17))
input_ = random_unif(rng, (2, 9, 10, 8))
x = theano.tensor.tensor4()
ybn = conv_seq.apply(x)
y = conv_seq_no_bn.apply(x)
yield (assert_equal, ybn.eval({x: input_}), y.eval({x: input_}))
std = conv_seq.children[-2].population_stdev
std.set_value(3 * std.get_value(borrow=True))
yield (assert_equal, ybn.eval({x: input_}), y.eval({x: input_}) / 3.)