Skip to content

Commit f23ebb7

Browse files
committed
update week 5
1 parent c309e59 commit f23ebb7

File tree

8 files changed

+1473
-2467
lines changed

8 files changed

+1473
-2467
lines changed

doc/pub/week5/html/week5-bs.html

Lines changed: 141 additions & 228 deletions
Large diffs are not rendered by default.

doc/pub/week5/html/week5-reveal.html

Lines changed: 130 additions & 215 deletions
Large diffs are not rendered by default.

doc/pub/week5/html/week5-solarized.html

Lines changed: 137 additions & 222 deletions
Large diffs are not rendered by default.

doc/pub/week5/html/week5.html

Lines changed: 137 additions & 222 deletions
Large diffs are not rendered by default.
0 Bytes
Binary file not shown.

doc/pub/week5/ipynb/week5.ipynb

Lines changed: 858 additions & 1466 deletions
Large diffs are not rendered by default.

doc/pub/week5/pdf/week5.pdf

-1.86 KB
Binary file not shown.

doc/src/week5/week5.do.txt

Lines changed: 70 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,76 @@ input to a hidden layer, such that each neuron in the final pooling
296296
layer is connected to every single neuron in the hidden layer. This
297297
then serves as input to the output layer, e.g. a softmax output for
298298
classification.
299-
299+
300+
!split
301+
===== Initialize TensorFlow =====
302+
303+
!bc pycod
304+
from tensorflow.keras import datasets, layers, models
305+
from tensorflow.keras.layers import Input
306+
from tensorflow.keras.models import Sequential #This allows appending layers to existing models
307+
from tensorflow.keras.layers import Dense #This allows defining the characteristics of a particular layer
308+
from tensorflow.keras import optimizers #This allows using whichever optimiser we want (sgd,adam,RMSprop)
309+
from tensorflow.keras import regularizers #This allows using whichever regularizer we want (l1,l2,l1_l2)
310+
from tensorflow.keras.utils import to_categorical #This allows using categorical cross entropy as the cost function
311+
!ec
312+
313+
314+
!split
315+
===== Example of how we can up a model (without a specific image) =====
316+
317+
The 6 lines of code below define the convolutional base using a common pattern: a stack of Conv2D and MaxPooling2D layers.
318+
319+
As input, a CNN takes tensors of shape (image_height, image_width,
320+
color_channels), ignoring the batch size. If you are new to these
321+
dimensions, color_channels refers to (R,G,B). In this example, you
322+
will configure the CNN to process inputs of shape (32, 32, 3) as an
323+
example. You can do this by passing the argument input_shape to our
324+
first layer.
325+
326+
!bc pycod
327+
model = models.Sequential()
328+
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
329+
model.add(layers.MaxPooling2D((2, 2)))
330+
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
331+
model.add(layers.MaxPooling2D((2, 2)))
332+
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
333+
334+
# Here we display the architecture of our model so far.
335+
336+
model.summary()
337+
!ec
338+
You can see that the output of every Conv2D and MaxPooling2D layer is a 3D tensor of shape (height, width, channels). The width and height dimensions tend to shrink as you go deeper in the network. The number of output channels for each Conv2D layer is controlled by the first argument (e.g., 32 or 64). Typically, as the width and height shrink, you can afford (computationally) to add more output channels in each Conv2D layer.
339+
340+
341+
342+
343+
!split
344+
===== Add Dense layers on top =====
345+
346+
To complete this model, you will feed the last output tensor from the
347+
convolutional base (of shape (4, 4, 64)) into one or more Dense layers
348+
to perform classification. Dense layers take vectors as input (which
349+
are 1D), while the current output is a 3D tensor. First, you will
350+
flatten (or unroll) the 3D output to 1D, then add one or more dense
351+
layers on top. The MNIST data discussed below has 10 output classes, so we would use a final dense
352+
layer with 10 outputs and a softmax activation.
353+
354+
!bc pycod
355+
model.add(layers.Flatten())
356+
model.add(layers.Dense(64, activation='relu'))
357+
model.add(layers.Dense(10))
358+
Here's the complete architecture of our model.
359+
model.summary()
360+
!ec
361+
As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two Dense layers.
362+
363+
364+
300365

301366
!split
302367
===== Prerequisites: Collect and pre-process data =====
368+
Now we switch to the MNIST data set.
303369
!bc pycod
304370
# import necessary packages
305371
import numpy as np
@@ -354,10 +420,6 @@ from tensorflow.keras.layers import Dense #This allows defining the ch
354420
from tensorflow.keras import optimizers #This allows using whichever optimiser we want (sgd,adam,RMSprop)
355421
from tensorflow.keras import regularizers #This allows using whichever regularizer we want (l1,l2,l1_l2)
356422
from tensorflow.keras.utils import to_categorical #This allows using categorical cross entropy as the cost function
357-
#from tensorflow.keras import Conv2D
358-
#from tensorflow.keras import MaxPooling2D
359-
#from tensorflow.keras import Flatten
360-
361423
from sklearn.model_selection import train_test_split
362424

363425
# representation of labels
@@ -372,7 +434,7 @@ X_train, X_test, Y_train, Y_test = train_test_split(inputs, labels, train_size=t
372434
!ec
373435

374436
!split
375-
===== Running with Keras =====
437+
===== Running with Keras and setting up the model =====
376438

377439
!bc pycod
378440
def create_convolutional_neural_network_keras(input_shape, receptive_field,
@@ -386,7 +448,7 @@ def create_convolutional_neural_network_keras(input_shape, receptive_field,
386448
model.add(layers.Dense(n_neurons_connected, activation='relu', kernel_regularizer=regularizers.l2(lmbd)))
387449
model.add(layers.Dense(n_categories, activation='softmax', kernel_regularizer=regularizers.l2(lmbd)))
388450

389-
sgd = optimizers.SGD(lr=eta)
451+
sgd = optimizers.SGD(learning_rate=eta)
390452
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
391453

392454
return model
@@ -463,112 +525,6 @@ plt.show()
463525

464526

465527

466-
467-
!split
468-
===== The CIFAR01 data set =====
469-
470-
The CIFAR10 dataset contains 60,000 color images in 10 classes, with
471-
6,000 images in each class. The dataset is divided into 50,000
472-
training images and 10,000 testing images. The classes are mutually
473-
exclusive and there is no overlap between them.
474-
475-
!bc pycod
476-
import tensorflow as tf
477-
478-
from tensorflow.keras import datasets, layers, models
479-
import matplotlib.pyplot as plt
480-
481-
# We import the data set
482-
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
483-
484-
# Normalize pixel values to be between 0 and 1 by dividing by 255.
485-
train_images, test_images = train_images / 255.0, test_images / 255.0
486-
487-
!ec
488-
489-
490-
491-
!split
492-
===== Verifying the data set =====
493-
494-
To verify that the dataset looks correct, let's plot the first 25 images from the training set and display the class name below each image.
495-
496-
!bc pycod
497-
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
498-
'dog', 'frog', 'horse', 'ship', 'truck']
499-
plt.figure(figsize=(10,10))
500-
for i in range(25):
501-
plt.subplot(5,5,i+1)
502-
plt.xticks([])
503-
plt.yticks([])
504-
plt.grid(False)
505-
plt.imshow(train_images[i], cmap=plt.cm.binary)
506-
# The CIFAR labels happen to be arrays,
507-
# which is why you need the extra index
508-
plt.xlabel(class_names[train_labels[i][0]])
509-
plt.show()
510-
!ec
511-
512-
!split
513-
===== Set up the model =====
514-
515-
The 6 lines of code below define the convolutional base using a common pattern: a stack of Conv2D and MaxPooling2D layers.
516-
517-
As input, a CNN takes tensors of shape (image_height, image_width, color_channels), ignoring the batch size. If you are new to these dimensions, color_channels refers to (R,G,B). In this example, you will configure our CNN to process inputs of shape (32, 32, 3), which is the format of CIFAR images. You can do this by passing the argument input_shape to our first layer.
518-
519-
!bc pycod
520-
model = models.Sequential()
521-
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
522-
model.add(layers.MaxPooling2D((2, 2)))
523-
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
524-
model.add(layers.MaxPooling2D((2, 2)))
525-
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
526-
527-
# Let's display the architecture of our model so far.
528-
529-
model.summary()
530-
!ec
531-
532-
You can see that the output of every Conv2D and MaxPooling2D layer is a 3D tensor of shape (height, width, channels). The width and height dimensions tend to shrink as you go deeper in the network. The number of output channels for each Conv2D layer is controlled by the first argument (e.g., 32 or 64). Typically, as the width and height shrink, you can afford (computationally) to add more output channels in each Conv2D layer.
533-
534-
535-
536-
537-
!split
538-
===== Add Dense layers on top =====
539-
540-
To complete our model, you will feed the last output tensor from the
541-
convolutional base (of shape (4, 4, 64)) into one or more Dense layers
542-
to perform classification. Dense layers take vectors as input (which
543-
are 1D), while the current output is a 3D tensor. First, you will
544-
flatten (or unroll) the 3D output to 1D, then add one or more Dense
545-
layers on top. CIFAR has 10 output classes, so you use a final Dense
546-
layer with 10 outputs and a softmax activation.
547-
548-
!bc pycod
549-
model.add(layers.Flatten())
550-
model.add(layers.Dense(64, activation='relu'))
551-
model.add(layers.Dense(10))
552-
Here's the complete architecture of our model.
553-
554-
model.summary()
555-
!ec
556-
As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two Dense layers.
557-
558-
!split
559-
===== Compile and train the model =====
560-
561-
!bc pycod
562-
model.compile(optimizer='adam',
563-
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
564-
metrics=['accuracy'])
565-
566-
history = model.fit(train_images, train_labels, epochs=10,
567-
validation_data=(test_images, test_labels))
568-
569-
!ec
570-
571-
572528
!split
573529
===== Finally, evaluate the model =====
574530

@@ -591,7 +547,7 @@ print(test_acc)
591547
!split
592548
===== Building code using Pytorch =====
593549

594-
This code loads and normalizes the MNIST dataset. Thereafter it defines a CNN architecture with:
550+
This code loads and normalizes the MNIST dataset. Thereafter it defines a CNN architecture using PyTorch with:
595551
o Two convolutional layers
596552
o Max pooling
597553
o Dropout for regularization

0 commit comments

Comments
 (0)