我正在尝试用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)
答案 0 :(得分:0)