Deep learning¶
This example shows how to use ATOM to train and validate a Convolutional Neural Network implemented with Keras.
Import the MNIST dataset from keras.datasets. This is a well known image dataset whose goal is to classify handwritten digits.
Load the data¶
In [1]:
Copied!
# Disable annoying tf warnings
import logging
import tensorflow as tf
tf.get_logger().setLevel(logging.ERROR)
# Import standard packages
from atom import ATOMClassifier, ATOMModel
from skopt.space.space import Integer, Categorical
# Keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D
from keras.wrappers.scikit_learn import KerasClassifier
# Disable annoying tf warnings
import logging
import tensorflow as tf
tf.get_logger().setLevel(logging.ERROR)
# Import standard packages
from atom import ATOMClassifier, ATOMModel
from skopt.space.space import Integer, Categorical
# Keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D
from keras.wrappers.scikit_learn import KerasClassifier
In [2]:
Copied!
# Create the convolutional neural network
def neural_network():
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation="relu", input_shape=(28, 28, 1)))
model.add(Conv2D(64, kernel_size=3, activation="relu"))
model.add(Flatten())
model.add(Dense(10, activation="softmax"))
model.compile(
optimizer="adam",
loss="categorical_crossentropy",
metrics=["accuracy"],
)
return model
# Since ATOM uses sklearn's API, use Keras' wrapper
model = KerasClassifier(neural_network, epochs=1, batch_size=512, verbose=0)
# Convert the model to an ATOM model
model = ATOMModel(model, acronym="NN", fullname="Neural network")
# Create the convolutional neural network
def neural_network():
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation="relu", input_shape=(28, 28, 1)))
model.add(Conv2D(64, kernel_size=3, activation="relu"))
model.add(Flatten())
model.add(Dense(10, activation="softmax"))
model.compile(
optimizer="adam",
loss="categorical_crossentropy",
metrics=["accuracy"],
)
return model
# Since ATOM uses sklearn's API, use Keras' wrapper
model = KerasClassifier(neural_network, epochs=1, batch_size=512, verbose=0)
# Convert the model to an ATOM model
model = ATOMModel(model, acronym="NN", fullname="Neural network")
In [3]:
Copied!
# Download the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Download the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
In [4]:
Copied!
# Reshape data to fit model
X_train = X_train.reshape(60000,28,28,1)
X_test = X_test.reshape(10000,28,28,1)
data = (X_train, y_train), (X_test, y_test)
# Reshape data to fit model
X_train = X_train.reshape(60000,28,28,1)
X_test = X_test.reshape(10000,28,28,1)
data = (X_train, y_train), (X_test, y_test)
Run the pipeline¶
In [5]:
Copied!
atom = ATOMClassifier(*data, n_rows=0.1, n_jobs=6, warnings=False, verbose=2)
atom = ATOMClassifier(*data, n_rows=0.1, n_jobs=6, warnings=False, verbose=2)
<< ================== ATOM ================== >> Algorithm task: multiclass classification. Parallel processing with 6 cores. Dataset stats ====================== >> Shape: (7000, (28, 28, 1), 2) --------------------------------------- Train set size: 6000 Test set size: 1000 --------------------------------------- | | dataset | train | test | |---:|:----------|:----------|:----------| | 0 | 681 (1.1) | 579 (1.1) | 102 (1.3) | | 1 | 804 (1.3) | 681 (1.3) | 123 (1.6) | | 2 | 722 (1.2) | 614 (1.1) | 108 (1.4) | | 3 | 712 (1.1) | 617 (1.1) | 95 (1.2) | | 4 | 682 (1.1) | 605 (1.1) | 77 (1.0) | | 5 | 624 (1.0) | 540 (1.0) | 84 (1.1) | | 6 | 698 (1.1) | 591 (1.1) | 107 (1.4) | | 7 | 726 (1.2) | 620 (1.1) | 106 (1.4) | | 8 | 693 (1.1) | 601 (1.1) | 92 (1.2) | | 9 | 658 (1.1) | 552 (1.0) | 106 (1.4) |
In [6]:
Copied!
# When the input data has more than 2 dimensions, ATOM creates a
# dataset with just one column of shape (n_samples, shape_sample)
atom.head()
# When the input data has more than 2 dimensions, ATOM creates a
# dataset with just one column of shape (n_samples, shape_sample)
atom.head()
Out[6]:
Multidimensional feature | Target | |
---|---|---|
0 | [[[0], [0], [0], [0], [0], [0], [0], [0], [0],... | 2 |
1 | [[[0], [0], [0], [0], [0], [0], [0], [0], [0],... | 9 |
2 | [[[0], [0], [0], [0], [0], [0], [0], [0], [0],... | 5 |
3 | [[[0], [0], [0], [0], [0], [0], [0], [0], [0],... | 3 |
4 | [[[0], [0], [0], [0], [0], [0], [0], [0], [0],... | 7 |
In [7]:
Copied!
# Every row in the column contains the data of one image
print(f"Shape of one image: {atom.iloc[0, 0].shape}")
print(f"atom's shape (n_rows, (shape_image), n_cols): {atom.shape}")
# Every row in the column contains the data of one image
print(f"Shape of one image: {atom.iloc[0, 0].shape}")
print(f"atom's shape (n_rows, (shape_image), n_cols): {atom.shape}")
Shape of one image: (28, 28, 1) atom's shape (n_rows, (shape_image), n_cols): (7000, (28, 28, 1), 2)
In [8]:
Copied!
# Like any other model, we can define custom dimensions for the bayesian optimization
dim = [Integer(1, 3, name="epochs"), Categorical([32, 64, 128, 256], name="batch_size")]
atom.run(model, metric="f1_weighted", n_calls=5, bo_params={"dimensions": dim, "cv": 1, "max_time": 120})
# Like any other model, we can define custom dimensions for the bayesian optimization
dim = [Integer(1, 3, name="epochs"), Categorical([32, 64, 128, 256], name="batch_size")]
atom.run(model, metric="f1_weighted", n_calls=5, bo_params={"dimensions": dim, "cv": 1, "max_time": 120})
Training ===================================== >> Models: NN Metric: f1_weighted Running BO for Neural network... Initial point 1 --------------------------------- Parameters --> {'epochs': 1, 'batch_size': 128} Evaluation --> f1_weighted: 0.9139 Best f1_weighted: 0.9139 Time iteration: 15.626s Total time: 15.630s Initial point 2 --------------------------------- Parameters --> {'epochs': 2, 'batch_size': 64} Evaluation --> f1_weighted: 0.9532 Best f1_weighted: 0.9532 Time iteration: 10.250s Total time: 26.005s Initial point 3 --------------------------------- Parameters --> {'epochs': 3, 'batch_size': 128} Evaluation --> f1_weighted: 0.9601 Best f1_weighted: 0.9601 Time iteration: 13.350s Total time: 39.394s Initial point 4 --------------------------------- Parameters --> {'epochs': 1, 'batch_size': 64} Evaluation --> f1_weighted: 0.9431 Best f1_weighted: 0.9601 Time iteration: 5.555s Total time: 44.974s Initial point 5 --------------------------------- Parameters --> {'epochs': 2, 'batch_size': 64} Evaluation --> f1_weighted: 0.9581 Best f1_weighted: 0.9601 Time iteration: 10.668s Total time: 55.675s Results for Neural network: Bayesian Optimization --------------------------- Best parameters --> {'epochs': 3, 'batch_size': 128} Best evaluation --> f1_weighted: 0.9601 Time elapsed: 58.922s Fit --------------------------------------------- Train evaluation --> f1_weighted: 0.9993 Test evaluation --> f1_weighted: 0.9599 Time elapsed: 17.371s ------------------------------------------------- Total time: 1m:16s Final results ========================= >> Duration: 1m:16s ------------------------------------------ Neural network --> f1_weighted: 0.9599
Analyze the results¶
In [9]:
Copied!
# Use the prediction methods like any other model
atom.nn.predict_proba(X_train)
# Use the prediction methods like any other model
atom.nn.predict_proba(X_train)
Out[9]:
array([[3.3193524e-07, 5.8962629e-10, 8.7443772e-07, ..., 1.3339026e-06, 9.2499363e-07, 1.3533815e-05], [9.9999058e-01, 1.8723382e-11, 9.3173858e-06, ..., 7.7003387e-10, 4.8539359e-08, 4.6308526e-09], [6.7930179e-14, 5.5634137e-09, 9.0469169e-12, ..., 5.4167479e-09, 1.3212116e-10, 6.2716066e-07], ..., [1.7736274e-10, 9.5250149e-14, 4.2735815e-13, ..., 7.2675728e-12, 1.5567214e-07, 3.7829523e-06], [2.9188291e-06, 8.0337742e-12, 4.5623146e-08, ..., 5.2204485e-10, 4.7253510e-13, 6.2878592e-13], [1.3656822e-06, 3.8385992e-09, 1.8453775e-05, ..., 1.7541604e-05, 9.9955970e-01, 1.8250587e-04]], dtype=float32)
In [10]:
Copied!
# Or make plots...
atom.nn.plot_confusion_matrix()
# Or make plots...
atom.nn.plot_confusion_matrix()