Machine Learning for absolute beginners: Your first Neural Network in ~10 minutes & < 20 lines of code!

  1. Its very dynamic. One can come across something new to be learnt on a daily basis; be it in terms of frameworks, literature, best practices, etc.
  2. There are umpteen number of resources (like this one) that can guide your ML journey. However I personally have often found that you cannot find a ‘silver bullet’ that can train you holistically and hence you need to choose carefully where to start, without the risk of burning out. Thus, I created this article in the form of a springboard to quick start the ML journey.
  3. ML from the outside seems very enticing and promising. It is only when one starts to study it (in a top to bottom approach) is it realised that there is an arduous journey across a demanding passage of theory and literature before actually getting to behold your own ‘artificially intelligent being’.
  1. The images that we will be classifying shall be from the MNIST dataset
A glimpse of the MNIST dataset
2+3
  • Loading dataset: As discussed in the earlier article¹, data is used to train an ANN. We shall be using the MNIST dataset to train our ANN. The MNIST dataset is a collection of 70,000 images. 60,000 of these are generally used for training and remaining for testing the ANN. All the images are of decimal digits ranging from 0 to 9.
  • Defining the model: A model needs to be defined before it can be trained. It requires one to define how many and what types of layers should there be in the ANN, how many neurons should there be in each layer and what activation function will each layer be using.
  • Compiling the model: Compilation requires one to define the loss and optimisation parameters of the model. These are nothing but methods to cut the stencil¹.
  • Fitting the model: The model is made to ‘fit’ the dataset by feeding the model with the data and running multiple cycles of update and corrections over the model. This step is also known as training the model.
  • Prediction: This is the last part where you can use your trained model to perform predictions for you. An image is given to the model for which you already know the class. The class predicted by the model can be compared with its ‘true’ class.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten,Conv2D,MaxPooling2D
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt
import numpy as np
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train = x_train/255
x_test = x_test/255
model = Sequential([
Flatten(input_shape = (28,28)),
Dense(32, activation = ‘relu’),
Dense(10, activation = ‘softmax’)
])
model.compile(loss = "sparse_categorical_crossentropy", optimizer = "adam", metrics = ['accuracy'])
model.fit(x_train,y_train,epochs = 5)
image_index = 9
plt.imshow(x_train[image_index])
plt.show()
print("The predicted class for the above image is: ",np.argmax(model.predict(x_train[image_index][np.newaxis,...])))

--

--

A computer science researcher straying across observations, facts, theories and applications. Also fascinated by intelligence, chaos and philosophy in general.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suraj Pandey

Suraj Pandey

80 Followers

A computer science researcher straying across observations, facts, theories and applications. Also fascinated by intelligence, chaos and philosophy in general.