You are on page 1of 6

DECENTRALIZED LEARNING

(ASSIGNMENT # 04 .... SEMESTER FALL- 2023)


Submission Date (23 December, 2023)
Submitted By:
Mazhar Abbas
20021519-054
Submitted To:
Dr. Zahid Iqbal
Course Code:
CS-411
Degree Program Title and Section:
BS-VII Computer Science (A)

Department of Computer Science

1|Page
Question # 01: Study the different model evaluation metrics including
1. Accuracy

2. Precision

3. Recall

4. F1 score

and implement the concepts in Assignment 3 to measure the model performance.

Accuracy
Formula: (TP + TN) / (TP + TN + FP + FN)

Description: Accuracy represents the overall correctness of the model. It is the ratio of correctly
predicted instances (true positives and true negatives) to the total number of instances.

When to Use: Accuracy is suitable for balanced datasets where the classes are relatively evenly
distributed.

Scenario: Predicting whether a coin is heads or tails based on its features.

Precision
Formula: TP / (TP + FP)

Description: Precision is the ratio of correctly predicted positive observations to the total
predicted positives. It focuses on the accuracy of positive predictions.

When to Use: Precision is important when the cost of false positives is high. For example, in
applications like spam detection, precision focuses on minimizing the number of legitimate
instances incorrectly classified as positive (false positives).

Scenario: Email spam detection.

2|Page
Recall
Formula: TP / (TP + FN)

Description: Recall is the ratio of correctly predicted positive observations to the all
observations in actual class. It is a measure of the ability of a classification model to capture all
relevant instances.

When to Use: Recall is crucial when the cost of false negatives is high. For instance, in medical
diagnosis, recall is important to ensure that the model captures as many positive cases as
possible, even if it means tolerating some false positives.

Scenario: Medical diagnosis for a rare disease.

F1 Score
Formula: 2 * (Precision * Recall) / (Precision + Recall)

Description: The F1 score is the harmonic mean of precision and recall. It provides a balance
between precision and recall. F1 score reaches its best value at 1 and worst at 0.

When to Use: The F1 score is appropriate when there is an imbalance between precision and
recall, and you want to find a balance between the two. It is particularly useful when both false
positives and false negatives need to be minimized.

Scenario: Fraud detection in credit card transactions.

3|Page
Implement the Concepts in Assignment # 03
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras import backend as K

# Set the learning rate


learning_rate = 0.01

# Input values
input_data = np.array([[5, 3]])

# Target value
target = np.array([[0]])

# Define the model


model = Sequential()

# Add the hidden layer with 4 neurons


model.add(Dense(4, input_dim=2, activation='sigmoid', use_bias=True,
bias_initializer='ones'))

# Add the output layer with 1 neuron


model.add(Dense(1, activation='sigmoid', use_bias=True,
bias_initializer='ones'))

# Set the initial weights


hidden_layer_weights = np.array([[0.4, 0.1], [0.2, 0.6], [0.3, 0.5], [0.7,
0.8]])
hidden_layer_biases = np.array([1, 1, 1, 1])

output_layer_weights = np.array([[0.9], [0.5], [0.3], [0.4]])


output_layer_biases = np.array([1])

model.layers[0].set_weights([hidden_layer_weights.T, hidden_layer_biases])
# Transpose the output layer weights to match the correct shape (4, 1)
output_layer_weights_corrected = output_layer_weights.reshape((4, 1))
model.layers[1].set_weights([output_layer_weights_corrected,
output_layer_biases])
4|Page
# Forward propagation
output = model.predict(input_data)
print("Initial Output:", output)

# Calculate loss using mean square error


loss = 0.5 * np.mean((output - target) ** 2)

# Metrics Calculation
accuracy = np.mean(np.round(output) == target)
precision = np.sum(np.round(output) * target) / (np.sum(np.round(output))
+ 1e-10)
recall = np.sum(np.round(output) * target) / (np.sum(target) + 1e-10)
f1_score = 2 * (precision * recall) / (precision + recall + 1e-10)

print("Initial Loss:", loss)


print("Initial Accuracy:", accuracy)
print("Initial Precision:", precision)
print("Initial Recall:", recall)
print("Initial F1 Score:", f1_score)

# Backward propagation
delta_output = (output - target) * output * (1 - output)
# Transpose the hidden layer output to match the correct shape
transpose_hidden_output = K.transpose(model.layers[0].output)
model.layers[1].set_weights([output_layer_weights_corrected -
learning_rate * np.dot(transpose_hidden_output, delta_output),
output_layer_biases - learning_rate *
np.sum(delta_output, axis=0)])

delta_hidden = np.dot(delta_output, model.layers[1].get_weights()[0].T) *


model.layers[0].output * (1 - model.layers[0].output)
model.layers[0].set_weights([hidden_layer_weights.T - learning_rate *
np.dot(input_data.T, delta_hidden),
hidden_layer_biases - learning_rate *
np.sum(delta_hidden, axis=0)])

# Display updated weights and biases


for layer in model.layers:
print(layer.get_weights())

5|Page
# Forward propagation after backpropagation
updated_output = model.predict(input_data)

# Metrics Calculation after Backpropagation


accuracy = np.mean(np.round(updated_output) == target)
precision = np.sum(np.round(updated_output) * target) /
(np.sum(np.round(updated_output)) + 1e-10)
recall = np.sum(np.round(updated_output) * target) / (np.sum(target) + 1e-
10)
f1_score = 2 * (precision * recall) / (precision + recall + 1e-10)

print("Final Output:", updated_output)


print("Final Accuracy:", accuracy)
print("Final Precision:", precision)
print("Final Recall:", recall)
print("Final F1 Score:", f1_score)

OUTPUT
1/1 [==============================] - 0s 52ms/step
Initial Output: [[0.9548363]]
Initial Loss: 0.4558561884504986
Initial Accuracy: 0.0
Initial Precision: 0.0
Initial Recall: 0.0
Initial F1 Score: 0.0
---------------------------------------------------------------------------

6|Page

You might also like