本文整理汇总了Python中blocks.graph.ComputationGraph方法的典型用法代码示例。如果您正苦于以下问题:Python graph.ComputationGraph方法的具体用法?Python graph.ComputationGraph怎么用?Python graph.ComputationGraph使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类blocks.graph
的用法示例。
在下文中一共展示了graph.ComputationGraph方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init_beam_search
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def init_beam_search(self, beam_size):
"""Compile beam search and set the beam size.
See Blocks issue #500.
"""
if hasattr(self, '_beam_search') and self.beam_size == beam_size:
# Only recompile if the user wants a different beam size
return
self.beam_size = beam_size
generated = self.get_generate_graph(use_mask=False, n_steps=3)
cg = ComputationGraph(generated.values())
samples, = VariableFilter(
applications=[self.generator.generate], name="outputs")(cg)
self._beam_search = BeamSearch(beam_size, samples)
self._beam_search.compile()
示例2: __init__
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def __init__(self, variables, use_take_last=False):
self.variables = variables
self.use_take_last = use_take_last
self.variable_names = [v.name for v in self.variables]
if len(set(self.variable_names)) < len(self.variables):
duplicates = []
for vname in set(self.variable_names):
if self.variable_names.count(vname) > 1:
duplicates.append(vname)
raise ValueError("variables should have different names!"
" Duplicates: {}".format(', '.join(duplicates)))
self._computation_graph = ComputationGraph(self.variables)
self.inputs = self._computation_graph.inputs
self._initialized = False
self._create_aggregators()
self._compile()
示例3: test_dataset_evaluators
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def test_dataset_evaluators():
X = theano.tensor.matrix('X')
brick = TestBrick(name='test_brick')
Y = brick.apply(X)
graph = ComputationGraph([Y])
monitor_variables = [v for v in graph.auxiliary_variables]
validator = DatasetEvaluator(monitor_variables)
data = [numpy.arange(1, 5, dtype=theano.config.floatX).reshape(2, 2),
numpy.arange(10, 16, dtype=theano.config.floatX).reshape(3, 2)]
data_stream = IterableDataset(dict(X=data)).get_example_stream()
values = validator.evaluate(data_stream)
assert values['test_brick_apply_V_squared'] == 4
numpy.testing.assert_allclose(
values['test_brick_apply_mean_row_mean'], numpy.vstack(data).mean())
per_batch_mean = numpy.mean([batch.mean() for batch in data])
numpy.testing.assert_allclose(
values['test_brick_apply_mean_batch_element'], per_batch_mean)
with assert_raises(Exception) as ar:
data_stream = IterableDataset(dict(X2=data)).get_example_stream()
validator.evaluate(data_stream)
assert "Not all data sources" in ar.exception.args[0]
示例4: test_saved_inner_graph
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def test_saved_inner_graph():
"""Make sure that the original inner graph is saved."""
x = tensor.tensor3()
recurrent = SimpleRecurrent(dim=3, activation=Tanh())
y = recurrent.apply(x)
application_call = get_application_call(y)
assert application_call.inner_inputs
assert application_call.inner_outputs
cg = ComputationGraph(application_call.inner_outputs)
# Check that the inner scan graph is annotated
# with `recurrent.apply`
assert len(VariableFilter(applications=[recurrent.apply])(cg)) == 3
# Check that the inner graph is equivalent to the one
# produced by a stand-alone of `recurrent.apply`
assert is_same_graph(application_call.inner_outputs[0],
recurrent.apply(*application_call.inner_inputs,
iterate=False))
示例5: setup_model
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def setup_model(p):
ladder = LadderAE(p)
# Setup inputs
input_type = TensorType('float32', [False] * (len(p.encoder_layers[0]) + 1))
x_only = input_type('features_unlabeled')
x = input_type('features_labeled')
y = theano.tensor.lvector('targets_labeled')
ladder.apply(x, y, x_only)
# Load parameters if requested
if p.get('load_from'):
with open(p.load_from + '/trained_params.npz') as f:
loaded = numpy.load(f)
cg = ComputationGraph([ladder.costs.total])
current_params = VariableFilter(roles=[PARAMETER])(cg.variables)
logger.info('Loading parameters: %s' % ', '.join(loaded.keys()))
for param in current_params:
assert param.get_value().shape == loaded[param.name].shape
param.set_value(loaded[param.name])
return ladder
示例6: get_costs
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def get_costs(self, probs, y, y_mask,
decay_cs=None, opt_rets=None):
"""
probs : dict, mapping cg_name to probabilities
y : theano tensor variable
y_mask : theano tensor variable
decay_cs : list of l2 regularization weights
opt_rets : dict, mapping cg_name to optional returned variables
"""
costs = self.decoder.costs(probs, y, y_mask)
if decay_cs is not None:
for name, cost in costs.iteritems():
if decay_cs[name] > 0.:
decay_c = theano.shared(numpy.float32(decay_cs[name]),
name='decay_c')
weight_decay = 0.
for pp in ComputationGraph(cost).parameters:
weight_decay += (pp ** 2).sum()
weight_decay *= decay_c
costs[name] += weight_decay
costs[name].name = name
return costs
示例7: get_cost_graph
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def get_cost_graph(self, batch=True,
prediction=None, prediction_mask=None):
if batch:
inputs = self.inputs
inputs_mask = self.inputs_mask
groundtruth = self.labels
groundtruth_mask = self.labels_mask
else:
inputs, inputs_mask = self.bottom.single_to_batch_inputs(
self.single_inputs)
groundtruth = self.single_labels[:, None]
groundtruth_mask = None
if not prediction:
prediction = groundtruth
if not prediction_mask:
prediction_mask = groundtruth_mask
cost = self.cost(inputs_mask=inputs_mask,
labels=prediction,
labels_mask=prediction_mask,
**inputs)
cost_cg = ComputationGraph(cost)
if self.criterion['name'].startswith("mse"):
placeholder, = VariableFilter(theano_name='groundtruth')(cost_cg)
cost_cg = cost_cg.replace({placeholder: groundtruth})
return cost_cg
示例8: init_generate
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def init_generate(self):
generated = self.get_generate_graph(use_mask=False)
cg = ComputationGraph(generated['outputs'])
self._do_generate = cg.get_theano_function()
示例9: _compile
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def _compile(self):
"""Compiles Theano functions.
.. todo::
The current compilation method does not account for updates
attached to `ComputationGraph` elements. Compiling should
be out-sourced to `ComputationGraph` to deal with it.
"""
inputs = []
outputs = []
updates = None
if self.theano_buffer.accumulation_updates:
updates = OrderedDict()
updates.update(self.theano_buffer.accumulation_updates)
inputs += self.theano_buffer.inputs
if self.updates:
# Handle the case in which we dont have any theano variables
# to evaluate but we do have MonitoredQuantity
# that may require an update of their own
if updates is None:
updates = self.updates
else:
updates.update(self.updates)
inputs += self.monitored_quantities_buffer.inputs
outputs = self.monitored_quantities_buffer.requires
if inputs != []:
self.unique_inputs = list(set(inputs))
self._accumulate_fun = theano.function(self.unique_inputs,
outputs,
updates=updates)
else:
self._accumulate_fun = None
示例10: __init__
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def __init__(self, beam_size, samples):
self.beam_size = beam_size
# Extracting information from the sampling computation graph
cg = ComputationGraph(samples)
self.inputs = cg.inputs
self.generator = get_brick(samples)
if not isinstance(self.generator, BaseSequenceGenerator):
raise ValueError
self.generate_call = get_application_call(samples)
if (not self.generate_call.application ==
self.generator.generate):
raise ValueError
self.inner_cg = ComputationGraph(self.generate_call.inner_outputs)
# Fetching names from the sequence generator
self.context_names = self.generator.generate.contexts
self.state_names = self.generator.generate.states
# Parsing the inner computation graph of sampling scan
self.contexts = [
VariableFilter(bricks=[self.generator],
name=name,
roles=[INPUT])(self.inner_cg)[0]
for name in self.context_names]
self.input_states = []
# Includes only those state names that were actually used
# in 'generate'
self.input_state_names = []
for name in self.generator.generate.states:
var = VariableFilter(
bricks=[self.generator], name=name,
roles=[INPUT])(self.inner_cg)
if var:
self.input_state_names.append(name)
self.input_states.append(var[0])
self.compiled = False
示例11: __init__
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def __init__(self, cost, parameters):
self.cost = cost
self.parameters = parameters
self._cost_computation_graph = ComputationGraph(self.cost)
self._updates = []
示例12: test_variable_filter_roles_error
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def test_variable_filter_roles_error():
# Creating computation graph
brick1 = Linear(input_dim=2, output_dim=2, name='linear1')
x = tensor.vector()
h1 = brick1.apply(x)
cg = ComputationGraph(h1)
# testing role error
VariableFilter(roles=PARAMETER)(cg.variables)
示例13: test_variable_filter_applications_error
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def test_variable_filter_applications_error():
# Creating computation graph
brick1 = Linear(input_dim=2, output_dim=2, name='linear1')
x = tensor.vector()
h1 = brick1.apply(x)
cg = ComputationGraph(h1)
VariableFilter(applications=brick1.apply)(cg.variables)
示例14: test_many_steps
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def test_many_steps(self):
x = tensor.tensor3('x')
mask = tensor.matrix('mask')
h = self.simple.apply(x, mask=mask, iterate=True)
calc_h = theano.function(inputs=[x, mask], outputs=[h])
x_val = 0.1 * numpy.asarray(list(itertools.permutations(range(4))),
dtype=theano.config.floatX)
x_val = numpy.ones((24, 4, 3),
dtype=theano.config.floatX) * x_val[..., None]
mask_val = numpy.ones((24, 4), dtype=theano.config.floatX)
mask_val[12:24, 3] = 0
h_val = numpy.zeros((25, 4, 3), dtype=theano.config.floatX)
for i in range(1, 25):
h_val[i] = numpy.tanh(h_val[i - 1].dot(
2 * numpy.ones((3, 3))) + x_val[i - 1])
h_val[i] = (mask_val[i - 1, :, None] * h_val[i] +
(1 - mask_val[i - 1, :, None]) * h_val[i - 1])
h_val = h_val[1:]
assert_allclose(h_val, calc_h(x_val, mask_val)[0], rtol=1e-04)
# Also test that initial state is a parameter
initial_state, = VariableFilter(roles=[INITIAL_STATE])(
ComputationGraph(h))
assert is_shared_variable(initial_state)
assert initial_state.name == 'initial_state'
示例15: _get_bn_params
# 需要导入模块: from blocks import graph [as 别名]
# 或者: from blocks.graph import ComputationGraph [as 别名]
def _get_bn_params(self, output_vars):
# Pick out the nodes with batch normalization vars
cg = ComputationGraph(output_vars)
var_filter = VariableFilter(roles=[BNPARAM])
bn_ps = var_filter(cg.variables)
if len(bn_ps) == 0:
logger.warn('No batch normalization parameters found - is' +
' batch normalization turned off?')
self._bn = False
self._counter = None
self._counter_max = None
bn_share = []
output_vars_replaced = output_vars
else:
self._bn = True
assert len(set([p.name for p in bn_ps])) == len(bn_ps), \
'Some batch norm params have the same name'
logger.info('Batch norm parameters: %s' % ', '.join([p.name for p in bn_ps]))
# Filter out the shared variables from the model updates
def filter_share(par):
lst = [up for up in cg.updates if up.name == 'shared_%s' % par.name]
assert len(lst) == 1
return lst[0]
bn_share = map(filter_share, bn_ps)
# Replace the BN coefficients in the test data model - Replace the
# theano variables in the test graph with the shareds
output_vars_replaced = cg.replace(zip(bn_ps, bn_share)).outputs
# Pick out the counter
self._counter = self._param_from_updates(cg.updates, 'counter')
self._counter_max = self._param_from_updates(cg.updates, 'counter_max')
return bn_ps, bn_share, output_vars_replaced