0% found this document useful (0 votes)
14 views

Program 4

this is the program
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Program 4

this is the program
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Importing Libraries
import
importnumpy
numpyasasnp
np

 NumPy is imported, which is a powerful library for numerical computations, especially for
working with arrays and performing matrix operations.

2. Defining the Sigmoid Activation Function and its Derivative

def sigmoid(x):

return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):

return x * (1 - x)

 Sigmoid Activation Function: This is a common activation function used in neural networks. It
maps input values to a range between 0 and 1, which helps in squashing the output to be non-linear.

 Sigmoid Derivative: The derivative of the sigmoid function is used in backpropagation to compute
how much the weights should be adjusted based on the error.

3. Defining the Input and Output Data (XOR Problem)

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])

y = np.array([[0], [1], [1], [0]]) # XOR problem output


 Input Data (X): This is a matrix representing the input data for the XOR problem. Each row is an
input pair of binary values (0 or 1).

 Output Data (y): The desired output for each corresponding input. For XOR, the output is 1 if the
inputs are different, otherwise 0.

4. Setting Up the Neural Network Parameters


np.random.seed(1)

Random Seed: This ensures the random number generator produces the same sequence every time
the program runs, which helps in reproducibility of results.

input_layer_neurons = 2 # Number of features in the input layer

hidden_layer_neurons = 2 # Number of neurons in the hidden layer

output_layer_neurons = 1 # Number of neurons in the output layer

Neurons per Layer:

 Input Layer: 2 neurons because the input consists of 2 features.

 Hidden Layer: 2 neurons for this example. This layer processes input and learns useful
features.

 Output Layer: 1 neuron because the output is a single binary value (0 or 1).

5. Initializing Weights

weights_input_hidden = np.random.uniform(size=(input_layer_neurons, hidden_layer_neurons))

weights_hidden_output = np.random.uniform(size=(hidden_layer_neurons, output_layer_neurons))

Weight Matrices:

 weights_input_hidden: A 2x2 matrix representing weights between the input and hidden
layer, initialized with random values.

 weights_hidden_output: A 2x1 matrix representing weights between the hidden and output
layer.

6. Setting Learning Rate and Epochs

learning_rate = 0.5

epochs = 10000
 Learning Rate: A hyperparameter that controls the step size during the update of weights. A value
of 0.5 is used here.

 Epochs: The number of iterations over the entire training dataset, set to 10,000.

7. Training the Network (Forward Pass + Backpropagation)

for epoch in range(epochs):

The training loop runs for 10,000 iterations.

Forward Pass

hidden_layer_input = np.dot(X, weights_input_hidden)

hidden_layer_output = sigmoid(hidden_layer_input)

output_layer_input = np.dot(hidden_layer_output, weights_hidden_output)

predicted_output = sigmoid(output_layer_input)

 Input to Hidden Layer: hidden_layer_input = np.dot(X, weights_input_hidden) computes the


input to the hidden layer using matrix multiplication (dot product) between input X and the
weights.

 Hidden Layer Output: The activation function (sigmoid) is applied to the input to get the
output of the hidden layer.

 Input to Output Layer: The same process is repeated for the output layer, where the hidden
layer’s output is multiplied by the weights between the hidden and output layers.

 Predicted Output: The sigmoid activation is applied to get the final output (between 0 and
1).

Calculating Error

error = y - predicted_output

Error: The difference between the actual output (y) and the predicted output (predicted_output).

d_predicted_output = error * sigmoid_derivative(predicted_output)

error_hidden_layer = d_predicted_output.dot(weights_hidden_output.T)

d_hidden_layer = error_hidden_layer * sigmoid_derivative(hidden_layer_output)


 Error at Output Layer: The derivative of the error with respect to the predicted output is
calculated (d_predicted_output). This will be used to adjust the weights between the hidden and
output layers.

 Error at Hidden Layer: The error is propagated backward from the output layer to the hidden layer
using the dot product of the output layer’s error with the transpose of the weights.

 Gradient at Hidden Layer: The gradient (d_hidden_layer) is calculated by multiplying the error at
the hidden layer with the derivative of the sigmoid function applied to the hidden layer's output.

Updating Weights

weights_hidden_output += hidden_layer_output.T.dot(d_predicted_output) * learning_rate

weights_input_hidden += X.T.dot(d_hidden_layer) * learning_rate

 Weight Update: The weights are updated by moving in the direction of the negative gradient
to reduce the error. The learning rate controls the size of the weight updates.

8. Monitoring the Error


if epoch % 1000 == 0:

print(f"Epoch {epoch}, Error: {np.mean(np.abs(error))}")

Every 1000 epochs, the mean absolute error is printed to monitor how well the model is learning.

9. Final Output

print("\nFinal predicted output:")

print(predicted_output)

After training is complete, the final


predicted output is printed, which should approximate the XOR truth table:

This means the network has learned to solve the XOR problem.

[[0]

[1]

[1]

[0]]

You might also like