21AIL66 Program 4

4. To understand the working principle of Artificial Neural network with feed forward and feed backward principle.

import numpy as np


# Sigmoid activation function
def sigmoid(x):
    return 1 / (1 + np.exp(-x))


# Derivative of the sigmoid function
def sigmoid_derivative(x):
    return x * (1 - x)


# Initialize the neural network
def initialize_network(input_size, hidden_size, output_size):
    np.random.seed(1)
    weights_input_hidden = np.random.rand(input_size, hidden_size)
    weights_hidden_output = np.random.rand(hidden_size, output_size)
    return weights_input_hidden, weights_hidden_output


# Forward propagation
def forward_propagate(inputs, weights_input_hidden, weights_hidden_output):
    hidden_input = np.dot(inputs, weights_input_hidden)
    hidden_output = sigmoid(hidden_input)
    final_input = np.dot(hidden_output, weights_hidden_output)
    final_output = sigmoid(final_input)
    return hidden_output, final_output


# Backpropagation
def backpropagate(inputs, hidden_output, final_output, expected_output, weights_input_hidden, weights_hidden_output, learning_rate):
    output_error = expected_output - final_output
    output_delta = output_error * sigmoid_derivative(final_output)

    hidden_error = output_delta.dot(weights_hidden_output.T)
    hidden_delta = hidden_error * sigmoid_derivative(hidden_output)

    weights_hidden_output += hidden_output.T.dot(output_delta) * learning_rate
    weights_input_hidden += inputs.T.dot(hidden_delta) * learning_rate

    return weights_input_hidden, weights_hidden_output


# Training the network
def train_network(inputs, expected_output, weights_input_hidden, weights_hidden_output, learning_rate, epochs):
    for epoch in range(epochs):
        hidden_output, final_output = forward_propagate(inputs, weights_input_hidden, weights_hidden_output)
        weights_input_hidden, weights_hidden_output = backpropagate(inputs, hidden_output, final_output, expected_output, weights_input_hidden, weights_hidden_output, learning_rate)
        if epoch % 1000 == 0:
            loss = np.mean(np.square(expected_output - final_output))
            print(f"Epoch {epoch}, Loss: {loss}")
    return weights_input_hidden, weights_hidden_output


# Test the network
def test_network(inputs, weights_input_hidden, weights_hidden_output):
    _, final_output = forward_propagate(inputs, weights_input_hidden, weights_hidden_output)
    return final_output


# Sample data (XOR problem)
inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
expected_output = np.array([[0], [1], [1], [0]])

# Initialize network
input_size = 2
hidden_size = 2
output_size = 1
weights_input_hidden, weights_hidden_output = initialize_network(input_size, hidden_size, output_size)

# Train network
learning_rate = 0.1
epochs = 10000
weights_input_hidden, weights_hidden_output = train_network(inputs, expected_output, weights_input_hidden, weights_hidden_output, learning_rate, epochs)

# Test network
final_output = test_network(inputs, weights_input_hidden, weights_hidden_output)
print("Final Output:")
print(final_output)

OUTPUT:

Epoch 0, Loss: 0.25113460775471164
Epoch 1000, Loss: 0.24997062307298465
Epoch 2000, Loss: 0.24988983587678032
Epoch 3000, Loss: 0.2496648454106593
Epoch 4000, Loss: 0.24862398435696278
Epoch 5000, Loss: 0.24078796428162225
Epoch 6000, Loss: 0.21058187185381871
Epoch 7000, Loss: 0.17400399739438355
Epoch 8000, Loss: 0.14434207640133986
Epoch 9000, Loss: 0.12160411613871475
Final Output:
[[0.25837323]
 [0.69141587]
 [0.69132956]
 [0.39870372]]

Leave a Reply

Your email address will not be published. Required fields are marked *