Generic filters
Exact matches only

# A Practical Introduction to Early Stopping in Machine Learning | by B. Chen | Jul, 2020

Next, let’s create `X` and `y`. Keras and TensorFlow 2.0 only take in Numpy array as inputs, so we will have to convert DataFrame back to Numpy array.

`# Creating X and yX = df[['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']]# Convert DataFrame into np arrayX = np.asarray(X)y = df[['label_setosa', 'label_versicolor', 'label_virginica']]# Convert DataFrame into np arrayy = np.asarray(y)`

Finally, let’s split the dataset into a training set (80%)and a test set (20%) using `train_test_split()` from sklearn library.

`X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.20)`

Great! our data is ready for building a Machine Learning model.

There are 3 ways to create a machine learning model with Keras and TensorFlow 2.0. Since we are building a simple fully connected neural network and for simplicity, let’s use the easiest way: Sequential Model with `Sequential()`.

Let’s go ahead and create a function called `create_model()` to return a Sequential model.

`from tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import Densedef create_model(): model = Sequential([Dense(64, activation='relu', input_shape=(4,)),Dense(128, activation='relu'),Dense(128, activation='relu'),Dense(128, activation='relu'),Dense(64, activation='relu'),Dense(64, activation='relu'),Dense(64, activation='relu'),Dense(3, activation='softmax')])return model`

Our model has the following specifications:

• The first layer (also known as the input layer) has the `input_shape` to set the input size `(4,)`
• The input layer has 64 units, followed by 3 dense layers, each with 128 units. Then there are further 3 dense layers, each with 64 units. All these layers use the ReLU activation function.
• The output Dense layer has 3 units and the softmax activation function.

## Compile and train the model

In order to train a model, we first have to configure our model using `compile()` and pass the following arguments:

• Use Adam (`adam`) optimization algorithm as the optimizer
• Use categorical cross-entropy loss function (`categorical_crossentropy`) for our multiple-class classification problem
• For simplicity, use `accuracy` as our evaluation metrics to evaluate the model during training and testing.
`model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])`

After that, we can call `model.fit()` to fit our model to the training data.

`history = model.fit(X_train, y_train, epochs=200, validation_split=0.25, batch_size=40, verbose=2)`

If all runs smoothly, we should get an output like below

`Train on 84 samples, validate on 28 samplesEpoch 1/20084/84 - 1s - loss: 1.0901 - accuracy: 0.3214 - val_loss: 1.0210 - val_accuracy: 0.7143Epoch 2/20084/84 - 0s - loss: 1.0163 - accuracy: 0.6905 - val_loss: 0.9427 - val_accuracy: 0.7143......Epoch 200/20084/84 - 0s - loss: 0.5269 - accuracy: 0.8690 - val_loss: 0.4781 - val_accuracy: 0.8929`

## Plot the learning curves

Finally, let’s plot the loss vs. epochs graph on the training and validation sets.

It is preferable to create a small function for plotting metrics. Let’s go ahead and create a function `plot_metric()`.

`%matplotlib inline%config InlineBackend.figure_format = 'svg'def plot_metric(history, metric):train_metrics = history.history[metric]val_metrics = history.history['val_'+metric]epochs = range(1, len(train_metrics) + 1)plt.plot(epochs, train_metrics)plt.plot(epochs, val_metrics)plt.title('Training and validation '+ metric)plt.xlabel("Epochs")plt.ylabel(metric)plt.legend(["train_"+metric, 'val_'+metric])plt.show()`

By running `plot_metric(history, 'loss')` to get a picture of loss progress.