<jupyter_start><jupyter_text>Load libraries<jupyter_code># data tools
import os
import numpy as np
import matplotlib.pyplot as plt

# sklearn tools
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import classification_report

# tf tools
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (Conv2D, 
                                     MaxPooling2D, 
                                     Activation, 
                                     Flatten, 
                                     Dense)
from tensorflow.keras.utils import plot_model
from tensorflow.keras.optimizers import SGD
from tensorflow.keras import backend as K
def plot_history(H, epochs):
    # visualize performance
    plt.style.use("fivethirtyeight")
    plt.figure()
    plt.plot(np.arange(0, epochs), H.history["loss"], label="train_loss")
    plt.plot(np.arange(0, epochs), H.history["val_loss"], label="val_loss")
    plt.plot(np.arange(0, epochs), H.history["accuracy"], label="train_acc")
    plt.plot(np.arange(0, epochs), H.history["val_accuracy"], label="val_acc")
    plt.title("Training Loss and Accuracy")
    plt.xlabel("Epoch #")
    plt.ylabel("Loss/Accuracy")
    plt.legend()
    plt.tight_layout()
    plt.show()<jupyter_output><empty_output><jupyter_text>Load cifar10 data<jupyter_code>((trainX, trainY), (testX, testY)) = cifar10.load_data()
trainX = trainX.astype("float") / 255.
testX = testX.astype("float") / 255.
# integers to one-hot vectors
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.fit_transform(testY)

# initialize label names for CIFAR-10 dataset
labelNames = ["airplane", "automobile", "bird", 
              "cat", "deer", "dog", "frog", "horse", 
              "ship", "truck"]<jupyter_output><empty_output><jupyter_text>ShallowNet  ShallowNet architecture: >INPUT => CONV => ReLU => FC<jupyter_code># initialise model
model = Sequential()

# define CONV => RELU layer
model.add(Conv2D(32, (3, 3), 
                 padding="same", 
                 input_shape=(32, 32, 3)))
model.add(Activation("relu"))

# softmax classifier
model.add(Flatten())
model.add(Dense(10))
model.add(Activation("softmax"))<jupyter_output><empty_output><jupyter_text>__Compile model__<jupyter_code>opt = SGD(lr =.01)
model.compile(loss="categorical_crossentropy",
              optimizer=opt,
              metrics=["accuracy"])<jupyter_output><empty_output><jupyter_text>__Model summary__<jupyter_code>model.summary()<jupyter_output><empty_output><jupyter_text>__Plot model__<jupyter_code>plot_model(model, show_shapes=True, show_layer_names=True)<jupyter_output><empty_output><jupyter_text>__Train model__<jupyter_code>H = model.fit(trainX, trainY, 
              validation_data=(testX, testY), 
              batch_size=32,
              epochs=40,
              verbose=1)<jupyter_output><empty_output><jupyter_text>__Evaluate model__<jupyter_code>predictions = model.predict(testX, batch_size=32)
print(classification_report(testY.argmax(axis=1),
                            predictions.argmax(axis=1),
                            target_names=labelNames))<jupyter_output><empty_output><jupyter_text>plot_model(H)  LeNet LeNet architecture:>INPUT => CONV => ReLU => MAXPOOL => CONV => ReLU => MAXPOOL => FC => ReLU => FC __Define model__<jupyter_code># define model
model = Sequential()

# first set of CONV => RELU => POOL
model.add(Conv2D(32, (3, 3), 
                 padding="same", 
                 input_shape=(32, 32, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), 
                       strides=(2, 2)))

# second set of CONV => RELU => POOL
model.add(Conv2D(50, (5, 5), 
                 padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), 
                       strides=(2, 2)))

# FC => RELU
model.add(Flatten())
model.add(Dense(500))
model.add(Activation("relu"))

# softmax classifier
model.add(Dense(10))
model.add(Activation("softmax"))<jupyter_output><empty_output><jupyter_text>__Compile model__<jupyter_code>opt = SGD(lr=0.01)
model.compile(loss="categorical_crossentropy",
              optimizer=opt,
              metrics=["accuracy"])<jupyter_output><empty_output><jupyter_text>__Model summary__<jupyter_code>model.summary()<jupyter_output><empty_output><jupyter_text>__Plot model__<jupyter_code>plot_model(model, show_shapes=True, show_layer_names=True)<jupyter_output><empty_output><jupyter_text>__Train model__<jupyter_code># train model
H = model.fit(trainX, trainY, 
              validation_data=(testX, testY), 
              batch_size=32,
              epochs=20,
              verbose=1)
plot_model(H,20)
predictions = model.predict(testX, batch_size=32)
print(classification_report(testY.argmax(axis=1),
                            predictions.argmax(axis=1),
                            target_names=labelNames))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13100524828421478, 'fraction_numerical': 0.017561566410981024, 'mean_word_length': 3.6922348484848486, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '383591', 'license': 'MIT', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1665, 'n_words': 265, 'path': 'notebooks/session9.ipynb', 'repo_name': 'sofieditmer/cds-visual', 'size': '4954'}