Neural Networks

Building a neural network to validate wine quality

By the end of this post, you will have built a fully functioning deep learning neural network capable of distinguishing between good and bad wine. A must-have for Friday nights.

This is a slightly more organised and robust version of How to train your first model in 15 minutes or less and How to use neural networks to make predictions. We will also be using a larger dataset this time around.

The dataset we will use contains twelve columns, eleven of which describe features of wines and the last one that grades the wine in question on a scale from 0 to 10, 10 being the best. You can download the dataset here, it contains data of over 4800+ different wines.

Here are the wine features that we will be considering, in order as per the dataset:

  1. fixed acidity
  2. volatile acidity
  3. citric acid
  4. residual sugar
  5. chlorides
  6. free sulfur dioxide
  7. total sulfur dioxide
  8. density
  9. pH
  10. sulfates
  11. alcohol

Let’s just dive straight into fitting your model:

Assuming you have all libraries installed, running the command above should train a model and save it as wine-model.h5 on the same folder.

Note that I increased significantly the number of epochs and also the batch size. Epochs are defined as “one pass over the entire dataset”, which means we are going through the entire dataset 5000 thousand times. I should also say that a batch is a set of N samples (a sample being each line of the dataset, or each of the wines described).

Not a lot of differences from the last tutorial, but do note that we had to change the output a little to adapt to how we are training our model. The dataset grades the wine from 0 to 10, 10 being the best, by using simple math and diving that by 10 then rounding it we are able to convert the grade to 0 (bad) and 1 (good). All wines rated below 5 are bad and all wines rated 5 or above are good. This logic is on line 11. I also increased the batch_size to 50 because 10 was making it too slow.

To evaluate this model I used the code below:

If you run the code above you will see that the accuracy is of 79.18%, less than the other example, even though we have a lot more data. Amongst other things, this is because we also have more inputs to account for; there are 11 features this time against 8 the other time. Almost 80% accuracy still pretty decent for us to play with.

Let’s test this bad boy with the code below:

Note that on line 8 I described a good wine, and on line 9 I described a bad wine. When you run this code and ask Keras to predict whether the described wines are good or bad, if you did everything the same way I did above, you should get the right answers returned.

What is next?

There are many enhancements that can be done to make sure this can be used in a real world scenario, one of them is applying supervised learning techniques. In fact, I strongly recommend you read my ebook Deep Learning for Developers, it’s free if you have a Kindle account and quite cheap if you don’t. In this book I take use the exact example above to teach neural networks, only I take the code to the next level by applying supervised learning techniques and modifying it to look like a real world use case.