Creating a Model
Subscribe to Tech with Tim
Creating the Model
Now time to create our first neural network model! We will do this by using the Sequential object from keras. A Sequential model simply defines a sequence of layers starting with the input layer and ending with the output layer. Our model will have 3 layers, and input layer of 784 neurons (representing all of the 28x28 pixels in a picture) a hidden layer of an arbitrary 128 neurons and an output layer of 10 neurons representing the probability of the picture being each of the 10 classes.
model = keras.Sequential([ keras.layers.Flatten(input_shape=(28,28)), keras.layers.Dense(128, activation="relu"), keras.layers.Dense(10, activation="softmax") ])
Training the Model
Now that we have defined the model it is time to compile and train it. Compiling the model is just picking the optimizer, loss function and metrics to keep track of. Training is the process of passing our data to the model.
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) model.fit(train_images, train_labels, epochs=5)
Testing the Model
Now that the model has been trained it is time to test it for accuracy. We will do this using the following line of code:
test_loss, test_acc = model.evaluate(test_images, test_labels) print('\nTest accuracy:', test_acc)
In the next tutorial we will use our trained model to make predictions on specific images.