For more projects visit: https://setscholars.net
# Suppress warnings in Jupyter Notebooks
import warnings
warnings.filterwarnings("ignore")
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
In this notebook, we will learn how to use Metrics for Deep Learning with Keras in Python.
Below is a list of the metrics that you can use in Keras on regression problems.
import warnings
warnings.filterwarnings("ignore")
from numpy import array
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from matplotlib import pyplot
# prepare sequence
X = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0,
2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0
])
# create model
model = Sequential()
model.add(Dense(2, input_dim=1))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam', metrics=['mse', 'mae', 'mape'])
# train model
history = model.fit(X, X, epochs=500, batch_size=len(X), verbose=0)
# plot metrics
pyplot.figure(figsize=(8,6)); pyplot.plot(history.history['mse']); pyplot.show()
pyplot.figure(figsize=(8,6)); pyplot.plot(history.history['mae']); pyplot.show()
pyplot.figure(figsize=(8,6)); pyplot.plot(history.history['mape']); pyplot.show()
Below is a list of the metrics that you can use in Keras on classification problems.
import warnings
warnings.filterwarnings("ignore")
from numpy import array
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from matplotlib import pyplot
# prepare sequence
X = array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
y = array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
# create model
model = Sequential()
model.add(Dense(2, input_dim=1))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# train model
history = model.fit(X, y, epochs=400, batch_size=len(X), verbose=0)
# plot metrics
pyplot.figure(figsize=(8,6))
pyplot.plot(history.history['accuracy'])
pyplot.show()
In this coding recipe, we discussed how to use Metrics for Deep Learning with Keras in Python.
Specifically, we have learned the followings: