<jupyter_start><jupyter_text>Deep learning---------------------------This example shows how to use ATOM to train and validate a Convolutional Neural Network implemented with [Keras](https://keras.io/).Import the MNIST dataset from [keras.datasets](https://keras.io/api/datasets/mnist/). This is a well known image dataset whose goal is to classify handwritten digits.  Load the data<jupyter_code># 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
# Create the convolutional neural network
def neural_network():
    model = Sequential()
    model.add(
        Conv2D(
            filters=64,
            kernel_size=3,
            activation="relu",
            input_shape=(28, 28, 1),
        )
    )
    model.add(Conv2D(filters=64, kernel_size=3, activation="relu"))
    model.add(Flatten())
    model.add(Dense(units=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=5, verbose=0)

# Convert the model to an ATOM model
model = ATOMModel(model, acronym="NN", fullname="Neural network")
# Download the MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# 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)<jupyter_output><empty_output><jupyter_text>Run the pipeline<jupyter_code>atom = ATOMClassifier(*data, n_rows=0.1, n_jobs=6, warnings=False, verbose=2)
# 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()
# 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}")
# Like any other model, we can define custom dimensions for the bayesian optimization
atom.run(
    models=model,
    metric="f1_weighted",
    n_calls=5,
    bo_params={
        "dimensions": [Categorical([32, 64, 128, 256], name="batch_size")],
        "max_time": 120,
    }
)<jupyter_output>Training ========================= >>
Models: NN
Metric: f1_weighted



Running BO for Neural network...
| call             | batch_size | f1_weighted | best_f1_weighted |    time | total_time |
| ---------------- | ---------- | ----------- | ---------------- | ------- | ---------- |
| Initial point 1  |         64 |      0.9674 |           0.9674 | 25.896s |    25.899s |
| Initial point 2  |         64 |      0.9674 |           0.9674 |  0.000s |    26.087s |
| Initial point 3  |        128 |      0.9592 |           0.9674 | 22.819s |    48.962s |
| Initial point 4  |        256 |      0.9462 |           0.9674 | 22.668s |     1m:12s |
| Initial point 5  |         64 |      0.9674 |           0.9674 |  0.001s |     1m:12s |

Results for Neural network:         
Bayesian Optimization ---------------------------
Best call --> Initial point 1
Best parameters --> {'batch_size': 64}
Best evaluation --> f1_weighted: 0.9674
Time elapsed: 1m:12s
Fit ------------------------------------------[...]<jupyter_text>Analyze the results<jupyter_code># Use the prediction methods like any other model
atom.nn.predict_proba(X_train)
# Or make plots...
atom.nn.plot_confusion_matrix()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1553679767871274, 'fraction_numerical': 0.0490635716169876, 'mean_word_length': 2.8110552763819094, 'pattern_counts': {'":': 2, '<': 12, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '640962', 'license': 'MIT', 'n_tokens_mistral': 1331, 'n_tokens_neox': 1202, 'n_words': 333, 'path': 'docs_sources/examples/deep_learning.ipynb', 'repo_name': 'tvdboom/ATOM', 'size': '3791'}