烤宽面条 - 意外的关键字错误

时间:2015-11-05 14:40:27

标签: python theano lasagne

我正在尝试用Python学习机器学习 - 并且想要运行千层面/ nolearn包。我已经安装了所有软件包 - 我使用下面的脚本(来自http://semantive.com/deep-learning-examples/),这会产生以下错误。如果有人知道如何解决此错误,请与我们联系。

脚本只在一个烤宽面条模块中给出初始错误:

  File "<ipython-input-89-2752ae2387c3>", line 11, in <module>
    from nolearn.lasagne import visualize

ImportError: cannot import name visualize

随后 - pad参数周围出现错误:

Traceback (most recent call last):

  File "<ipython-input-90-7a7b6ee7a652>", line 66, in <module>
    network = net.fit(x_train, y_train)

  File "C:\Users\Anaconda\lib\site-packages\nolearn\lasagne.py", line 138, in fit
    out = self._output_layer = self.initialize_layers()

  File "C:\Users\Anaconda\lib\site-packages\nolearn\lasagne.py", line 369, in initialize_layers
    layer = layer_factory(layer, **layer_params)

  File "C:\Users\src\lasagne\lasagne\layers\conv.py", line 368, in __init__
    super(Conv2DLayer, self).__init__(incoming, **kwargs)

TypeError: __init__() got an unexpected keyword argument 'pad'

代码

import cPickle as pickle
    import os
    import numpy as np

    import matplotlib.pyplot as plt
    import matplotlib.cm as cm
    import lasagne
    from lasagne import layers
    from lasagne.updates import nesterov_momentum
    from nolearn.lasagne import NeuralNet
    from nolearn.lasagne import visualize
    from sklearn.metrics import confusion_matrix, classification_report, accuracy_score


    def load_data(path):
        x_train = np.zeros((50000, 3, 32, 32), dtype='uint8')
        y_train = np.zeros((50000,), dtype="uint8")

        for i in range(1, 6):
            data = unpickle(os.path.join(path, 'data_batch_' + str(i)))
            images = data['data'].reshape(10000, 3, 32, 32)
            labels = data['labels']
            x_train[(i - 1) * 10000:i * 10000, :, :, :] = images
            y_train[(i - 1) * 10000:i * 10000] = labels

        test_data = unpickle(os.path.join(path, 'test_batch'))
        x_test = test_data['data'].reshape(10000, 3, 32, 32)
        y_test = np.array(test_data['labels'])

        return x_train, y_train, x_test, y_test


    def unpickle(file):
        f = open(file, 'rb')
        dict = pickle.load(f)
        f.close()
        return dict


    net = NeuralNet(
        layers=[('input', layers.InputLayer),
                ('conv2d1', layers.Conv2DLayer),
                ('maxpool1', layers.MaxPool2DLayer),
                ('conv2d2', layers.Conv2DLayer),
                ('maxpool2', layers.MaxPool2DLayer),
                ('dense', layers.DenseLayer),
                ('output', layers.DenseLayer),
                ],
        input_shape=(None, 3, 32, 32),
        conv2d1_num_filters=20,
        conv2d1_filter_size=(5, 5),
        conv2d1_stride=(1, 1),
        conv2d1_pad=(2, 2),
        conv2d1_nonlinearity=lasagne.nonlinearities.rectify,
        maxpool1_pool_size=(2, 2),
        conv2d2_num_filters=20,
        conv2d2_filter_size=(5, 5),
        conv2d2_stride=(1, 1),
        conv2d2_pad=(2, 2),
        conv2d2_nonlinearity=lasagne.nonlinearities.rectify,
        maxpool2_pool_size=(2, 2),
        dense_num_units=1000,
        dense_nonlinearity=lasagne.nonlinearities.rectify,
        output_nonlinearity=lasagne.nonlinearities.softmax,
        output_num_units=10,
        update=nesterov_momentum,
        update_momentum=0.9,
        update_learning_rate=0.0001,
        max_epochs=100,
        verbose=True
    )

    x_train, y_train, x_test, y_test = load_data(os.path.expanduser('~/Dropbox/Python/cifar-10-python.tar/cifar-10-python/cifar-10-batches-py/'))

    network = net.fit(x_train, y_train)
    predictions = network.predict(x_test)

    print classification_report(y_test, predictions)
    print accuracy_score(y_test, predictions)

1 个答案:

答案 0 :(得分:0)

您似乎正在使用不相容版本的Lasagne作为您的nolearn版本。

2015年7月和8月,pad功能已添加到Lasagne的Conv2DLayer课程中(请参阅herehere)。您的nolearn版本显然希望使用该版本或更高版本。

有两种可能性:

  1. 您(可能是偶然)在您的系统上有两个版本的Lasagne,但它是一个较旧的,首先由Python找到。如果是这样,请删除旧版本和/或确保通过Python找到新版本(第一个)。

  2. 你只是有一个过时的烤宽面条版本。解决方案:更新它!你如何做到这一点可能取决于你如何安装它。最终你需要从Github获得最新版本。