本文整理汇总了Python中blocks.graph.ComputationGraph类的典型用法代码示例。如果您正苦于以下问题:Python ComputationGraph类的具体用法?Python ComputationGraph怎么用?Python ComputationGraph使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ComputationGraph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_cost_graph
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
示例2: get_updates
def get_updates(self, learning_rate, grads, lr_scalers):
"""Wraps the respective method of the wrapped learning rule.
Performs name-based input substitution for the monitored values.
Currently very hacky: the inputs from the gradients are typically
named `$ALGO[$SOURCE]` in PyLearn2, where `$ALGO` is the algorithm
name and `$SOURCE` is a source name from the data specification.
This convention is exploited to match them with the inputs of
monitoring values, whose input names are expected to match source
names.
"""
updates = self.learning_rule.get_updates(learning_rate, grads,
lr_scalers)
grad_inputs = ComputationGraph(list(grads.values())).dict_of_inputs()
for value, accumulator in zip(self.values, self.accumulators):
value_inputs = ComputationGraph(value).dict_of_inputs()
replace_dict = dict()
for name, input_ in value_inputs.items():
# See docstring to see how it works
grad_input = grad_inputs[unpack(
[n for n in grad_inputs
if n.endswith('[{}]'.format(name))],
singleton=True)]
replace_dict[input_] = tensor.unbroadcast(
grad_input, *range(grad_input.ndim))
updates[accumulator] = (
accumulator + theano.clone(value, replace_dict))
self._callback_called = True
updates.update(self.updates)
return updates
示例3: test_replace
def test_replace():
# Test if replace works with outputs
x = tensor.scalar()
y = x + 1
cg = ComputationGraph([y])
doubled_cg = cg.replace([(y, 2 * y)])
out_val = doubled_cg.outputs[0].eval({x: 2})
assert out_val == 6.0
示例4: test_snapshot
def test_snapshot():
x = tensor.matrix('x')
linear = MLP([Identity(), Identity()], [10, 10, 10],
weights_init=Constant(1), biases_init=Constant(2))
linear.initialize()
y = linear.apply(x)
cg = ComputationGraph(y)
snapshot = cg.get_snapshot(dict(x=numpy.zeros((1, 10), dtype=floatX)))
assert len(snapshot) == 14
示例5: test_replace_variable_not_in_graph
def test_replace_variable_not_in_graph():
# Test if warning appears when variable is not in graph
with warnings.catch_warnings(record=True) as w:
x = tensor.scalar()
y = x + 1
z = tensor.scalar()
cg = ComputationGraph([y])
cg.replace([(y, 2 * y), (z, 2 * z)])
assert len(w) == 1
assert "not a part of" in str(w[-1].message)
示例6: test_replace_variable_is_auxiliary
def test_replace_variable_is_auxiliary():
# Test if warning appears when variable is an AUXILIARY variable
with warnings.catch_warnings(record=True) as w:
x = tensor.scalar()
y = x + 1
add_role(y, AUXILIARY)
cg = ComputationGraph([y])
cg.replace([(y, 2 * y)])
assert len(w) == 1
assert "auxiliary" in str(w[-1].message)
示例7: __init__
def __init__(self, data_stream, variables, path=None, **kwargs):
self.data_stream = data_stream
self.variables = variables
self.path = path
self.prediction = None
kwargs.setdefault("after_training", True)
super(PredictDataStream, self).__init__(**kwargs)
cg = ComputationGraph(variables)
self.theano_function = cg.get_theano_function()
示例8: __init__
def __init__(self, generator, steps=320, n_samples = 10,
mean_data = 0, std_data = 1, sample_rate = 8000,
save_name = "sample_", **kwargs):
super(Speak, self).__init__(**kwargs)
steps = 300
sample = ComputationGraph(generator.generate(n_steps=steps,
batch_size=n_samples, iterate=True))
self.sample_fn = sample.get_theano_function()
self.mean_data = mean_data
self.std_data = std_data
self.sample_rate = sample_rate
self.save_name = save_name
示例9: test_computation_graph
def test_computation_graph():
x = tensor.matrix('x')
y = tensor.matrix('y')
z = x + y
z.name = 'z'
a = z.copy()
a.name = 'a'
b = z.copy()
b.name = 'b'
r = tensor.matrix('r')
cg = ComputationGraph([a, b])
assert set(cg.inputs) == {x, y}
assert set(cg.outputs) == {a, b}
assert set(cg.variables) == {x, y, z, a, b}
assert cg.variables[2] is z
assert ComputationGraph(a).inputs == cg.inputs
cg2 = cg.replace({z: r})
assert set(cg2.inputs) == {r}
assert set([v.name for v in cg2.outputs]) == {'a', 'b'}
W = theano.shared(numpy.zeros((3, 3),
dtype=theano.config.floatX))
cg3 = ComputationGraph([z + W])
assert set(cg3.shared_variables) == {W}
cg4 = ComputationGraph([W])
assert cg4.variables == [W]
w1 = W ** 2
cg5 = ComputationGraph([w1])
assert W in cg5.variables
assert w1 in cg5.variables
# Test scan
s, _ = theano.scan(lambda inp, accum: accum + inp,
sequences=x,
outputs_info=tensor.zeros_like(x[0]))
scan = s.owner.inputs[0].owner.op
cg6 = ComputationGraph(s)
assert cg6.scans == [scan]
assert all(v in cg6.scan_variables for v in scan.inputs + scan.outputs)
示例10: test_computation_graph
def test_computation_graph():
x = tensor.matrix('x')
y = tensor.matrix('y')
z = x + y
a = z.copy()
a.name = 'a'
b = z.copy()
b.name = 'b'
r = tensor.matrix('r')
cg = ComputationGraph([a, b])
assert set(cg.inputs) == {x, y}
assert set(cg.outputs) == {a, b}
assert set(cg.variables) == {x, y, z, a, b}
assert ComputationGraph(a).inputs == cg.inputs
cg2 = cg.replace({z: r})
assert set(cg2.inputs) == {r}
assert set([v.name for v in cg2.outputs]) == {'a', 'b'}
示例11: __init__
def __init__(self, variables, use_take_last=False):
_validate_variable_names(variables)
self.variables = variables
self.variable_names = [v.name for v in self.variables]
self.use_take_last = use_take_last
self._computation_graph = ComputationGraph(self.variables)
self.inputs = self._computation_graph.inputs
self._initialized = False
self._create_aggregators()
self._compile()
示例12: test_batchnorm_rolling
def test_batchnorm_rolling():
layer = BatchNormalization(
input_dim = 5, rolling_accumulate=True)
layer.initialize()
x = T.matrix()
x_val = np.ones((6, 5), dtype=theano.config.floatX)
x_val[0,0] = 10.0
y = layer.apply(x)
cg = ComputationGraph([y])
_func = cg.get_theano_function()
for i in range(100):
ret = _func(x_val)
u = layer.u.get_value()
assert_allclose(u[0], 1.58491838)
assert_allclose(u[1], 0.6339674)
s = layer.s.get_value()
assert_allclose(s[0], 7.13214684)
assert_allclose(s[1], 0.)
示例13: __init__
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):
raise ValueError("variables should have different names")
self._computation_graph = ComputationGraph(self.variables)
self.inputs = self._computation_graph.inputs
self._initialized = False
self._create_aggregators()
self._compile()
示例14: get_cost_graph
def get_cost_graph(self, batch=True,
prediction=None, prediction_mask=None):
if batch:
recordings = self.recordings
recordings_mask = self.recordings_mask
groundtruth = self.labels
groundtruth_mask = self.labels_mask
else:
recordings = self.single_recording[:, None, :]
recordings_mask = tensor.ones_like(recordings[:, :, 0])
groundtruth = self.single_transcription[:, None]
groundtruth_mask = None
if not prediction:
prediction = groundtruth
if not prediction_mask:
prediction_mask = groundtruth_mask
cost = self.cost(recordings, recordings_mask,
prediction, prediction_mask)
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
示例15: _get_bn_params
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