Different types of neural networks, for specific tasks:
A node is like a simple decision-making unit that:
each input gets a value proportional to its weight, and if the total values cross a certain threshold the node “fires” or produces an output
Dense layer:
We can add an activation to a Dense layer:
Stand alone activation layers:
Dropout layer:
Normalization layer (to stabilize and speed up training):
Regularization Layers (L1 and L2), control model complexity.
L1 (Lasso) regularization – adds the absolute value of the coefficients as penalty, promoting sparsity. It sets some coefficients to zero, which results in feature selection.
L2 (Ridge) regularization – adds the squared value of the coefficients as penalty. It shrinks coefficients towards zero.
In addition to normalization layers (to stabilize training) and dropout layers (to prevent overfitting), here are the layers we use in CNNs:
Convolutional Layers, 1D:
Convolutional Layers, 2D:
Convolutional Layers, 3D:
Pooling (max):
Keras has a handy image_dataset_from_directory()
method. We have to speficy batch size, image height and width (the method will resize all images for us):
We can get the class names from the data:
Let’s rescale the pixel values, and include Conv2D
and tf.keras.layers.MaxPooling2D
layers to process the 2D data. Then we include a Dropout
layer. Flatten
and Dense
are layers we have used for the digits problem. Then our final Dense
has 5 nodes because we have 5 classes.
model = tf.keras.Sequential([
tf.keras.layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(num_classes)
])
We will compile our model like you did before:
And then fit our training data:
Here’s the code to test the model with a new image:
img = tf.keras.utils.load_img("test_flowers/tulips.jpeg",
target_size=(img_height, img_width))
img_array = tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # create a batch
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
print(
"This image most likely belongs to {} with a {:.2f} percent confidence."
.format(class_names[np.argmax(score)], 100 * np.max(score))
)