Let's now look at a classification problem where there are multiple output classes. In this case, we want to classify our dataset into 3 flower species: Iris-setosa, Iris-versicolor and Iris-virginica, based on the flower's attributes.
The dataset we're using is the Iris Flowers Dataset from the UCI Machine Learning Repository. Here are the details. There are 150 different instances and this is its structure by columns:
0. Sepal length in cm
1. Sepal width in cm
2. Petal length in cm
3. Petal width in cm
4. Class (Iris-setosa, Iris-versicolor or Iris-virginica)
The last column is our output, which is in plain text, However, we can only create models that deal with numbers, so let's use One-hot Encoding. This is a way of encoding data that separates our output column into 3 separate columns corresponding to the 3 species, and using 0 and 1 we select the correct species. Here's an example where our original output was Iris-versicolor:
One-hot encoding example.
Open Neurons. In 'Train File', select the already included 'iris_one_hot.txt' file inside the 'Data' folder.
As for our inputs, they'll be columns 0-3, and the outputs will be the flower species in One-hot Encoding, columns 4-6.
Select the correct input and output columns, and press 'Ctrl + N' to create the model. We'll name it 'Iris_model'.
Our window should look like this:
Select input and output columns. Create model.
Let's add a new Hidden Layer.
Click 'Add' below the Output Layer. Select 12 neurons. We'll leave the Activation Function at 'ReLu', as it works well for most hidden layers. We'll call the layer 'Layer_2'. Click 'Add Layer'.
Add new hidden layer.
Finally, just like in the Diabetes problem, we have to change the Output layer for it to output values between 0 and 1, since this is a classification problem. This time, we'll use the 'Softmax' function.
Double click 'Output' and change the Activation Function to 'Softmax'. Press 'Save Edit'.
It's time to train the model. Let's change the training settings to be compatible with our problem.
Change 'Loss Function' to 'Categorical Crossentropy'. This is an adequate loss function for dealing with Multi-class problems.
Change 'Show metric' to 'Binary Accuracy'.
Change 'Epochs' to 200 and 'Batch Size' to 5.
Press all 'Show' options, so we can get more information about the model's performance. Press 'Train'.
Change training settings.
Here are our results:
Output of training.
Our model has a final Binary Accuracy of 98.2%, that's really good! It means that it's classifying the flowers correctly almost every time. Looking at the sample of outputs, after rounding the predicted outputs to 0 or 1, we see that they're all correct.
Model Binary Accuracy and Loss plots.
Here are the plots for the Binary Accuracy and Loss Function (Categorical Crossentropy). As we can see, by epoch 75 they're already both stabilized, so we could've lowered the epochs in the training settings to save some runtime.
And there we have it, our model is trained to classify Iris flowers based on their characteristics. Very useful in daily life if you ask me.
Note: if we wanted to further improve the performance of the model, we could either train it with a different dataset, or shuffle our current dataset and train it again.
Since we're very proud of our model, we want to print its predicted outputs to a file, so everyone can see the power of Machine Learning. For that, we'll use the 'Predict' function.
With the same input columns selected, change the 'Predict' file path to the 'iris_one_hot.txt' file.
Press the 'Predict' button below the input and output columns.
In 'Save output to text file', select the folder you want to save it to, and write as the file name 'iris_output'. Click 'Save'
Press 'Predict'.
Predict window.
Output file.
We finally have a text file with all of our predicted outputs from this dataset. Feel free to send it to all your distant relatives.
Our model was very successful at classifying these flowers, even with a relatively small dataset (only 150 instances). This specific problem and dataset are a very well studied problem in Data Analysis and often used as an example of a Multi-class Classification. Here's a tutorial on how to do it with Python.