Group A

QA7: Write a Python program to implement artificial neural network for forward propagation and back propagation.

ANN Forward and Back Propagation

Solution and implementation for QA7 from Artificial Neural Network (ann).

A7_ann_forward_backprop.py Download
import numpy as np

def sig(x):
    return 1 / (1 + np.exp(-x))

def dsig(x):
    return x * (1 - x)

inp = np.array([[0,0],[0,1],[1,0],[1,1]])
out = np.array([[0],[1],[1],[0]])

w1 = np.random.rand(2, 2)
b1 = np.zeros((1, 2))

w2 = np.random.rand(2, 1)
b2 = np.zeros((1, 1))

lr = 0.5

for _ in range(10000):

    h_in = np.dot(inp, w1) + b1
    h_out = sig(h_in)

    o_in = np.dot(h_out, w2) + b2
    o_out = sig(o_in)

    err = out - o_out

    d_out = err * dsig(o_out)

    d_hid = d_out.dot(w2.T) * dsig(h_out)

    w2 = w2 + h_out.T.dot(d_out) * lr
    b2 = b2 + np.sum(d_out, axis=0) * lr

    w1 = w1 + inp.T.dot(d_hid) * lr
    b1 = b1 + np.sum(d_hid, axis=0) * lr

print("Output:")
print(o_out)
A7_B1_B3.py Download
# Forward and Back Propagation

import numpy as np

# Input and Output
X = np.array([
    [0, 0],
    [0, 1],
    [1, 0],
    [1, 1]
])

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

# Weights
w1 = np.random.rand(2, 2)
w2 = np.random.rand(2, 1)

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

# Derivative
def derivative(x):
    return x * (1 - x)

# Training
for i in range(5000):

    # Forward Propagation
    h_input = np.dot(X, w1)
    h_output = sigmoid(h_input)

    o_input = np.dot(h_output, w2)
    output = sigmoid(o_input)

    # Error
    error = Y - output

    # Back Propagation
    d_output = error * derivative(output)

    d_hidden = d_output.dot(w2.T) * derivative(h_output)

    # Update Weights
    w2 += h_output.T.dot(d_output)
    w1 += X.T.dot(d_hidden)

# Final Output
print("Output:")
print(np.round(output))

Other Questions in Artificial Neural Network

See All Available Questions
Download