In machine learning, We have to first train the model on the data we have so that the model can learn and we can use that model to predict the further results.
So this recipe is a short example of how to run/fit data with 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.
Here we have added four layers which will be connected one after other.
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'])
We can fit a model on the data we have and can use the model after that. Fitting a model means training our model on a data i.e. passing the data into the model so that the model can update its internal mathematical variables and get ready to make predictions.
Here we are using the data which we have splitted i.e the training data for fitting the model.
While fitting we can pass various parameters like batch_size, epochs, verbose, validation_data and so on.