8

以下に示すような簡単なコードがあります。

class testxx(object):
    def __init__(self, input):
        self.input = input
        self.output = T.sum(input)
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = np.float32)
classfier = testxx(a)
outxx = classfier.output
outxx = np.asarray(outxx, dtype = np.float32)

ただし、次のエラー情報が表示されます。

ValueError: setting an array element with a sequence.

さらに、theano.tensor の関数を使用すると、返されるのは "tensor" と呼ばれるようで、結果が行列のようになるはずなのに、単に numpy.array 型に切り替えることはできません。

それが私の質問です: outxx を numpy.array 型に切り替えるにはどうすればよいですか?

4

2 に答える 2

2

from ではなく from を使用testxxするため、おそらくnumpy 配列ではなく、入力として a を期待します。sum()theano.tensornumpyTensorVariable

=> に置き換えa = np.array(...)ますa = T.matrix(dtype=theano.config.floatX)

最後の行の前に、に依存するoutxxa になります。したがって、 の値を与えることで評価できます。TensorVariableaa

outxx = np.asarray(...)=> 最後の行を次の 2 行に置き換えます。

f = theano.function([a], outxx)
outxx = f(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = np.float32))

次のコードはエラーなしで実行されます。

import theano
import theano.tensor as T
import numpy as np

class testxx(object):
    def __init__(self, input):
        self.input = input
        self.output = T.sum(input)
a = T.matrix(dtype=theano.config.floatX)
classfier = testxx(a)
outxx = classfier.output
f = theano.function([a], outxx)
outxx = f(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = np.float32))

スカラーの追加に関するTheanoのドキュメントには、他の同様の例が示されています。

于 2015-08-07T17:22:07.730 に答える