我想计算sumproduct
Theano 中的两个数组。两个数组都被声明为共享变量,并且是先前计算的结果。阅读教程,我发现了如何使用扫描来计算我想要使用“正常”张量数组的内容,但是当我尝试将代码调整为共享数组时,我收到了错误消息TypeError: function() takes at least 1 argument (1 given)
。(请参阅下面的最小运行代码示例)
我的代码中的错误在哪里?我的误解在哪里?我也愿意采用不同的方法来解决我的问题。
一般来说,我更喜欢直接采用共享变量的版本,因为在我的理解中,首先将数组转换回 Numpy 数组,然后再将它们传递给 Theano,会很浪费。
使用共享变量生成sumproduct
代码的错误消息:
import theano
import theano.tensor as T
import numpy
a1 = [1,2,4]
a2 = [3,4,5]
Ta1_shared = theano.shared(numpy.array(a1))
Ta2_shared = theano.shared(numpy.array(a2))
outputs_info = T.as_tensor_variable(numpy.asarray(0, 'float64'))
Tsumprod_result, updates = theano.scan(fn=lambda Ta1_shared, Ta2_shared, prior_value:
prior_value + Ta1_shared * Ta2_shared,
outputs_info=outputs_info,
sequences=[Ta1_shared, Ta2_shared])
Tsumprod_result = Tsumprod_result[-1]
Tsumprod = theano.function(outputs=Tsumprod_result)
print Tsumprod()
错误信息:
TypeError: function() takes at least 1 argument (1 given)
使用非共享变量的工作sumproduct
代码:
import theano
import theano.tensor as T
import numpy
a1 = [1, 2, 4]
a2 = [3, 4, 5]
Ta1 = theano.tensor.vector("a1")
Ta2 = theano.tensor.vector("coefficients")
outputs_info = T.as_tensor_variable(numpy.asarray(0, 'float64'))
Tsumprod_result, updates = theano.scan(fn=lambda Ta1, Ta2, prior_value:
prior_value + Ta1 * Ta2,
outputs_info=outputs_info,
sequences=[Ta1, Ta2])
Tsumprod_result = Tsumprod_result[-1]
Tsumprod = theano.function(inputs=[Ta1, Ta2], outputs=Tsumprod_result)
print Tsumprod(a1, a2)