2015-11-14 24 views
1

我正在接近神經網絡實現,試圖建立一個使用Theano工作MLP。在教程之後,我試圖通過添加一個圖層來增強網絡,總共兩個隱藏層,每個隱藏層具有相同數量的單元(250)。問題是,當我運行腳本時遇到「形狀不匹配」ValueError。我的代碼是教程代碼的修改版本,可以在這裏找到http://deeplearning.net/tutorial/mlp.htmlTheano MLP與2隱藏層引發形狀不匹配錯誤

我修改的部分是片段-2,即MLP對象,如下所示:

class MLP(object): 

def __init__(self, rng, input, n_in, n_hidden, n_out): 
    """Initialize the parameters for the multilayer perceptron 

    :type rng: numpy.random.RandomState 
    :param rng: a random number generator used to initialize weights 

    :type input: theano.tensor.TensorType 
    :param input: symbolic variable that describes the input of the 
    architecture (one minibatch) 

    :type n_in: int 
    :param n_in: number of input units, the dimension of the space in 
    which the datapoints lie 

    :type n_hidden: int 
    :param n_hidden: number of hidden units 

    :type n_out: int 
    :param n_out: number of output units, the dimension of the space in 
    which the labels lie 

    """ 

    self.hiddenLayer1 = HiddenLayer(
     rng=rng, 
     input=input, 
     n_in=n_in, 
     n_out=n_hidden, 
     activation=T.tanh 
    ) 
    #try second hidden layer 
    self.hiddenLayer2 = HiddenLayer(
     rng=rng, 
     input=self.hiddenLayer1.output, 
     n_in=n_in, 
     n_out=n_hidden, 
     activation=T.tanh 
    ) 


    # The logistic regression layer gets as input the hidden units 
    # of the hidden layer 
    self.logRegressionLayer = LogisticRegression(
     input=self.hiddenLayer2.output, 
     n_in=n_hidden, 
     n_out=n_out 
    ) 
    # end-snippet-2 start-snippet-3 
    # L1 norm ; one regularization option is to enforce L1 norm to 
    # be small 
    self.L1 = (
     abs(self.hiddenLayer1.W).sum() 
     + abs(self.hiddenLayer2.W).sum() 
     + abs(self.logRegressionLayer.W).sum() 
    ) 

    # square of L2 norm ; one regularization option is to enforce 
    # square of L2 norm to be small 
    self.L2_sqr = (
     (self.hiddenLayer1.W ** 2).sum() 
     + (self.hiddenLayer2.W ** 2).sum() 
     + (self.logRegressionLayer.W ** 2).sum() 
    ) 

    # negative log likelihood of the MLP is given by the negative 
    # log likelihood of the output of the model, computed in the 
    # logistic regression layer 
    self.negative_log_likelihood = (
     self.logRegressionLayer.negative_log_likelihood 
    ) 
    # same holds for the function computing the number of errors 
    self.errors = self.logRegressionLayer.errors 

    # the parameters of the model are the parameters of the two layer it is 
    # made out of 
    self.params = self.hiddenLayer1.params + self.hiddenLayer2.params + self.logRegressionLayer.params 
    # end-snippet-3 

    # keep track of model input 
    self.input = input 

我還除去可讀性一些評論。輸出誤差我得到的是:

ValueError: Shape mismatch: x has 250 cols (and 20 rows) but y has 784 rows (and 250 cols) Apply node that caused the error: Dot22(Elemwise{Composite{tanh((i0 + i1))}}[(0, 0)].0, W) Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)] Inputs shapes: [(20, 250), (784, 250)] Inputs strides: [(2000, 8), (2000, 8)] Inputs values: ['not shown', 'not shown']

回答

2

到層2所需要的輸入的大小是大小相同的輸出從層1

hiddenLayer2hiddenLayer1作爲輸入,並hiddenLayer1.n_out == n_hidden但「hiddenLayer2。 n_in == n_in'。在這種情況下,n_hidden == 250n_in == 784。他們應該匹配,但不要因此而出現錯誤。

解決辦法是製作hiddenLayer2.n_in == hiddenLayer1.n_out