Deep Learning

Deep Learning (DL)

Deep Learning (DL) is a subset of Machine Learning that utilizes artificial neural networks with multiple layers to identify complex patterns in data. Unlike traditional machine learning methods, which often depend on manually engineered features, deep learning models automatically learn hierarchical representations directly from raw data. This capability makes them particularly effective for processing unstructured data, such as images, audio, text, and video. Deep learning is widely applied in various fields, including image recognition, speech processing, natural language processing, autonomous systems, and cybersecurity, where large-scale and complex data need to be analyzed efficiently.

Process

  • Input (raw data)
  • Hidden layers (learn low-level -> high-level features automatically)
  • Output (prediction / classification)

Example (Addition)

import numpy as np # For numerical operations and generating random data
from tensorflow.keras.models import Sequential # For building a sequential neural network
from tensorflow.keras.layers import LSTM, Dense, Dropout, SpatialDropout1D, Embedding # Neural network layers
from keras.callbacks import EarlyStopping # Stop training early if model stops improving

# Generate random input data
x = np.random.randint(0, 500, size=(1000,2)) # 1000 samples, 2 features each (random integers 0-499)
y = x[:, 0] + x[:, 1] # Target is sum of two features

# Build a simple neural network
model = Sequential() # Initialize sequential model
model.add(Dense(32, input_shape=(2,), activation=’relu’)) # Hidden layer with 32 neurons, ReLU activation
model.add(Dense(1)) # Output layer with 1 neuron (predict sum)

# Compile the model
model.compile(loss=’mean_absolute_error’, optimizer=’adam’, metrics=[‘mae’]) # Use MAE loss and Adam optimizer

# Train the model
model.fit(
    x, y, # Training data and targets
    validation_split=0.2, # Use 20% of data for validation
    batch_size=32, # Batch size for training
    epochs=100, # Maximum number of epochs
    verbose=1, # Show progress
    callbacks=[EarlyStopping(monitor=’val_loss’, patience=5)] # Stop early if validation loss doesn’t improve for 5 epochs
)

# Predict on new data
print(model.predict(np.array([[0.2, 10], [50, 1]]))) # Predict sum for two new samples

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM,Dense, Dropout, SpatialDropout1D,Embedding
from keras.callbacks import EarlyStopping

x = np.random.randint(0, 500, size=(1000,2))
y = x[:, 0] + x[:, 1]

model = Sequential()
model.add(Dense(32, input_shape=(2,), activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['mae'])

# ~1000 samples, batch size 32 (hyperparameter)
# For fixed validation, use train_test_split instead of validation_split
model.fit(x, y, validation_split=0.2, batch_size=32, epochs=100, verbose=1, callbacks=[EarlyStopping(monitor='val_loss', patience=5)])

print(model.predict(np.array([[0.2, 10], [50, 1]])))

Example (Multiplication)

import numpy as np # For creating and handling arrays
from tensorflow.keras.models import Sequential # For building a sequential neural network
from tensorflow.keras.layers import LSTM, Dense, Dropout, SpatialDropout1D, Embedding # Neural network layers
from keras.callbacks import EarlyStopping # Stop training early if validation loss stops improving

# Generate random input data
x = np.random.randint(0, 10, size=(1000,2)) # 1000 samples, each with 2 features (integers 0-9)
y = x[:, 0] * x[:, 1] # Target = multiplication of the two features

# Build the neural network
model = Sequential() # Initialize sequential model
model.add(Dense(64, input_shape=(2,), activation=’relu’)) # First hidden layer with 64 neurons, ReLU activation
model.add(Dense(64, activation=’relu’)) # Second hidden layer with 64 neurons, ReLU activation
model.add(Dense(1)) # Output layer with 1 neuron (predict the product)

# Compile the model
model.compile(loss=’mean_absolute_error’, optimizer=’adam’, metrics=[‘mae’]) # MAE loss for regression, Adam optimizer

# Train the model
model.fit(
    x, y, # Training data and targets
    validation_split=0.2, # Use 20% of data for validation
    batch_size=32, # Batch size
    epochs=100, # Maximum number of epochs
    verbose=1, # Show progress bar
    callbacks=[EarlyStopping(monitor=’val_loss’, patience=5)] # Stop early if validation loss does not improve for 5 epochs
)

# Predict new data
print(model.predict(np.array([[2, 3]]))) # Predict the product of 2*3

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM,Dense, Dropout, SpatialDropout1D,Embedding
from keras.callbacks import EarlyStopping

x = np.random.randint(0, 10, size=(1000,2))
y = x[:, 0] * x[:, 1]

model = Sequential()
model.add(Dense(64, input_shape=(2,), activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['mae'])

# ~1000 samples, batch size 32 (hyperparameter)
# For fixed validation, use train_test_split instead of validation_split
model.fit(x, y, validation_split=0.2, batch_size=32, epochs=100, verbose=1, callbacks=[EarlyStopping(monitor='val_loss', patience=5)])

print(model.predict(np.array([[2, 3]])))

Predicting Suspicious Emails (phishing)

import numpy as np # Numerical operations (not heavily used here but commonly included)
from tensorflow.keras.models import Sequential # Sequential model (stack layers linearly)
from tensorflow.keras.layers import Dense # Fully connected (dense) neural network layers
from sklearn.feature_extraction.text import CountVectorizer # Converts text into numeric feature vectors (bag-of-words)
emails = [
    “Click here to reset your password”, # Likely phishing example
    “Your invoice is attached”, # Likely safe example
    “Verify your bank account immediately”, # Likely phishing example
    “Meeting tomorrow at 10am”, # Likely safe example
]
labels = [1, 0, 1, 0] # Target labels: 1 = phishing, 0 = safe
vectorizer = CountVectorizer() # Initialize text vectorizer (bag-of-words model)
features = vectorizer.fit_transform(emails).toarray() # Learn vocabulary + convert emails into numeric feature matrix
model = Sequential() # Create a sequential neural network model
model.add(Dense(32, input_shape=(features.shape[1],), activation=’relu’)) # Input layer + first hidden layer (32 neurons)
model.add(Dense(16, activation=’relu’)) # Second hidden layer (16 neurons)
model.add(Dense(1, activation=’sigmoid’)) # Output layer (1 neuron for binary classification, sigmoid = probability)
model.compile(optimizer=’adam’, loss=’binary_crossentropy’, metrics=[‘accuracy’]) # Configure model training settings
model.fit(features, labels, epochs=50, verbose=0) # Train the model for 50 iterations (epochs), no training output shown
new_emails = vectorizer.transform([
    “Your account will be locked, click here”, # Suspicious/phishing-like message
    “Lunch tomorrow?” # Normal/safe message
]).toarray() # Convert new emails into the same feature format
prediction = model.predict(new_emails) > 0.5 # Predict probabilities and convert to True/False using threshold 0.5
print(“Phishing predictions (True=Phishing, False=Safe):”, prediction) # Display prediction results

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.feature_extraction.text import CountVectorizer

emails = [
    "Click here to reset your password",
    "Your invoice is attached",
    "Verify your bank account immediately",
    "Meeting tomorrow at 10am",
]

labels = [1, 0, 1, 0]  # 1 = phishing, 0 = safe

vectorizer = CountVectorizer()
features = vectorizer.fit_transform(emails).toarray()

model = Sequential()
model.add(Dense(32, input_shape=(features.shape[1],), activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))  
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(features, labels, epochs=50, verbose=0)

new_emails = vectorizer.transform([
    "Your account will be locked, click here",
    "Lunch tomorrow?"
]).toarray()

prediction = model.predict(new_emails) > 0.5
print("Phishing predictions (True=Phishing, False=Safe):", prediction)

Predicting Suspicious Files (Malware)

import numpy as np # Library for numerical operations and arrays
from tensorflow.keras.models import Sequential # Sequential model to stack layers
from tensorflow.keras.layers import Dense # Fully connected neural network layers
x = np.random.randint(0, 100, size=(1000, 3)) # Generate 1000 samples, each with 3 random features (0–99)
y = (x[:,0] + x[:,1] + x[:,2] > 150).astype(int) # Label: 1 (malware) if sum > 150, else 0 (safe)
model = Sequential() # Initialize the neural network model
model.add(Dense(32, input_shape=(3,), activation=’relu’)) # Input layer + first hidden layer (32 neurons, ReLU activation)
model.add(Dense(16, activation=’relu’)) # Second hidden layer (16 neurons)
model.add(Dense(1, activation=’sigmoid’)) # Output layer (1 neuron, sigmoid for binary classification)
model.compile(optimizer=’adam’, loss=’binary_crossentropy’, metrics=[‘accuracy’]) # Configure model with optimizer, loss, and accuracy metric
model.fit(x, y, epochs=50, batch_size=32, verbose=0)  # Train the model for 50 epochs with batch size of 32
new_files = np.array([[60, 50, 50], [10, 5, 15]]) # New data samples to classify (each has 3 features)
prediction = model.predict(new_files) > 0.5 # Predict probabilities and convert to True/False using threshold 0.5
print(“Malware predictions (True=Malware, False=Safe):”, prediction) # Print classification results

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

x = np.random.randint(0, 100, size=(1000, 3))
y = (x[:,0] + x[:,1] + x[:,2] > 150).astype(int)  # 1 = malware, 0 = safe

model = Sequential()
model.add(Dense(32, input_shape=(3,), activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(x, y, epochs=50, batch_size=32, verbose=0)

new_files = np.array([[60, 50, 50], [10, 5, 15]])
prediction = model.predict(new_files) > 0.5
print("Malware predictions (True=Malware, False=Safe):", prediction)