Neural Networks

This code is a machine learning model that is trying to predict the number of subsets in a given set based on some input data. It's using PyTorch, a popular deep learning library, to define and train the model.


More specifically the neural network is trying to predict what the union of some sets is this is a typical example of data that is used to train the NN:

 subsets

[1 0 1 1 0 1 1 0]

 [0 1 0 0 1 0 0 1]

 [1 0 0 0 0 0 0 0]

 [0 0 0 0 0 0 0 1]

  their union

[1 1 1 1 1 1 1 1]

  true score: 1.0

  predicted score: 0.866.

Here true score is how close the "their union" array is to the actual array and predicted score is how close the NN things the "their union" array is to the actual array. Here is another example:

  subsets

[1 0 0 0 0 0 0 0]

 [0 1 0 0 1 1 0 0]

 [0 0 0 0 0 0 0 0]

 [0 0 0 1 0 0 0 1]

  their union

[1 1 0 1 1 1 0 1]

  true score: 0.75

  predicted score: 0.614


These plots demonstrate the performance of the neural network as it is trained on increasing amounts of test data. As the images progress, it becomes clear that the network's accuracy improves with more training data. From random guesses to consistent predictions.


This code is a complete example of a simple image classification task using the Fashion-MNIST dataset. The code loads the dataset, splits it into training and testing sets, and then normalizes the pixel values of the images between 0 and 1. It then creates a simple neural network using the Sequential API of TensorFlow's Keras library. The model has 3 layers: an input layer (flatten), a hidden layer (dense) and an output layer (dense). The model is then compiled and trained with the training data for 10 epochs. The code also evaluates the model's performance on the test set using the evaluate method, and it prints the test accuracy. Finally, the code allows the user to enter a number to select an image from the test set and the model will predict the class of the image and display it.

Epoch 1/10

1875/1875 [==============================] - 17s 8ms/step - loss: 0.4974 - accuracy: 0.8245

Epoch 2/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.3743 - accuracy: 0.8653

Epoch 3/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.3352 - accuracy: 0.8770

Epoch 4/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.3107 - accuracy: 0.8852

Epoch 5/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.2947 - accuracy: 0.8910

Epoch 6/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.2770 - accuracy: 0.8985

Epoch 7/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.2661 - accuracy: 0.9013

Epoch 8/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.2551 - accuracy: 0.9054

Epoch 9/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.2448 - accuracy: 0.9088

Epoch 10/10

1875/1875 [==============================] - 5s 2ms/step - loss: 0.2342 - accuracy: 0.9129

<keras.callbacks.History at 0x7fa993ab6970>

These images show what the NN predicts the coloured image is going to be. In black and white its an image that represents what the NN is seeing.

This script loads and normalizes the CIFAR-10 dataset, which consists of 60,000 32x32 color images in 10 classes, with 6,000 images per class. There are 50,000 training images and 10,000 test images. It then visualizes one of the images from the dataset and creates a convolutional neural network using the Keras library in TensorFlow. The model is trained on the training set for 4 epochs and then evaluated on the test set. The script also uses an image data generator to augment the training data by applying random rotations, shifts, shears, zooms, and horizontal flips. Finally, the script imports the cats_vs_dogs dataset, which consists of images of cats and dogs, and splits the data into training, testing, and validation sets.


Epoch 1/3

582/582 [==============================] - 380s 644ms/step - loss: 0.1860 - accuracy: 0.9240 - val_loss: 0.0913 - val_accuracy: 0.9686

Epoch 2/3

582/582 [==============================] - 380s 651ms/step - loss: 0.0733 - accuracy: 0.9731 - val_loss: 0.0654 - val_accuracy: 0.9764

Epoch 3/3

582/582 [==============================] - 373s 638ms/step - loss: 0.0583 - accuracy: 0.9780 - val_loss: 0.0567 - val_accuracy: 0.9815

[0.9240193367004395, 0.9730789661407471, 0.9779688119888306]

These are some examples of the images used to train the NN.