Machine Learning in Python
In this tutorial we will go through the Hello World project for a Convolutional Neural Network (which is used for images). First let's start off by saying what a Neural Network is. Think of two 'visible' layers. The input might have let's say 3 nodes and the output 2. To narrow it down we have a big set of hidden layers. That's the basics. Today we will be working with the MNIST dataset. I would recommend doing this outside of repl just because it will process faster. If you want to stay on your browser try https://colab.research.google.com/notebooks/intro.ipynb#recent=true. First we need to load up the dataset. In most cases creating the dataset is a hassle but since we have one that's preloaded it should be easy.
import tensorflow as tf import numpy as np mnist = tf.keras.datasets.mnist (x_train,y_train),(x_test,y_test) = mnist
What we've done here is imported our dataset and split it into variables.
x_train = tf.keras.utils.normalize(x_train) x_test = tf.keras.utils.normalize(x_test)
This will just normalize our data so we can better run it through. Now time for the fun part. Let's build our model.
model = tf.keras.models.Sequential() model.add(tf.keras.layers.Flatten()) #1d Databyte model.add(tf.keras.layers.Dense(128,activation = 'relu')) model.add(tf.keras.layers.Dense(128,activation='relu')) model.add(tf.keras.layers.Dense(10,activation ='softmax')
Now this might be a LOT to take in. What we're doing is adding hidden layers. These layers will be what our input goes through.
Now time to compile.
You don't need to understand any of this right now. Just keep in mind that this is how we finish our model
model.fit(x_train,y_train,epochs=3) #an epoch is how many times we run through our dataset.
To save and load do.
model.save('lol.model') model = tf.keras.models.load_model('lol.model')
Finally to predict just do
prediction =model.predict(x_test) prediction_real = np.argmax(prediction)
Done. You've just created your first neural network. Hope you enjoyed - Robowolf