<jupyter_start><jupyter_text>https://colab.research.google.com/drive/1QehP6u9S494MeKZ4msD77OypupcXo5Oi<jupyter_code>from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.utils import np_utils
from keras.layers import Conv2D, MaxPooling2D, Flatten
from keras.utils import to_categorical
import numpy as np
X_train=np.array([[[1,2,3,4],[2,3,4,5],[5,6,7,8],[1,3,4,5]],[[-1,2,3,-4],[2,-3,4,5],[-5,6,-7,8],[-1,-3,-4,-5]]])
y_train=np.array([0,1])
X_train = X_train / 8
#y_train = to_categorical(y_train)
X_train = X_train.reshape(X_train.shape[0],X_train.shape[1],X_train.shape[1],1 ).astype('float32')
model = Sequential()
model.add(Conv2D(1, (3,3), input_shape=(4,4,1),activation='relu', name='Convolution_layer'))
model.add(MaxPooling2D(pool_size=(2, 2), name='Pooling_layer'))
model.add(Flatten(name='Flatten_layer'))
model.add(Dense(1, activation='sigmoid', name = 'Output_layer'))
model.compile(loss='binary_crossentropy', optimizer='adam',metrics=['accuracy'])
model.summary()
history = model.fit(X_train, y_train, epochs = 1000)
history_dict = history.history
loss_values = history_dict['loss']
acc_values = history_dict['acc']
epochs = range(1, len(loss_values) + 1)

import matplotlib.ticker as mtick
import matplotlib.pyplot as plt
%matplotlib inline
plt.subplot(211)
plt.plot(epochs, history.history['loss'], 'bo', label='Training loss')
plt.title('Training loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.grid('off')
plt.show()
plt.subplot(212)
plt.plot(epochs, history.history['acc'], 'bo', label='Training accuracy')
plt.title('Training accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.gca().set_yticklabels(['{:.0f}%'.format(x*100) for x in plt.gca().get_yticks()]) 
plt.legend()
plt.grid('off')
plt.show()
model.weights
model.get_weights()[0].shape
model.predict(X_train[0].reshape(1,4,4,1))
model.get_weights()
X_train[0].shape[0]-model.get_weights()[0].shape[0]
sumprod = []
for i in range(X_train[0].shape[0]-model.get_weights()[0].shape[0]+1):
  for j in range(X_train[0].shape[0]-model.get_weights()[0].shape[0]+1):
    img_subset = np.array(X_train[0,i:(i+3),j:(j+3),0])
    model_filter = model.get_weights()[0].reshape(3,3)
    val = np.sum(img_subset*model_filter) + model.get_weights()[1]
    sumprod.append(val)
  
sumprod= np.array(sumprod).reshape(2,2,1)
sumprod = np.where(sumprod>0,sumprod,0)
pooling_layer_output = np.max(sumprod)
intermediate_output_value = pooling_layer_output*model.get_weights()[2]+model.get_weights()[3]
1/(1+np.exp(-intermediate_output_value))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19370924434215572, 'fraction_numerical': 0.04487917146144994, 'mean_word_length': 8.118881118881118, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '386014', 'license': 'MIT', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1047, 'n_words': 144, 'path': 'Chapter04/CNN_working_details.ipynb', 'repo_name': 'PacktPublishing/Neural-Networks-with-Keras-Cookbook', 'size': '2607'}