替换theano图中表达式的变量

时间:2015-11-25 15:46:48

标签: python theano

给出以下代码:

func returnExampleArray() -> [TypeHere] {    
    return [.. objects of correct type here..]
}

是否可以在表达式 f 中用 z + A 替换 y ,因此它等同于 x + z + A + B ?可以通过在图表中搜索<​​em> y 的出现并用 z + A 替换它们来手动完成。但是,在API中使用更简单的高级方法似乎是合乎逻辑的。

1 个答案:

答案 0 :(得分:1)

这是discussed on the theano-users mailing list

您可以使用givens的{​​{1}}机制或使用theano.function

以下是一些示例代码:

theano.clone

请注意,必须调整theano函数的输入,以确保只接受未指定的张量作为输入(即import numpy as np import theano import theano.tensor as T x, y, z = T.dmatrices('x', 'y', 'z') A = theano.shared(np.random.rand(3, 4), borrow=True, name='A') B = theano.shared(np.random.rand(3, 4), borrow=True, name='B') h1 = x + y + B h2 = theano.clone(h1, {y: z + A}) f1 = theano.function([x, y], h1) f2 = theano.function([x, z], h2) f3 = theano.function([x, z], h1, givens={y: z + A}) a = np.random.randn(3, 4) b = np.random.randn(3, 4) print f1(a, b) print f2(a, b) print f3(a, b) 一旦被y函数替换后不再是输入)。