Your code is correct (in accordance with the book).
In some runs, I also noticed low accuracy. This is because of the small dataset (150 samples).
If you re-run the steps from the shuffle step and below, then you will receive different results every time. In some cases, it can easily be 96% accuracy. Just, rerun the experiment.
Is there a way to pull the actual predictions from Axon.Loop.evaluator? I tried removing the accuracy metric, but that just returns an empty map. How can I compare the predicted values with y_test?
I ran into this also, and decided it had to be some kind of typo with how the test set is set up. After a lot of head scratching, I think there’s a more subtle error with the setup of the test data. I believe when the species are assigned their positions in the one-hot encoding vector, that order is determined by the order in which the species are encountered in the test and training data.
For instance, if the species of the first 3 rows of the training set are "Iris-virginica", "Iris-setosa", ""Iris-versicolor", then those entries in the train_y data will look like [1, 0, 0], [0, 1, 0], [0, 0, 1] and the model will learn to predict [1, 0, 0] if the features match what it’s learned about “Iris-virginica.”
If the species are encountered in a different order in the test data, then we may end up with “Iris-virginica” having the 2nd position instead of the first in the test_y data, so the model will predict [1, 0, 0] but the scoring logic will be comparing against [0, 1, 0]
I had a few more minutes to play with this, and so far it looks like we can get better results by processing the x and y data into tensors before splitting up test and training sets.
Thanks, I will try this. Could it be that the one-hot encoding creates different category values since we are encoding different sets of labels? I think that might explain the low accuracy, if so.
Edit: Yep, I just tried it, and this seems to have fixed the problem. Thanks, @grossvogel!
Notice it manually maps the categorical labels to {0,1,2}, before converting it to a tensor to be one-hot encoded
I have to say I really prefer @grossvogel’s alternative code that uses Explorer.Series.cast(:category) instead since Explorer already does provide a convenient method for one-hot encoding.
I’ve reported this issue to @seanmor5 as an Errata on the official devtalk forum referencing this thread.
Edit: Added the link to download the accompanying Livebooks from PragProg.