<jupyter_start><jupyter_text>Convolution Neural Network<jupyter_code># Optional to surpress warning
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.datasets import cifar10

# Set physical device to GPU
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# Data Shape
print('Data shape: ',x_train.shape)
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
#  Model Initialization
model = keras.Sequential(
    [
        layers.InputLayer(input_shape=(32, 32, 3)),
        layers.Conv2D(32, 3, padding='valid', activation='relu'),
        layers.MaxPool2D(pool_size=(2,2)),
        layers.Conv2D(64, 3, activation='relu'),
        layers.MaxPool2D(),
        layers.Conv2D(128, 3, activation='relu'),
        layers.Flatten(),
        layers.Dense(64, activation='relu'),
        layers.Dense(10)
    ]
)

model.summary()
model.compile(
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    optimizer=keras.optimizers.Adam(learning_rate=3e-4),
    metrics=['accuracy']
)

model.fit(x_train, y_train, batch_size=64, epochs=10, verbose=2)
model.evaluate(x_test, y_test, batch_size=64, verbose=True)<jupyter_output>Epoch 1/10
782/782 - 9s - loss: 1.7068 - accuracy: 0.3774
Epoch 2/10
782/782 - 4s - loss: 1.3580 - accuracy: 0.5135
Epoch 3/10
782/782 - 4s - loss: 1.2270 - accuracy: 0.5659
Epoch 4/10
782/782 - 4s - loss: 1.1278 - accuracy: 0.6035
Epoch 5/10
782/782 - 4s - loss: 1.0488 - accuracy: 0.6328
Epoch 6/10
782/782 - 4s - loss: 0.9923 - accuracy: 0.6538
Epoch 7/10
782/782 - 4s - loss: 0.9491 - accuracy: 0.6699
Epoch 8/10
782/782 - 4s - loss: 0.9000 - accuracy: 0.6888
Epoch 9/10
782/782 - 4s - loss: 0.8611 - accuracy: 0.7020
Epoch 10/10
782/782 - 4s - loss: 0.8264 - accuracy: 0.7147
157/157 [==============================] - 1s 3ms/step - loss: 0.9054 - accuracy: 0.6859<jupyter_text>Functional API<jupyter_code>def my_model():
    inputs = keras.layers.Input(shape=(32, 32, 3))
    x = layers.Conv2D(32, 3)(inputs)
    x = layers.BatchNormalization()(x)
    x = keras.activations.relu(x)
    x = layers.MaxPool2D()(x)
    x = layers.Conv2D(64, 5, padding='same')(x)
    x = layers.BatchNormalization()(x)
    x = keras.activations.relu(x)
    x = layers.Conv2D(128, 3)(x)
    x = layers.BatchNormalization()(x)
    x = keras.activations.relu(x)
    x = layers.Flatten()(x)
    x = layers.Dense(64, activation='relu')(x)
    outputs = layers.Dense(10)(x)
    model = keras.Model(inputs=inputs, outputs=outputs)
    return model

model = my_model()

model.compile(
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    optimizer=keras.optimizers.Adam(learning_rate=3e-4),
    metrics=['accuracy']
)

model.fit(x_train, y_train, batch_size=64, epochs=10, verbose=2)
model.evaluate(x_test, y_test, batch_size=64, verbose=True)<jupyter_output>Epoch 1/10
782/782 - 6s - loss: 1.2912 - accuracy: 0.5447
Epoch 2/10
782/782 - 5s - loss: 0.8926 - accuracy: 0.6867
Epoch 3/10
782/782 - 5s - loss: 0.7311 - accuracy: 0.7435
Epoch 4/10
782/782 - 5s - loss: 0.6168 - accuracy: 0.7836
Epoch 5/10
782/782 - 5s - loss: 0.5288 - accuracy: 0.8152
Epoch 6/10
782/782 - 5s - loss: 0.4407 - accuracy: 0.8468
Epoch 7/10
782/782 - 5s - loss: 0.3702 - accuracy: 0.8722
Epoch 8/10
782/782 - 5s - loss: 0.3001 - accuracy: 0.8983
Epoch 9/10
782/782 - 5s - loss: 0.2435 - accuracy: 0.9197
Epoch 10/10
782/782 - 5s - loss: 0.1935 - accuracy: 0.9373
157/157 [==============================] - 1s 4ms/step - loss: 0.9196 - accuracy: 0.7298