<jupyter_start><jupyter_text>Convolutional Neural Networks<jupyter_code>import pandas as pd
import numpy as np

%matplotlib inline
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>MNIST<jupyter_code>from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data('/tmp/mnist.npz')
X_train.shape
X_test.shape
X_train[0]
plt.imshow(X_train[0], cmap='gray')
X_train = X_train.reshape(-1, 28*28)
X_test = X_test.reshape(-1, 28*28)
X_train.shape
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255.0
X_test /= 255.0
X_train[0]
from keras.utils.np_utils import to_categorical
y_train_cat = to_categorical(y_train)
y_test_cat = to_categorical(y_test)
y_train[0]
y_train_cat[0]
y_train_cat.shape
y_test_cat.shape
from keras.models import Sequential
from keras.layers import Dense
import keras.backend as K

K.clear_session()

model = Sequential()
model.add(Dense(512, input_dim=28*28, activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy',
             optimizer='rmsprop',
             metrics=['accuracy'])
h = model.fit(X_train, y_train_cat, batch_size=128, 
             epochs=10, verbose=1, validation_split=0.30)
plt.plot(h.history['acc'])
plt.plot(h.history['val_acc'])
plt.legend(['Training', 'Validation'])
plt.title('Accuracy')
plt.xlabel('Epochs')
test_accuracy = model.evaluate(X_test, y_test_cat)[1]
test_accuracy<jupyter_output>10000/10000 [==============================] - 1s 62us/step<jupyter_text>Tensor Math<jupyter_code>A = np.random.randint(10, size=(2, 3, 4, 5))
B = np.random.randint(10, size=(2, 3))
A
A[0, 1, 0, 3]
B<jupyter_output><empty_output><jupyter_text>A random colored image<jupyter_code>img = np.random.randint(255, size=(4, 4, 3), dtype='uint8')
img
plt.figure(figsize=(10, 5))
plt.subplot(221)
plt.imshow(img)
plt.title('All Channels Combined')

plt.subplot(222)
plt.imshow(img[:, :, 0], cmap='Reds')
plt.title('Red Channel')

plt.subplot(223)
plt.imshow(img[:, :, 1], cmap='Greens')
plt.title('Green Channel')

plt.subplot(224)
plt.imshow(img[:, :, 2], cmap='Blues')
plt.title("Blue Channel")<jupyter_output><empty_output><jupyter_text>Tensor Operations<jupyter_code>2 * A
A + A
A.shape
B.shape
np.tensordot(A, B, axes=([0, 1], [0, 1]))
np.tensordot(A, B, axes=([0], [0])).shape
a = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
            dtype='float32')
b = np.array([-1, 1], dtype='float32')
c = np.convolve(a, b)
a
b
c
plt.subplot(211)
plt.plot(a, 'o-')

plt.subplot(212)
plt.plot(c, 'o-')<jupyter_output><empty_output><jupyter_text>Image Filters with Convolutions<jupyter_code>from scipy.ndimage.filters import convolve
from scipy.signal import convolve2d
from scipy import misc
img = misc.ascent()
img.shape
plt.imshow(img, cmap='gray')
h_kernel = np.array([[1, 2, 1],
                    [0, 0, 0],
                    [-1, -2, -1]])
plt.imshow(h_kernel, cmap='gray')
res = convolve2d(img, h_kernel)
plt.imshow(res, cmap='gray')<jupyter_output><empty_output><jupyter_text>Convolutional Neural Networks<jupyter_code>from keras.layers import Conv2D
img.shape
plt.figure(figsize=(5, 5))
plt.imshow(img, cmap='gray')
img_tensor = img.reshape((1, 512, 512, 1))
model = Sequential()
model.add(Conv2D(1, (3, 3), strides=(2, 1), 
                input_shape=(512, 512, 1)))
model.compile('adam', 'mse')
img_pred_tensor = model.predict(img_tensor)
img_pred_tensor.shape
img_pred = img_pred_tensor[0, :, :, 0]
plt.imshow(img_pred, cmap='gray')
weights = model.get_weights()
weights[0].shape
plt.imshow(weights[0][:, :, 0, 0], cmap='gray')
weights[0] = np.ones(weights[0].shape)
model.set_weights(weights)
img_pred_tensor = model.predict(img_tensor)
img_pred = img_pred_tensor[0, :, :, 0]
plt.imshow(img_pred, cmap='gray')
model = Sequential()
model.add(Conv2D(1, (3, 3), input_shape=(512, 512, 1), 
                padding='same'))
model.compile('adam', 'mse')

img_pred_tensor = model.predict(img_tensor)

img_pred_tensor.shape<jupyter_output><empty_output><jupyter_text>Pooling Layers<jupyter_code>from keras.layers import MaxPool2D, AvgPool2D
model = Sequential()
model.add(MaxPool2D((5, 5), input_shape=(512, 512, 1)))
model.compile('adam', 'mse')
img_pred = model.predict(img_tensor)[0, :, :, 0]
plt.imshow(img_pred, cmap='gray')
model = Sequential()
model.add(AvgPool2D((5, 5), input_shape=(512, 512, 1)))
model.compile('adam', 'mse')
img_pred = model.predict(img_tensor)[0, :, :, 0]
plt.imshow(img_pred, cmap='gray')<jupyter_output><empty_output><jupyter_text>Final Architecture<jupyter_code>X_train = X_train.reshape(-1, 28, 28, 1)
X_test = X_test.reshape(-1, 28, 28, 1)
X_train.shape
from keras.layers import Flatten, Activation
K.clear_session()

model = Sequential()

model.add(Conv2D(32, (3, 3), input_shape=(28, 28, 1)))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Activation('relu'))

model.add(Flatten())

model.add(Dense(128, activation='relu'))

model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy',
             optimizer='rmsprop',
             metrics=['accuracy'])
model.summary()
model.fit(X_train, y_train_cat, batch_size=128, epochs=2,
         verbose=1, validation_split=0.3)
model.evaluate(X_test, y_test_cat)<jupyter_output>10000/10000 [==============================] - 2s 158us/step
{'fraction_non_alphanumeric': 0.1818848551521819, 'fraction_numerical': 0.05005500550055005, 'mean_word_length': 6.140052356020942, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '322555', 'license': 'MIT', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2190, 'n_words': 386, 'path': 'CNN Code Along.ipynb', 'repo_name': 'psd314/zero_to_deep_learning', 'size': '5454'}