In sequential model we can add layers one by one with different arguments.Then after making the model we have to compile the model so that we can use it. So have you tried to compile a sequential model?
So this recipe is a short example of how we can compile a keras model?
import pandas as pd import numpy as np from keras.datasets import mnist from sklearn.model_selection import train_test_split from keras.models import Sequential from keras.layers import Dense from keras.layers import Dropout
We have imported pandas, numpy, mnist(which is the dataset), train_test_split, Sequential, Dense and Dropout. We will use these later in the recipe.
Here we have used the inbuilt mnist dataset and stored the train data in X_train and y_train. We have used X_test and y_test to store the test data.
(X_train, y_train), (X_test, y_test) = mnist.load_data()
We have created an object model for sequential model. We can use two args i.e layers and name.
model = Sequential()Now, We are adding the layers by using 'add'. We can specify the type of layer, activation function to be used and many other things while adding the layer.
model.add(Dense(512)) model.add(Dropout(0.3)) model.add(Dense(256, activation='relu')) model.add(Dropout(0.2))
Compiling a model is required to finalise the model and make it completely ready to use. For compilation, we need to specify an optimizer and a loss function. We can compile a model by using compile attribute. Let us first look at its parameters before using it.
model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])