一起使用两个theano函数
Using two theano functions together
如果我有类似的东西:
import theano.tensor as T
from theano import function
a = T.dscalar('a')
b = T.dscalar('b')
first_func = a * b
second_func = a - b
first = function([a, b], first_func)
second = function([a, b], second_func)
我想创建第三个函数 first_func(1,2) + second_func(3,4)
,有没有办法做到这一点并创建一个将这两个较小的函数作为输入传递的函数?
我想做类似的事情:
third_func = first(a, b) + second(a,b)
third = function([a, b], third_func)
但这不起作用。将我的函数分解为更小的函数的正确方法是什么?
我想分解函数的唯一方法是根据张量变量,而不是函数调用。这应该有效:
import theano.tensor as T
from theano import function
a = T.dscalar('a')
b = T.dscalar('b')
first_func = a * b
second_func = a - b
first = function([a, b], first_func)
second = function([a, b], second_func)
third_func = first_func + second_func
third = function([a, b], third_func)
third_func = first(a, b) + second(a,b)
不起作用,因为函数调用需要实数值,而 a
和 b
是 tensor/symbolic 变量。基本上应该用张量定义数学运算,然后使用函数来计算这些张量的值。
如果我有类似的东西:
import theano.tensor as T
from theano import function
a = T.dscalar('a')
b = T.dscalar('b')
first_func = a * b
second_func = a - b
first = function([a, b], first_func)
second = function([a, b], second_func)
我想创建第三个函数 first_func(1,2) + second_func(3,4)
,有没有办法做到这一点并创建一个将这两个较小的函数作为输入传递的函数?
我想做类似的事情:
third_func = first(a, b) + second(a,b)
third = function([a, b], third_func)
但这不起作用。将我的函数分解为更小的函数的正确方法是什么?
我想分解函数的唯一方法是根据张量变量,而不是函数调用。这应该有效:
import theano.tensor as T
from theano import function
a = T.dscalar('a')
b = T.dscalar('b')
first_func = a * b
second_func = a - b
first = function([a, b], first_func)
second = function([a, b], second_func)
third_func = first_func + second_func
third = function([a, b], third_func)
third_func = first(a, b) + second(a,b)
不起作用,因为函数调用需要实数值,而 a
和 b
是 tensor/symbolic 变量。基本上应该用张量定义数学运算,然后使用函数来计算这些张量的值。