You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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
+
300
365
301
366
!split
302
367
===== Prerequisites: Collect and pre-process data =====
368
+
Now we switch to the MNIST data set.
303
369
!bc pycod
304
370
# import necessary packages
305
371
import numpy as np
@@ -354,10 +420,6 @@ from tensorflow.keras.layers import Dense #This allows defining the ch
354
420
from tensorflow.keras import optimizers #This allows using whichever optimiser we want (sgd,adam,RMSprop)
355
421
from tensorflow.keras import regularizers #This allows using whichever regularizer we want (l1,l2,l1_l2)
356
422
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
-
361
423
from sklearn.model_selection import train_test_split
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.
# 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.
0 commit comments