<jupyter_start><jupyter_text>Creating CNN with Functional API<jupyter_code>import numpy as np
from tensorflow.keras.layers import Dense, Dropout, Input
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
(x_train,y_train),(x_test,y_test) = mnist.load_data()
num_labels = len(np.unique(y_train))
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
image_size = x_train.shape[1]

x_train = np.reshape(x_train,[-1, image_size, image_size, 1])
x_test = np.reshape(x_test,[-1, image_size, image_size, 1])
x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255
input_shape = (image_size,image_size,1)
batch_size = 128
kernel_size = 3
filters = 64
dropout = 0.3
inputs = Input(shape=input_shape)
y = Conv2D(filters=filters,
          kernel_size = kernel_size,
          activation='relu')(inputs)
y = MaxPooling2D()(y)
y = Conv2D(filters=filters,
          kernel_size=kernel_size,
          activation='relu')(y)
y = MaxPooling2D()(y)
y = Conv2D(filters=filters,
         kernel_size=kernel_size,
         activation='relu')(y)
y = Flatten()(y)
y = Dropout(dropout)(y)
outputs = Dense(num_labels,activation='softmax')(y)
model = Model(inputs=inputs, outputs=outputs)
model.summary()
model.compile(loss='categorical_crossentropy',
             optimizer='adam',
             metrics='accuracy')
model.fit(x_train,y_train,
         validation_data=(x_test,y_test),
         epochs=15,
         batch_size = batch_size)
score = model.evaluate(x_test,y_test,batch_size = batch_size,verbose = 2)
print("\nTest accuracy: %.1f%%"%(100.0*score[1]))<jupyter_output>Test accuracy: 99.0%<jupyter_text>2-input and 1-output model (Y-model with Functional API)<jupyter_code>from tensorflow.keras.layers import concatenate
from tensorflow.keras.utils import plot_model
(x_train,y_train),(x_test,y_test) = mnist.load_data()

num_labels = len(np.unique(y_train))
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

image_size = x_train.shape[1]

x_train = np.reshape(x_train,[-1, image_size, image_size, 1])
x_test = np.reshape(x_test,[-1, image_size, image_size, 1])

x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255
input_shape = (image_size,image_size,1)
batch_size = 128
kernel_size = 3
dropout = 0.4
n_filters = 32
#left branch
left_inputs = Input(shape = input_shape)
x = left_inputs
filters = n_filters

# 3 - layers of Conv-Dropout-Maxpool
for i in range(3):
    x = Conv2D(filters=filters,
              kernel_size = kernel_size,
              padding='same',
              activation = 'relu')(x)
    x = Dropout(dropout)(x)
    x = MaxPooling2D()(x)
    filters *= 2

#=========================================================
    
#right branch
right_inputs = Input(shape=input_shape)
y = right_inputs

# 3 - layers of Conv-Dropout-Maxpool
for i in range(3):
    y = Conv2D(filters=filters,
              kernel_size = kernel_size,
              padding='same',
              activation = 'relu',
              dilation_rate=2)(y)
    
    y = Dropout(dropout)(y)
    y = MaxPooling2D()(y)
    filters *= 2


import os
y = concatenate([x,y])

y=Flatten()(y)
y=Dropout(dropout)(y)
outputs = Dense(num_labels,activation='softmax')(y)

model = Model([left_inputs,right_inputs],outputs)
# plot_model(model,show_shapes=True)
model.summary()
model.compile(loss='categorical_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])
model.fit([x_train,x_train],y_train,
          validation_data=([x_test,x_test],y_test),
         epochs= 1,
         batch_size = batch_size)
score = model.evaluate([x_test,x_test],
                      y_test,
                      batch_size=batch_size,
                      verbose=2)
print("\nTest accuracy: %.1f%%"%(100.0*score[1]))<jupyter_output>79/79 - 29s - loss: 0.0836 - accuracy: 0.9867

Test accuracy: 98.7%
{'fraction_non_alphanumeric': 0.14396594892338507, 'fraction_numerical': 0.026790185277916875, 'mean_word_length': 4.1021711366538955, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '597375', 'license': 'Apache-2.0', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1437, 'n_words': 255, 'path': 'Advanced_DL_with_TensorFlow_2 ~ Book/Chapter_2.ipynb', 'repo_name': 'kushagras71/understanding_ML', 'size': '3994'}