Professional Documents
Culture Documents
Shusen Wang
Inner Products
Vector Inner Product
1 5
•𝐚= 2 and 𝐛= 6 .
3 7
4 8
• Inner product:
𝐚, 𝐛 = 𝐚. 𝐛 = ∑1 𝑎1 𝑏1 = 70.
Matrix Inner Product
1 3 5 7
•𝐀= and 𝐁= .
2 4 6 8
• Inner product:
𝐀, 𝐁 = ∑1 ∑7 𝑎17 𝑏17 = 70.
•𝐀= and 𝐁 = .
• Inner product:
𝐀, 𝐁 = ∑1 ∑7 ∑; 𝑎17; 𝑏17; .
3×3 patch
Answer: 5 − 3 + 1 × 5 − 3 + 1 = 9.
Convolution
Convolution:
Result
3×3
Convolution
Convolution:
Result
3×3
The value 4 is the inner product of the patch and the filter .
Convolution
Convolution:
Result
3×3
The value 3 is the inner product of the patch and the filter .
Convolution
Convolution:
Result
3×3
The value 4 is the inner product of the patch and the filter .
Convolution
Convolution:
Result
3×3
Convolution
Convolution:
Result
3×3
Convolution:
Result
2×2
Convolution
Convolution:
Dimensions:
• Input: 𝑑B×𝑑C
• Filter: 𝑘B×𝑘C
• Output: 𝑑B − 𝑘B + 1 × 𝑑C − 𝑘C + 1
Convolution
Edge
detection
Dimensions:
Dimensions:
Input Image
5×5
Convolution: Stride
Output
5×5 • In the previous examples, the stride is 1.
• The filter moves 1 step each time.
Filter (Kernel)
3×3
Dimensions:
Filter (Kernel)
3×3 • Input: 𝑑B×𝑑C
• Filter: 𝑘B×𝑘C
• Stride: s
FG H;G FJ H;J
Input Image Stride=2 • Output: +1 × +1
I I
5×5
Convolution for Tensors
Filter (tensor)
3×3×3 • The input is the order-3 tensor of
shape 𝑑B×𝑑C×3, e.g., a color image.
• The filter is a order-3 tensor of shape
3×3×3.
convolution
output
CNN: Convolutional Layer 1
convolution
output
CNN: Pooling Layer 1
convolution
output pooling
output
Pooling
Settings:
• Max Pooling.
• Pool size = 2×2.
• No overlap (stride = 2×2).
Pooling
Settings:
• Max Pooling.
• Pool size = 2×2.
• No overlap (stride = 2×2).
Settings:
• Average Pooling.
• Pool size = 2×2.
• No overlap (stride = 2×2).
Pooling
convolution
output pooling
output
CNN: Convolutional Layer 2
• Perform tensor convolution.
• Apply 5×5×10 tensor filter to the 12×12×10 tensor input.
• Twenty such filters è twenty 8×8 feature maps.
12×12×10 tensor
(order-3 tensor)
convolution
output pooling
convolution
output
output
Order-3 tensor
convolution
output pooling
convolution pooling
output
output output
CNN: Flatten (Vectorization)
convolution
output pooling
convolution pooling
output
output output
CNN: Fully-Connected Layers
convolution
output pooling
convolution pooling
output
output output
CNN: How Many Trainable Parameters?
• Convolutional layers
# Filters Filter shape Stride Zero-padding
• Pooling layers
MaxPool or AvgPool Pool size Pool stride
• Fully-connected layers
Width
• Activation functions
ReLU SoftMax Sigmoid
Hyper-Parameters for Training
• Loss function.
Cross-entropy for classification
L1 or L2 for regression (the labels are continuous)
• Random initialization.
Gaussian or uniform? Scaling
Implement a CNN Using Keras
1. Load and Process the MNIST Dataset
1. Install Keras by following https://keras.io/#installation
In
2. [1]:
Load the MNIST dataset.
from keras.datasets import mnist
Load data
In [1]:
(x_train, y_train), (x_test, y_test) = mnist.load_data()
from keras.datasets import mnist
print('Shape of x_train: ' + str(x_train.shape))
print('Shape
(x_train, of x_test:
y_train), ' + str(x_test.shape))
(x_test, y_test) = mnist.load_data()
print('Shape of y_train: ' + str(y_train.shape))
print('Shape of
print('Shape of y_test: ' +' str(y_test.shape))
x_train: + str(x_train.shape))
print('Shape of x_test: ' + str(x_test.shape))
/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWa
print('Shape
from ._conv of y_train:
import ' + str(y_train.shape))
register_converters as _register_converters
print('Shape of y_test:
Using TensorFlow backend. ' + str(y_test.shape))
/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36:
Shape of x_train: (60000, 28, 28) Futur
fromof._conv
Shape import
x_test: register_converters
(10000, 28, 28) as _register_converters
Shape of
Using y_train: backend.
TensorFlow (60000,)
Shape of y_test: (10000,)
Shape of x_train: (60000, 28, 28)
Shape of x_test: (10000, 28, 28)
1. Load and Process the MNIST Dataset
In [3]:
Reshape:numpy
import convert
asthe
np60000×28×28 dataset to a 60000×28×28×1
tensor.
# vectorize the images to 784-dim vectors
x_train_vec = x_train.reshape((60000, 28, 28, 1)) / 255.0
x_test_vec = x_test.reshape((10000, 28, 28, 1)) / 255.0
In [4]:
y_train_vec = to_one_hot(y_train)
y_test_vec = to_one_hot(y_test)
print('Shape of y_train_vec is ' + str(y_train_vec.shape))
rand_indices = np.random.permutation(60000)
train_indices = rand_indices[0:50000]
valid_indices = rand_indices[50000:60000]
x_valid_vec = x_train_vec[valid_indices, :, :, :]
y_valid_vec = y_train_vec[valid_indices, :]
x_train_vec = x_train_vec[train_indices, :, :, :]
y_train_vec = y_train_vec[train_indices, :]
convolution
output pooling
convolution pooling
output
output output
2.
Build
the
CNN 2. Build the CNN
Build a ConvNet using Keras and summarize the model.
In [6]:
from keras import models
from keras import layers
model = models.Sequential()
model.add(layers.Conv2D(10, (5, 5), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(20, (5, 5), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
Filter number
model.add(layers.Flatten()) Filter shape Default
model.add(layers.Dense(100, activation='relu')) • Stride: 1
model.add(layers.Dense(10, activation='softmax')) • Zero-padding: no padding
In [6]:
from keras import models
from keras import layers
model = models.Sequential()
model.add(layers.Conv2D(10, (5, 5), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(20, (5, 5), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
Default
model.add(layers.Flatten())
pool_size • Stride: pool_size
model.add(layers.Dense(100, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
In [6]:
from keras import models
from keras import layers
model = models.Sequential()
model.add(layers.Conv2D(10, (5, 5), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2))) Output: 12×12×10
model.add(layers.Conv2D(20, (5, 5), activation='relu')) Output: 8×8×20
model.add(layers.MaxPooling2D((2, 2))) Output: 4×4×20
model.add(layers.Flatten())
model.add(layers.Dense(100, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
In [6]:
from keras import models
from keras import layers
model = models.Sequential()
model.add(layers.Conv2D(10, (5, 5), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2))) Output: 12×12×10
model.add(layers.Conv2D(20, (5, 5), activation='relu')) Output: 8×8×20
model.add(layers.MaxPooling2D((2, 2))) Output: 4×4×20
model.add(layers.Flatten()) Output: 320
model.add(layers.Dense(100, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
In [6]:
from keras import models
from keras import layers
model = models.Sequential()
model.add(layers.Conv2D(10, (5, 5), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2))) Output: 12×12×10
model.add(layers.Conv2D(20, (5, 5), activation='relu')) Output: 8×8×20
model.add(layers.MaxPooling2D((2, 2))) Output: 4×4×20
model.add(layers.Flatten()) Output: 320
model.add(layers.Dense(100, activation='relu')) Output: 100
model.add(layers.Dense(10, activation='softmax')) Output: 10
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 24, 24, 10) 260
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 12, 12, 10) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 8, 8, 20) 5020
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 4, 4, 20) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 320) 0
_________________________________________________________________
dense_1 (Dense) (None, 100) 32100
_________________________________________________________________
dense_2 (Dense) (None, 10) 1010
=================================================================
Total params: 38,390
Trainable params: 38,390
Non-trainable params: 0
_________________________________________________________________
3. Train the CNN
3.
Train
the
Softmax
Classifier
Specify: optimization algorithm, learning rate (LR), loss function, and metric.
In [7]:
from keras import optimizers
model.compile(optimizers.RMSprop(lr=0.0001),
loss='categorical_crossentropy',
metrics=['accuracy'])
Epoch 1/50
50000/50000 [==============================] - 12s 240us/step
Epoch 44/50
2/50
239us/step - loss: 0.0283
1.2455 - acc: 0.9916
0.7201 - val_loss: 0.4805
0.0457 - val_acc: 0.9877
0.8707
50000/50000 [==============================] - 1s
50000/50000 [==============================] - 12s 241us/step
Epoch 45/50
3/50
235us/step - loss: 0.0275
0.3563 - acc: 0.9915 19us/step0.9172
0.8995 - val_loss: 0.2851 - lo
0.0488 - val_acc:
0.9868
Epoch 2/50
50000/50000 [==============================] - 12s 244us/step - loss: 0.0269
Epoch 46/50
4/50
0.2474 - acc: 0.9917
0.9271 - val_loss: 0.2230
0.0438 - val_acc: 0.9880
0.9365
50000/50000 [==============================] - 1s
50000/50000 [==============================] - 13s
12s 251us/step
241us/step - loss: 0.0262
0.1943 - acc: 0.9921 13us/step0.9481
0.9420 - val_loss: 0.1837 - lo
0.0465 - val_acc:
0.9877
⋯
Epoch 47/50
5/50
Epoch 3/50
50000/50000 [==============================] - 13s
Epoch 48/50
6/50
12s 254us/step
242us/step - loss: 0.0255
0.1603 - acc: 0.9924
0.9525 - val_loss: 0.1535
0.0466 - val_acc: 0.9869
0.9553
50000/50000
50000/50000 [==============================]
[==============================] -- 12s
13s 239us/step
255us/step -
- loss:
loss: 0.0238
0.1086 -
- acc:
acc: 0.9927
0.9671 - val_loss: 0.1105
0.0446 -
- val_acc:
val_acc:
0.9881
50000/50000 [==============================] - 1s
Epoch 9/50
- val_loss:
13us/step0.9678
- lo
50000/50000 [==============================] - 1s 13us/step - loss: 1
Epoch 50/50 4. Examine the Results
50000/50000 [==============================] - 1s 14us/step - loss: 1
Plot the accuracy against epochs (1 epoch = 1 pass over the data).
In [18]:
import matplotlib.pyplot as plt
%matplotlib inline
• Convolutional layers.
• Filter number.
• Filter shape.
• Stride (default: one).
• Zero-padding (default: no padding).
2. Convolutional Neural Network
• Convolutional layers.
• Filter number.
• Filter shape.
• Stride (default: one).
• Zero-padding (default: no padding).
• Activation functions.
• Pooling layers.
• MaxPool, AveragePool, etc.
• Pool size.
• Pool stride (default: pool size).
3. Build and Train CNN using Keras
• Build a network.
• Add Conv, Pool, FC layers.
• Compile the model.
• Specify optimization algorithm, loss function, and metrics.
• Fit the model on training data.