2016-03-24 8 views
0

私はTheanoと機械学習に慣れています。そのために、線形回帰を計算したいと思います。私のコードは、Theanoの紹介からlogistic regression exampleに触発されています。Theano - Dimension Mis-matchを使った線形回帰

ValueError: Input dimension mis-match. (input[0].shape[0] = 1, input[1].shape[0] = 50) 
Apply node that caused the error: Elemwise{Composite{((-i0) + i1)}}[(0, 1)](b, CGemv{no_inplace}.0) 
Inputs types: [TensorType(float64, vector), TensorType(float64, vector)] 
Inputs shapes: [(1,), (50,)] 
Inputs strides: [(8,), (8,)] 
Inputs values: [array([ 0.]), 'not shown'] 

HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'. 
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node. 

私はエラーがサンプルデータがであることに関係している疑いがある:私は、コードを実行すると残念ながら

import numpy 
    import theano 
    import theano.tensor as T 

    class LinearRegression(object): 
     """ Calculate Linear Regression """ 

     def __init__(self, input): 
      """ Initialize the parameters of the logistic regression 

      Parameters: 
      ----------- 

      :type input: theano.tensor.TensorType 
      :param input: symbolic variable that describes the input of the 
          architecture (one minibatch) 
      """ 
      self.W = theano.shared(
       value=numpy.zeros(1, dtype=theano.config.floatX), 
       name='W', borrow=True 
      ) 

      self.b = theano.shared(
       value=numpy.zeros(1, dtype=theano.config.floatX), 
       name='b', borrow=True 
      ) 

      self.y_pred = T.dot(input, self.W) + self.b 

     def errors(self, y): 
      """ The squared distance 

      Parameters: 
      ---------- 

      :y input: array_like: 
      :param input: the sample data 

      """ 
      errors = y- self.y_pred 
      return T.sum(T.pow(errors, 2)) 


    def sgd_optimization(learning_rate=0.0013, n_epochs=100): 
     """ 
     Demonstrate stochastic gradient descent optimization of a linear model 

     Parameters: 
     ----- 
     :type learning_rate: float 
     :param learning_rate: learning rate used (factor for the stochastic 
           gradient) 

     :type n_epochs: int 
     :param n_epochs: maximal number of epochs to run the optimizer 
     """ 
     x_train = numpy.random.uniform(low=-2, high = 2, size=(50,1)) 
     epsilon = numpy.random.normal(scale=0.01, size=50) 
     y_train = numpy.squeeze(2*x_train) + epsilon 

     costs = [] 
     eta0, x, y = T.scalar('eta0'), T.matrix(name='x'), T.vector(name='y') 

     classifier = LinearRegression(input = x) 
     cost = classifier.errors(y) 
     g_W = T.grad(cost=cost, wrt=classifier.W) 
     g_b = T.grad(cost=cost, wrt=classifier.b) 
     update = [(classifier.W, classifier.W - eta0 * g_W), 
        (classifier.b, classifier.b - eta0 * g_b)] 

     train = theano.function(inputs = [eta0], 
           outputs = cost, 
           updates = update, 
           givens = {x: x_train, y: y_train}) 

     for _ in range(n_epochs): 
      costs.append(train(learning_rate)) 

     return costs, w 

    SSE, regressor = sgd_optimization() 

は、Pythonは、次のエラーメッセージを返します。

は、私は次のコードを書きました(50,1)と(1,1)の次元の回帰因子のみである。それにもかかわらず、私はしばらくしてから私のコードのエラーを修正することに失敗しました。誰かが間違いを修正するためのヒントを提供することができますか?私はどんな助けにも感謝しています!

答えて

1

あなたはbをブロードキャストする必要があります:私はTheanoはこれを自動的に行うことを期待する

self.y_pred = T.dot(input, self.W) + self.b[:, None] 

が、これがそうでないようです。


は、エラーメッセージの提案に従って問題を特定し、高い例外冗長でTheanoを実行するには

$ THEANO_FLAGS='exception_verbosity=high' python path/to/script.py 

これは、そのオペランドと一緒に問題のあるノードを含む出力のかなり多くを生産

Debugprint of the apply node: 
Elemwise{Composite{((-i0) + i1)}}[(0, 1)] [@A] <TensorType(float64, vector)> '' 
|b [@B] <TensorType(float64, vector)> 
|CGemv{no_inplace} [@C] <TensorType(float64, vector)> '' 
    |<TensorType(float64, vector)> [@D] <TensorType(float64, vector)> 
    |TensorConstant{-1.0} [@E] <TensorType(float64, scalar)> 
    |<TensorType(float64, matrix)> [@F] <TensorType(float64, matrix)> 
    |W [@G] <TensorType(float64, vector)> 
    |TensorConstant{1.0} [@H] <TensorType(float64, scalar)> 

ノードは、テンポラリノードCGemv{no_inplace}からのbの減算に対応します。 bを含む唯一のコード行は、

self.y_pred = T.dot(input, self.W) + self.b 
+0

です。ご返信ありがとうございました。あなたの提案は問題を解決しました。あなたはあなたの答えにちょっと凝ってくれますか?エラーメッセージからバイアスを正しくブロードキャストしなかったことをどうやって認識しましたか? – fabian

+0

私は詳細で答えを更新しました。元のエラーメッセージには、一致しない図形( '(1、)'と '(50、)'など)を含むすべての同じ情報が含まれています。 –