Code Explanation
Task – Deep Learning Models Code Explanation
ANN – Artificial Neural Network
1. from [Link] import Sequential
This line imports the Sequential model from Keras.
Sequential allows you to add layers one by one, like stacking blocks.
2. from [Link] import Dense
This brings in the Dense layer (also called a fully connected layer).
It means every neuron in this layer is connected to all neurons in the next.
3. model = Sequential()
This starts an empty neural network model.
Layers will now be added to this model.
4. [Link](Dense(64, activation='relu',
input_shape=(input_dim,)))
Adds the first hidden layer with 64 neurons.
ReLU helps the model learn better.
input_shape tells the model how many input values to expect.
5. [Link](Dense(1, activation='sigmoid')
Adds the final layer with 1 neuron.
Sigmoid is used for outputs between 0 and 1 (yes/no).
6. [Link](optimizer='adam',
loss='binary_crossentropy', metrics=['accuracy'])
Prepares the model to learn using Adam optimizer.
Uses binary crossentropy for binary classification.
Tracks accuracy.
7. [Link](X_train, y_train, epochs=10, batch_size=32)
Trains the model with input and answer data.
Goes through the data 10 times in batches of 32.
CNN – Convolutional Neural Network
1. from [Link] import Conv2D, MaxPooling2D, Flatten,
Dense
Brings in all necessary CNN layers:
- Conv2D: detects patterns
- MaxPooling2D: reduces image size
- Flatten: reshapes data
- Dense: normal layers
2. model = Sequential()
Starts a new CNN model.
3. [Link](Conv2D(32, (3, 3), activation='relu',
input_shape=(28, 28, 1)))
Adds a Conv layer with 32 filters.
Each filter looks at 3x3 parts of the image.
ReLU is the activation function.
Input is 28x28 grayscale image.
4. [Link](MaxPooling2D(pool_size=(2, 2)))
Downsamples the image to reduce size while keeping important parts.
5. [Link](Flatten())
Flattens 2D image data into 1D before sending to Dense layers.
6. [Link](Dense(64, activation='relu'))
Adds a Dense hidden layer to learn patterns.
7. [Link](Dense(10, activation='softmax'))
Final layer with 10 outputs for 10 classes.
Softmax converts output into probabilities.
8. [Link](optimizer='adam',
loss='categorical_crossentropy', metrics=['accuracy'])
Compiles the model for multiclass classification.
9. [Link](X_train, y_train, epochs=10, batch_size=32)
Trains the model for 10 epochs.
RNN – Recurrent Neural Network (for text)
1. from [Link] import Embedding, SimpleRNN, Dense
Loads layers for sequence models.
Embedding turns words into number vectors.
2. model = Sequential()
Starts the model.
3. [Link](Embedding(input_dim=5000, output_dim=64))
Turns each word into a 64-value vector.
Only the 5000 most common words are used.
4. [Link](SimpleRNN(64))
Adds an RNN layer that remembers previous steps.
5. [Link](Dense(1, activation='sigmoid'))
Adds the output layer for binary prediction.
6. [Link](optimizer='adam',
loss='binary_crossentropy', metrics=['accuracy'])
Compiles the model.
7. [Link](X_train, y_train, epochs=5, batch_size=64)
Trains the RNN model.
LSTM – Long Short-Term Memory
1. from [Link] import LSTM
Imports LSTM layer which remembers long-term info.
2. model = Sequential()
Starts the model.
3. [Link](Embedding(input_dim=5000, output_dim=64))
Turns words into 64-dimension vectors.
4. [Link](LSTM(128))
Adds LSTM layer with 128 units to remember important words.
5. [Link](Dense(1, activation='sigmoid'))
Output layer for binary output.
6. [Link](optimizer='adam',
loss='binary_crossentropy', metrics=['accuracy'])
Compiles the model.
7. [Link](X_train, y_train, epochs=5, batch_size=64)
Trains the model.
GRU – Gated Recurrent Unit
1. from [Link] import GRU
Imports GRU layer, which is simpler and faster than LSTM.
2. model = Sequential()
Starts the model.
3. [Link](Embedding(input_dim=5000, output_dim=64))
Embeds the input words.
4. [Link](GRU(128))
Adds the GRU layer with 128 units.
5. [Link](Dense(1, activation='sigmoid'))
Output layer with sigmoid activation.
6. [Link](optimizer='adam',
loss='binary_crossentropy', metrics=['accuracy'])
Prepares model for training.
7. [Link](X_train, y_train, epochs=5, batch_size=64)
Trains the GRU.
_________________________________________________________________________________________________________