On Sat, Oct 15, 2016, Pascal Lamblin wrote:
> Another option, still experimental, may be the `map_variables` function
> in scan_modules/scan_utils.
There seem to be some challenges regarding scalar constants with that
function, but I was able to do the following:
>>> theano.tensor.basic.constant.enable = False
>>> v = theano.tensor.lscalar('v')
>>> exp1 = 2 * v
>>>
exp1.name = 'exp1'
>>> exp2 = 4 * exp1
>>>
exp2.name = 'exp2'
>>> exp3 = 6 * exp2
>>>
exp3.name = 'exp3'
>>> exp4 = 8 * exp3
>>>
exp4.name = 'exp4'
>>> replace_dict = {'exp1': (3*exp1), 'exp2': (5*exp2), 'exp3': (7*exp3)}
>>> def replace(var):
... return replace_dict.get(
var.name, var)
>>> exp5, = theano.scan_module.scan_utils.map_variables(replace, [exp4])
>>> theano.printing.debugprint(exp5)
Elemwise{mul,no_inplace} [id A] 'exp4'
|TensorConstant{8} [id B]
|Elemwise{mul,no_inplace} [id C] ''
|TensorConstant{7} [id D]
|Elemwise{mul,no_inplace} [id C] ''
The issue is that it introduced a cycle in the graph: it replaced exp3
by 7*exp3, where exp3 is the new one...
I guess that illustrates the challenge of getting replacements right.