2015-06-20 55 views
0

在意大利千層麪的錯誤,我想基於theano /千層麪,將(基本上)嘗試做多變量回歸創造一個神經網絡。「ValueError異常:形狀不匹配」的神經網絡

代碼的肉:

train_value = train_df.values[:, 0] 
    train_data = train_df.values[:, 1:] 
    #print "train:", train_data.shape, train_label.shape 


    #test_data = test_df.values 
    #print "test:", test_data.shape 


    train_data = train_data.astype(np.float) 
    train_value = train_value.astype(np.int32) 



    fc_1hidden = NeuralNet(
     layers = [ # three layers: one hidden layer 
      ('input', layers.InputLayer), 
      ('hidden', layers.DenseLayer), 
      ('dropout', layers.DropoutLayer), 
      ('output', layers.DenseLayer), 
      ], 
     # layer parameters: 
     input_shape = (None, 36), # 36 rows of data 
     hidden_num_units = 100, # number of units in hidden layer 
     dropout_p = 0.25, # dropout probability 
     output_nonlinearity = softmax, # output layer uses softmax function 
     output_num_units = 10, # 10 labels 

     # optimization method: 
     #update = nesterov_momentum, 
     update = sgd, 
     update_learning_rate = 0.001, 
     #update_momentum = 0.9, 

     eval_size = 0.1, 

     # batch_iterator_train = BatchIterator(batch_size = 20), 
     # batch_iterator_test = BatchIterator(batch_size = 20), 

     max_epochs = 100, # we want to train this many epochs 
     verbose = 1, 
     ) 

fc_1hidden.fit(train_data, train_value) 
plot_loss(fc_1hidden) 

這裏,train_value僅1(數值)列的數據,我想訓練我的神經網絡預測,下面57列(train_data)都應該適當加權的參數/值(所有數字)以預測第一列中的值。

然而,當我運行該腳本,我得到以下錯誤:

Epoch | Train loss | Valid loss | Train/Val | Valid acc | Dur 
--------|--------------|--------------|---------------|-------------|------- 
Traceback (most recent call last): 
    File "neuralnetwork.py", line 77, in <module> 
    fc_1hidden.fit(train_data, train_value) 
    File "/Users/spadavec/anaconda/lib/python2.7/site-packages/nolearn/lasagne.py", line 150, in fit 
    self.train_loop(X, y) 
    File "/Users/spadavec/anaconda/lib/python2.7/site-packages/nolearn/lasagne.py", line 188, in train_loop 
    batch_train_loss = self.train_iter_(Xb, yb) 
    File "/Users/spadavec/anaconda/lib/python2.7/site-packages/theano/compile/function_module.py", line 606, in __call__ 
    storage_map=self.fn.storage_map) 
    File "/Users/spadavec/anaconda/lib/python2.7/site-packages/theano/compile/function_module.py", line 595, in __call__ 
    outputs = self.fn() 
ValueError: Shape mismatch: x has 83 cols (and 29 rows) but y has 36 rows (and 100 cols) 
Apply node that caused the error: Dot22(x_batch, W) 
Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)] 
Inputs shapes: [(29, 83), (36, 100)] 
Inputs strides: [(664, 8), (800, 8)] 
Inputs values: ['not shown', 'not shown'] 

我不知道在那裏得到這個形狀 - 沒有我的數據有83列或行。 (注意:我試圖修改這個腳本,它原本是用來查看臉部圖片並猜測不同部位(眼睛,鼻子,嘴巴等)的位置)。

我已經寫在pybrain這個(沒有差法)的更簡單的版本,但我想遷移到sklearn /烤寬麪條/ theano因爲它會打開更多的門。

+0

你看到了什麼,如果你'打印train_data.shape,只需調用之前train_value.shape''fit'? (只是爲了確認你提供的數據是你認爲自己的形狀) –

回答

0

既然你想要做的迴歸,一定要正確設置輸出類型:

output_nonlinearity = linear 

你確定你也有10輸出單元?我在千層麪經歷了一些奇怪的行爲。我認爲隨着時間的推移API已經發生變化,並且包含一些錯誤。我成功地使用了最新的API demo並使其適應我的需求。