0

順伝播をTheanoでコーディングしてみました。クラス名 hiddenLayer を次のように定義します。

theano.tensor を theano から T としてインポート 共有をインポート theano から np として numpy をインポート インポート関数

class hiddenLayer():
    """ Hidden Layer class
    """
    def __init__(self, n_in, n_out):
        rng = np.random
        self.W = shared(np.asarray(rng.uniform(low=-np.sqrt(6. / (n_in + n_out)),
                                               high=np.sqrt(6. / (n_in + n_out)),
                                               size=(n_in, n_out)),
                                   dtype=T.config.floatX),
                        name='W')
        self.b = shared(np.zeros(n_out, dtype=T.config.floatX), name='b')
        self.x = T.dvector('x')
        self.a = T.tanh(T.dot(self.x, self.W) + self.b)
        self.W_sum = shared(np.zeros([n_in, n_out]), name='W_sum')
        self.gw = 0
        self.gb = 0

hiddenLayer オブジェクトのリストを設定したいのですが、現在の hiddenLayer は次の hiddenLayer の入力です。最後に、 forward buy という名前の関数を定義し、エラーが発生し、コードは次のようになります。

def init_network(n_in, n_out, sl, x, y):
    l = []
    for i in range(sl):
        l.append(hiddenLayer(n_in, n_out))
    for i in range(sl):
        if i == 0:
            l[i].x = x
        elif i < sl-1:
            l[i].x = l[i-1].a
        else:
            l[i].x = l[i-1].a
            y = l[i].a
    return x, y, l

x = T.dvector('x')
y = T.dvector('y')
x, y, l = init_network(3, 3, 3, x, y)
forward = function(inputs=[x], outputs=y)

エラーメッセージは次のとおりです。

theano.compile.function_module.UnusedInputError: theano.function was asked to create a function computing outputs given certain inputs, but the provided input variable at index 0 is not part of the computational graph needed to compute the outputs: x.
To make this error into a warning, you can pass the parameter on_unused_input='warn' to theano.function. To disable it completely, use on_unused_input='ignore'.

何が問題なのか、どのように解決すればよいのか教えていただけないでしょうか? ありがとう

4

1 に答える 1

1

問題は、2 番目のループで lx をオーバーライドすることです。そんなことはできません。self.x がinitで使用されると、それに基づく結果は、self.x の現在のインスタンスに基づきます。したがって、オーバーライドすると、新しい x で他のものを再作成しません。

initへの入力として x を渡す必要があります。なしの場合は作成します。これは最初のレイヤー用です。もう 1 つの場合は、前のレイヤー出力である必要があります。

def __init__(self, n_in, n_out, x=None):
   if x is not None:
      self.x = x
   else:
      x = T.dvector('x')
于 2014-05-14T17:29:04.027 に答える