Laboratory Task 2 - Single Forward Pass and Error Analysis#

DS Elective 4 - Deep Learning#

Name: Keith Laspoña
Year & Section: DS4A

1. Perform standard imports#

import numpy as np

2. Define given values#

x = np.array([1, 0, 1])
y_actual = 1
W_hidden = np.array([[0.2, -0.3], [0.4, 0.1], [-0.5, 0.2]])
W_output = np.array([[-0.3], [-0.2]])
theta = np.array([-0.4, 0.2, 0.1])
learning_rate = 0.9

print(f"Input: {x}")
print(f"Actual output: {y_actual}")
Input: [1 0 1]
Actual output: 1

3. Calculate hidden layer weighted sums#

Z1 = (0.2 * 1) + (0.4 * 0) + (-0.5 * 1) + (-0.4)
Z2 = (-0.3 * 1) + (0.1 * 0) + (0.2 * 1) + (0.2)

print(f"Z1 = {Z1}")
print(f"Z2 = {Z2}")
Z1 = -0.7
Z2 = 0.10000000000000003

4. Apply sigmoid activation to hidden layer#

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

H1 = sigmoid(Z1)
H2 = sigmoid(Z2)

print(f"H1 = {H1:.6f}")
print(f"H2 = {H2:.6f}")
H1 = 0.331812
H2 = 0.524979

5. Calculate output layer weighted sum#

Z3 = (-0.3 * H1) + (-0.2 * H2) + 0.1

print(f"Z3 = {Z3:.6f}")
Z3 = -0.104540

6. Apply sigmoid activation to get final output#

y_predicted = sigmoid(Z3)
print(f"Predicted output: {y_predicted:.6f}")
Predicted output: 0.473889

7. Calculate error#

error = y_actual - y_predicted
print(f"Error = {error:.6f}")
Error = 0.526111

Conclusion#

  • Input layer: 3 neurons (x = [1, 0, 1])

  • Hidden layer: 2 neurons with sigmoid activation

  • Output layer: 1 neuron with sigmoid activation

Key Results#

  • The network predicted an output of 0.473889 when the target was 1.0

  • This resulted in an error of 0.526111, indicating the network needs training to improve its predictions

  • The sigmoid activation function successfully introduced non-linearity at each layer