2016-12-01 6 views
0

私はTheanoに組み込まれたExpmGrad関数を使用しようとしています。 しかし、ExpmGradのうちtheano.functionを定義すると、出力がtheano変数でなければならないというエラーが表示されます。Theanoのslinalg.ExpmGradから関数を定義できません

このExpmGrad機能を使用する正しい方法が正確にわからないため、オンラインで使用例が見つかりませんでした。

import theano 
from theano.tensor import T 
J1 = T.dscalar('J1') 
H = np.arange(16).reshape(4, 4) * J1 
gJ = theano.tensor.slinalg.ExpmGrad(H) 
f = theano.function([J1], gJ) 

、これは私が取得エラーです:

これは私が試したものです

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-122-2e2976e72a77> in <module>() 
     4 # gJ = theano.gradient.jacobian(H[0], J1) 
     5 gJ = theano.tensor.slinalg.ExpmGrad(H) 
----> 6 f = theano.function([J1], gJ) 

//anaconda/lib/python3.5/site-packages/theano/compile/function.py in function(inputs, outputs, mode, updates, givens, no_default_updates, accept_inplace, name, rebuild_strict, allow_input_downcast, profile, on_unused_input) 
    318     on_unused_input=on_unused_input, 
    319     profile=profile, 
--> 320     output_keys=output_keys) 
    321  # We need to add the flag check_aliased inputs if we have any mutable or 
    322  # borrowed used defined inputs 

//anaconda/lib/python3.5/site-packages/theano/compile/pfunc.py in pfunc(params, outputs, mode, updates, givens, no_default_updates, accept_inplace, name, rebuild_strict, allow_input_downcast, profile, on_unused_input, output_keys) 
    440           rebuild_strict=rebuild_strict, 
    441           copy_inputs_over=True, 
--> 442           no_default_updates=no_default_updates) 
    443  # extracting the arguments 
    444  input_variables, cloned_extended_outputs, other_stuff = output_vars 

//anaconda/lib/python3.5/site-packages/theano/compile/pfunc.py in rebuild_collect_shared(outputs, inputs, replace, updates, rebuild_strict, copy_inputs_over, no_default_updates) 
    225     raise TypeError('Outputs must be theano Variable or ' 
    226         'Out instances. Received ' + str(v) + 
--> 227         ' of type ' + str(type(v))) 
    228    # computed_list.append(cloned_v) 
    229  else: 

TypeError: Outputs must be theano Variable or Out instances. Received ExpmGrad of type <class 'theano.tensor.slinalg.ExpmGrad'> 

は私が間違って何をしているのですか?

答えて

1

ExpmGradは関数ではありません。theano.Opのサブクラスです。クラスを呼び出すことは、あなたが望む結果ではなく、Opインスタンスを作成するだけです。

それを適切に使用するには、あなたがそれを利用するためにファンクタとしてオペアンプをインスタンス化する必要があります上記のコードの場合

expm_grad = theano.tensor.slinalg.ExpmGrad() 
gJ = expm_grad(H, gw) 

を、あなたは正しく上流勾配であるgw引数を定義する必要があります。

注:勾配オペアンプは通常、直接使用するように設計されていません、間接的にそれを

J1 = T.dscalar('J1') 
H = np.arange(16).reshape(4, 4) * J1 
expH = theano.tensor.slinalg.expm(H) 
e = some_scalar_function(expH) 
gJ = theano.grad(e, J1) 
f = theano.function([J1], gJ) 
を使用するように theano.gradを使用することをお勧めします