<jupyter_start><jupyter_text>Importing Libraries<jupyter_code># loading libraries
# keras
import keras
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
from keras import regularizers
from keras.datasets import fashion_mnist
from keras.layers.normalization import BatchNormalization

# sklearn
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedShuffleSplit

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<jupyter_output>Using TensorFlow backend.<jupyter_text>Task 1<jupyter_code># loading the data from sklearn digits
digits = load_digits()
print(digits.data.shape)

X = digits.data
y = digits.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, stratify = y, shuffle = True)

num_classes = 10
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
# creating an initial model with 2 hidden layers and regularization paratmer = 0.01
def create_model(reg_parameter = 0.01, hidden_size = 32):  
  model = Sequential([
      Dense(hidden_size, input_shape=(64,), kernel_regularizer = regularizers.l2(reg_parameter)),
      Activation('relu'),
      Dense(int(hidden_size/2), kernel_regularizer = regularizers.l2(reg_parameter)),
      Activation('relu'),
      Dense(10),
      Activation('softmax')])

  model.compile("adam", "categorical_crossentropy", metrics=['accuracy'])
  return model
model = create_model()
model.fit(X_train, y_train, batch_size=128, epochs=10, verbose=1)
score = model.evaluate(X_test, y_test, verbose=0)
print("Test loss: {:.3f}".format(score[0]))
print("Test Accuracy: {:.3f}".format(score[1]))
# grid search for l2 regularization parameter and hidden layer size
# create model
model = KerasClassifier(build_fn=create_model, epochs=10, batch_size=128, verbose=0)
# define the grid search parameters
reg_param_list = [0.001, 0.01, 0.05, 0.1, 0.5, 1, 10, 100]
hidden_layers = [16, 32, 64, 128, 256]
param_grid = dict(hidden_size=hidden_layers, reg_parameter=reg_param_list)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv = StratifiedShuffleSplit(n_splits = 1, test_size = 0.2))
grid_result = grid.fit(X_train, y_train)
# summarize results
print("Best training score: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
print("Testing score: %f" % grid.score(X_test, y_test))
res = pd.DataFrame(grid.cv_results_)
res.pivot_table(index=["param_reg_parameter", "param_hidden_size"],
                values=["mean_test_score"])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10695375926579598, 'fraction_numerical': 0.02647370278856336, 'mean_word_length': 6.4188481675392675, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '550789', 'license': 'MIT', 'n_tokens_mistral': 1016, 'n_tokens_neox': 946, 'n_words': 230, 'path': 'Convolution Neural Networks/task1.ipynb', 'repo_name': 'Prasham8897/Applied_Machine_Learning', 'size': '2833'}