<jupyter_start><jupyter_text>Step 1: Load dataset<jupyter_code>num_classes = 10
digits = load_digits(n_class=num_classes)
x = digits.data
y = digits.target

H = 8
W = 8
C = 1
# reshape x into a numpy array of images of size: height x width x number_of_channels
### YOUR CODE HERE ###

print("data shape: ",x.shape)
print("class shape: ",y.shape)

# Split the data into training and testing sets
N_train = int(0.8 * x.shape[0])
x_train = x[:N_train,:]
y_train = y[:N_train]
x_test = x[N_train:,:]
y_test = y[N_train:]

# Convert training labels to one-hot vector

print("train label shape: ", y_train_onehot.shape)<jupyter_output><empty_output><jupyter_text>Step 2: Build a CNN model to classify digits<jupyter_code># First, define the based sequential model
model = keras.models.Sequential()

# Add the first convolutional layer:
# A 2D Convolution layer with: 
#     32 filters 
#     kernel size: 3x3
#     stride: 1
#     padding scheme: 'same'
#     use_bias: True
#     activation: relu
### YOUR CODE HERE ###

# Add the second convolutional layer:
# A 2D Convolution layer with: 
#     64 filters 
#     kernel size: 3x3
#     stride: 1
#     padding scheme: 'same'
#     use_bias: True
#     activation: relu
### YOUR CODE HERE ###


# Add dropout layer with rate = 0.75 
### YOUR CODE HERE ###

# Add 2D max pooling layer with pooling size = 2x2
### YOUR CODE HERE ###

# Add Flatten layer to reshape the output of the previous layer into 1D vectors
### YOUR CODE HERE ###

# Fully connected layers to perform the classification from the outputs of the previous layers
# Add a fully connected layer with 10 output units, and softmax activation
# (each hidden unit corresponds to one class, i.e. digit):
### YOUR CODE HERE ###


# Compile the model with 'categorical_crossentropy' loss function and 
# 'sgd' optimizer
model.compile(optimizer='sgd',
              loss='categorical_crossentropy',
              metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Step 3: Train the model<jupyter_code>model.fit(x_train, y_train_onehot, epochs=10, batch_size=32)<jupyter_output><empty_output><jupyter_text>Step 4: Evaluate the model<jupyter_code>scores = model.predict(x_test)
y_pred = np.argmax(scores, axis=1)
print("Accuracy={:.2f}".format(np.mean(y_pred == y_test)))<jupyter_output><empty_output><jupyter_text>Step 5: Visualize the classification results<jupyter_code>for selected_class in range(0,10):
    x_visualize = x_test[y_pred == selected_class]
    # plot some images of the digits
    n_img_per_row = 10
    img = np.zeros((10 * n_img_per_row, 10 * n_img_per_row))
    for i in range(n_img_per_row):
        ix = 10 * i + 1
        for j in range(n_img_per_row):
            iy = 10 * j + 1
            if i * n_img_per_row + j < len(x_visualize):
                img[ix:ix + 8, iy:iy + 8] = x_visualize[i * n_img_per_row + j].reshape((8, 8))
    plt.imshow(img, cmap=plt.cm.binary)
    plt.xticks([])
    plt.yticks([])
    plt.title('Test images predicted as "{:}"'.format(selected_class))
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11672644277795892, 'fraction_numerical': 0.019237039452233453, 'mean_word_length': 4.00489396411093, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '626773', 'license': 'Apache-2.0', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1062, 'n_words': 309, 'path': '8.- convolutional nn/enunciated/2.CNN.ipynb', 'repo_name': 'nik1168/data-representation-reduction-exercises', 'size': '3067'}