<jupyter_start><jupyter_text>Build a CNN Y-net with two independent inputs  using Keras functional API *  Read & convert the data<jupyter_code>(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train.shape
# Show a randomly selected pic from the dataset
plt.imshow(X_train[np.random.randint(60000)], cmap = 'gray')
plt.show()
# No. of labels
n_labels = len(np.unique(y_train))
n_labels
# Image size
img_size = X_train.shape[1]
# Convert labels to categoroical
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
# Reshape and noemalize input imgs
X_train = X_train.reshape([-1, img_size, img_size, 1])
X_test = X_test.reshape([-1, img_size, img_size, 1])
# Normalize 
X_train = X_train.astype('float32') / 255
X_test = X_test.astype('float32') / 255<jupyter_output><empty_output><jupyter_text>*  Define net params<jupyter_code>input_shape = (img_size, img_size, 1)
batch_size = 32
kernel_size = 3
dropout = .4
n_filters = 32<jupyter_output><empty_output><jupyter_text>*  Build the model<jupyter_code># Left branch
inputs_left = Input(shape = input_shape)
x1 = inputs_left
filters = n_filters

# Build 3 layers of Conv2D-Dropout-Pooling
for i in range(3):
    x1 = Conv2D(filters = filters, 
                kernel_size = kernel_size,
                padding = 'same', 
                activation = 'relu')(x1)
    x1 = Dropout(dropout)(x1)
    x1 = MaxPooling2D()(x1)
    filters *= 2
# Right branch
inputs_right = Input(shape = input_shape)
x2 = inputs_right
filters = n_filters

# Build 3 layers of Conv2D-Dropout-Pooling
for i in range(3):
    x2 = Conv2D(filters = filters, 
                kernel_size = kernel_size,
                padding = 'same', 
                activation = 'relu',
                dilation_rate = 2)(x2)
    x2 = Dropout(dropout)(x2)
    x2 = MaxPooling2D()(x2)
    filters *= 2
# Merge inputs
y = concatenate([x1, x2])

# Flatten & dropout
y = Flatten()(y)
y = Dropout(dropout)(y)

output = Dense(n_labels, activation = 'softmax')(y)
# Build the model
model = Model([inputs_left, inputs_right], output)
plot_model(model, to_file = 'model_01.png')
png = plt.imread('model_01.png')
plt.figure(figsize = (33, 16.5))
plt.imshow(png)
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>*  Compile the model<jupyter_code>model.compile(loss = 'categorical_crossentropy',
              optimizer = 'adam', 
              metrics = ['accuracy'])<jupyter_output><empty_output><jupyter_text>*  Fit the model<jupyter_code>model.fit([X_train, X_train], y_train,
          validation_data = ([X_test, X_test], y_test),
          epochs = 3, 
          batch_size = batch_size)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1247182569496619, 'fraction_numerical': 0.024793388429752067, 'mean_word_length': 3.968283582089552, 'pattern_counts': {'":': 0, '<': 21, '<?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': '497558', 'license': 'MIT', 'n_tokens_mistral': 1042, 'n_tokens_neox': 983, 'n_words': 212, 'path': '.ipynb_checkpoints/01_CNN-Y-net_(functional_API)-checkpoint.ipynb', 'repo_name': 'AlxndrMlk/KerasExcercises', 'size': '2662'}