2015-04-23 41 views
3

我想與烤寬麪條/ nolearn運行迴歸。我很難找到文件如何做到這一點(一般來說,新的深度學習)。迴歸與烤寬麪條:錯誤

開始了與一個簡單的網絡(一個隱藏層)

from lasagne import layers 

from lasagne.nonlinearities import softmax 
from lasagne.updates import nesterov_momentum 
from nolearn.lasagne import NeuralNet  

print(np.shape(X)) # (137, 43) 
print(np.shape(y)) # (137,) 

layers_s = [('input', layers.InputLayer), 
      ('dense0', layers.DenseLayer), 
      ('output', layers.DenseLayer)] 

net_s = NeuralNet(layers=layers_s, 

       input_shape=(None, num_features), 
       dense0_num_units=43, 
       output_num_units=1, 
       output_nonlinearity=None, 

       regression=True, 

       update=nesterov_momentum, 
       update_learning_rate=0.001, 
       update_momentum=0.9, 

       eval_size=0.2, 
       verbose=1, 
       max_epochs=100) 

net_s.fit(X, y) 

我得到以下錯誤:

TypeError         Traceback (most recent call last) 
<ipython-input-23-23c15ceec104> in <module>() 
----> 1 net_s.fit(X, y) 

/home/alex/anaconda3/lib/python3.4/site-packages/nolearn/lasagne.py in fit(self, X, y) 
    148    out, self.loss, self.update, 
    149    self.X_tensor_type, 
--> 150    self.y_tensor_type, 
    151   ) 
    152   self.train_iter_, self.eval_iter_, self.predict_iter_ = iter_funcs 

/home/alex/anaconda3/lib/python3.4/site-packages/nolearn/lasagne.py in _create_iter_funcs(self, output_layer, loss_func, update, input_type, output_type) 
    298   all_params = get_all_params(output_layer) 
    299   update_params = self._get_params_for('update') 
--> 300   updates = update(loss_train, all_params, **update_params) 
    301 
    302   train_iter = theano.function(

/home/alex/src/lasagne/lasagne/updates.py in nesterov_momentum(loss, all_params, learning_rate, momentum) 
    38 # such that the gradient can be evaluated at the current parameters. 
    39 def nesterov_momentum(loss, all_params, learning_rate, momentum=0.9): 
---> 40  all_grads = theano.grad(loss, all_params) 
    41  updates = [] 
    42 

/home/alex/anaconda3/lib/python3.4/site-packages/theano/gradient.py in grad(cost, wrt, consider_constant, disconnected_inputs, add_names, known_grads, return_disconnected) 
    431 
    432  if cost is not None and cost.ndim != 0: 
--> 433   raise TypeError("cost must be a scalar.") 
    434 
    435  if isinstance(wrt, set): 

TypeError: cost must be a scalar. 

謝謝..

+0

您是否設法爲此問題找到解決方案?我陷入了完全相同的錯誤信息。 –

回答

2

請確保您使用的版本是已知的nolearn和烤寬麪條

假設您一直在關注Using convolutional neural nets to detect facial keypoints tutorial。然後做正確的事情是從this requirements.txt file安裝依賴,就像這樣:

pip uninstall Lasagne 
pip uninstall nolearn 
pip install -r https://raw.githubusercontent.com/dnouri/kfkd-tutorial/master/requirements.txt 

但是,如果你使用nolearn從Git的主人,然後確保你安裝的意大利千層麪版本是在requirements.txt file found there

pip uninstall Lasagne 
pip install -r https://raw.githubusercontent.com/dnouri/nolearn/master/requirements.txt 
2

不知道nolearn什麼版本您正在使用的烤寬麪條。我確實注意到你有y的形狀是(137,)。從我的使用情況來看,這需要(137, 1)才能適合您的情況,並且一般來說dim 2需要與output_num_units匹配。

嘗試y.reshape((-1, 1))

如果這不起作用,它可能是一個Python 3兼容性問題。

+0

我有類似的問題,這解決了這個問題。謝謝! – Jihun

+0

如果您在確定將數據發送到NeuralNet時應該具有什麼樣的形狀時遇到困難:對於分類任務,目標矢量'y'只能有一個維度。對於迴歸任務,無論迴歸目標的數量如何,它總是有兩個維度。 下面是一段代碼來說明這一點:https://gist.github.com/dnouri/fe855653e9757e1ce8c4 –