我正在尝试在 iris 数据集上训练神经网络。我找到了一个关于使用 nolearn 的神经网络的教程,讲师使用了 mnist 数据集。我试图“模仿”相同的算法,但出现了错误。这是代码:
# Sklearn libraries
from sklearn.preprocessing import LabelEncoder
# Lasagne
import lasagne
from lasagne import layers
from lasagne.updates import nesterov_momentum
from nolearn.lasagne import NeuralNet
# Pandas and Numpy
import pandas as pd
import numpy as np
def load_data():
labelenc = LabelEncoder()
# Loading of dataset
iris = pd.read_csv('/home/gunslinger/Desktop/IrisDataset.csv', header=None)
iris.iloc[:, 4] = labelenc.fit_transform(iris.iloc[:, 4])
iris = iris.iloc[np.random.permutation(np.arange(len(iris)))]
# Initialization
X = iris.iloc[:, :4]
y = iris.iloc[:, 4]
X = X.astype(np.float32)
y = y.astype(np.int32)
X_train = X[:100]
X_valid = X[100:125]
X_test = X[125:150]
y_train = y[:100]
y_valid = y[100:125]
y_test = y[125:150]
return dict(
X_train=X_train,
y_train=y_train,
X_valid=X_valid,
y_valid=y_valid,
X_test=X_test,
y_test=y_test,
)
def nn_func(data):
net1 = NeuralNet(
layers=[('input', layers.InputLayer),
('hidden', layers.DenseLayer),
('output', layers.DenseLayer)
],
# Layer parameters:
input_shape=(None, 4),
hidden_num_units=5,
output_nonlinearity=lasagne.nonlinearities.softmax,
output_num_units=3,
# Optimization method:
update=nesterov_momentum,
update_learning_rate=0.01,
update_momentum=0.9,
max_epochs=10,
verbose=1,
)
net1.fit(data['X_train'], data['y_train'])
def main():
data = load_data()
print("Got %i testing datasets." % len(data['X_train']))
nn_func(data)
if __name__ == '__main__':
main()
以及运行代码时出现的错误:http: //pastebin.com/9eccuzEQ
有一个非常相似的问题。然而,为他解决问题的东西,对我却没有。