本文整理汇总了Python中blocks.graph.ComputationGraph.replace方法的典型用法代码示例。如果您正苦于以下问题:Python ComputationGraph.replace方法的具体用法?Python ComputationGraph.replace怎么用?Python ComputationGraph.replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类blocks.graph.ComputationGraph
的用法示例。
在下文中一共展示了ComputationGraph.replace方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_replace_variable_is_auxiliary
# 需要导入模块: from blocks.graph import ComputationGraph [as 别名]
# 或者: from blocks.graph.ComputationGraph import replace [as 别名]
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)
示例2: test_replace_variable_not_in_graph
# 需要导入模块: from blocks.graph import ComputationGraph [as 别名]
# 或者: from blocks.graph.ComputationGraph import replace [as 别名]
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)
示例3: get_cost_graph
# 需要导入模块: from blocks.graph import ComputationGraph [as 别名]
# 或者: from blocks.graph.ComputationGraph import replace [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
示例4: test_replace
# 需要导入模块: from blocks.graph import ComputationGraph [as 别名]
# 或者: from blocks.graph.ComputationGraph import replace [as 别名]
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
示例5: test_computation_graph
# 需要导入模块: from blocks.graph import ComputationGraph [as 别名]
# 或者: from blocks.graph.ComputationGraph import replace [as 别名]
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)
示例6: test_computation_graph
# 需要导入模块: from blocks.graph import ComputationGraph [as 别名]
# 或者: from blocks.graph.ComputationGraph import replace [as 别名]
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'}
示例7: get_cost_graph
# 需要导入模块: from blocks.graph import ComputationGraph [as 别名]
# 或者: from blocks.graph.ComputationGraph import replace [as 别名]
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
示例8: _get_bn_params
# 需要导入模块: from blocks.graph import ComputationGraph [as 别名]
# 或者: from blocks.graph.ComputationGraph import replace [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
示例9: apply_adaptive_noise
# 需要导入模块: from blocks.graph import ComputationGraph [as 别名]
# 或者: from blocks.graph.ComputationGraph import replace [as 别名]
def apply_adaptive_noise(computation_graph,
cost,
variables,
num_examples,
parameters=None,
init_sigma=1e-6,
model_cost_coefficient=1.0,
seed=None,
gradients=None,
):
"""Add adaptive noise to parameters of a model.
Each of the given variables will be replaced by a normal
distribution with learned mean and standard deviation.
A model cost is computed based on the precision of the the distributions
associated with each variable. It is added to the given cost used to
train the model.
See: A. Graves "Practical Variational Inference for Neural Networks",
NIPS 2011
Parameters
----------
computation_graph : instance of :class:`ComputationGraph`
The computation graph.
cost : :class:`~tensor.TensorVariable`
The cost without weight noise. It should be a member of the
computation_graph.
variables : :class:`~tensor.TensorVariable`
Variables to add noise to.
num_examples : int
Number of training examples. The cost of the model is divided by
the number of training examples, please see
A. Graves "Practical Variational Inference for Neural Networks"
for justification
parameters : list of :class:`~tensor.TensorVariable`
parameters of the model, if gradients are given the list will not
be used. Otherwise, it will be used to compute the gradients
init_sigma : float,
initial standard deviation of noise variables
model_cost_coefficient : float,
the weight of the model cost
seed : int, optional
The seed with which
:class:`~theano.sandbox.rng_mrg.MRG_RandomStreams` is initialized,
is set to 1 by default.
gradients : dict, optional
Adaptive weight noise introduces new parameters for which new cost
and gradients must be computed. Unless the gradients paramter is
given, it will use theano.grad to get the gradients
Returns
-------
cost : :class:`~tensor.TensorVariable`
The new cost
computation_graph : instance of :class:`ComputationGraph`
new graph with added noise.
gradients : dict
a dictionary of gradients for all parameters: the original ones
and the adaptive noise ones
noise_brick : :class:~lvsr.graph.NoiseBrick
the brick that holds all noise parameters and whose .apply method
can be used to find variables added by adaptive noise
"""
if not seed:
seed = config.default_seed
rng = MRG_RandomStreams(seed)
try:
cost_index = computation_graph.outputs.index(cost)
except ValueError:
raise ValueError("cost is not part of the computation_graph")
if gradients is None:
if parameters is None:
raise ValueError("Either gradients or parameters must be given")
logger.info("Taking the cost gradient")
gradients = dict(equizip(parameters,
tensor.grad(cost, parameters)))
else:
if parameters is not None:
logger.warn("Both gradients and parameters given, will ignore"
"parameters")
parameters = gradients.keys()
gradients = OrderedDict(gradients)
log_sigma_scale = 2048.0
P_noisy = variables # We will add noise to these
Beta = [] # will hold means, log_stdev and stdevs
P_with_noise = [] # will hold parames with added noise
# These don't change
P_clean = list(set(parameters).difference(P_noisy))
noise_brick = NoiseBrick()
for p in P_noisy:
#.........这里部分代码省略.........