0

I am trying to train a neural network on the iris dataset. I found a tutorial on neural networks using nolearn and the instructor used the mnist dataset. I tried to "mimick" the same algorithm but an error is raised. Here is the code:

 # 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()

And the error that I get when I run the code: http://pastebin.com/9eccuzEQ

There is a very similar question to this. However, what solved the problem for him, did not for me.

fxhh
  • 47
  • 9

1 Answers1

1

I have solved the problem myself. The data types had to be numpy arrays instead of pandas dataframes.

fxhh
  • 47
  • 9