Neural Network Code
Neural Network Code
# Parameters
num_i_units = 2 # Number of Input units
num_h_units = 2 # Number of Hidden units
num_o_units = 1 # Number of Output units
learning_rate = 0.01 # Learning rate
reg_param = 0 # Regularization parameter
max_iter = 5000 # Maximum iterations
m = len(X) # Number of training examples
# Activation function
def sigmoid(z, derv=False):
if derv:
return sigmoid(z) * (1 - sigmoid(z))
return 1 / (1 + np.exp(-z))
# Forward propagation
def forward(x, predict=False):
a1 = x.reshape(x.shape[0], 1) # Convert input to column vector
z2 = W1.dot(a1) + B1 # 2x1
a2 = sigmoid(z2) # Hidden layer activation
z3 = W2.dot(a2) + B2 # 1x1
a3 = sigmoid(z3) # Output layer activation
if predict:
return a3
return a1, a2, a3
# Training function
def train(W1, W2, B1, B2):
cost_history = np.zeros((max_iter, 1)) # To store cost at each iteration
for i in range(max_iter):
c=0
dW1 = np.zeros_like(W1)
dW2 = np.zeros_like(W2)
dB1 = np.zeros_like(B1)
dB2 = np.zeros_like(B2)
for j in range(m):
# Forward propagation
a0 = X[j].reshape(X[j].shape[0], 1) # 2x1
z1 = W1.dot(a0) + B1 # 2x1
a1 = sigmoid(z1) # 2x1
z2 = W2.dot(a1) + B2 # 1x1
a2 = sigmoid(z2) # 1x1
# Backpropagation
dz2 = a2 - y[j] # 1x1
dW2 += dz2 * a1.T # 1x2
dB2 += dz2 # 1x1
dz1 = np.multiply(W2.T.dot(dz2), sigmoid(z1, derv=True)) # 2x1
dW1 += dz1.dot(a0.T) # 2x2
dB1 += dz1 # 2x1
# Compute cost
c += -y[j] * np.log(a2) - (1 - y[j]) * np.log(1 - a2)
# Parameters
num_i_units = 2 # Number of Input units
num_h_units = 2 # Number of Hidden units
num_o_units = 1 # Number of Output units
learning_rate = 0.01 # Learning rate
reg_param = 0 # Regularization parameter
max_iter = 5000 # Maximum iterations
m = len(X) # Number of training examples
# Activation function
def sigmoid(z, derv=False):
if derv:
return sigmoid(z) * (1 - sigmoid(z))
return 1 / (1 + np.exp(-z))
# Forward propagation
def forward(x, predict=False):
a1 = x.reshape(x.shape[0], 1) # Convert input to column vector
z2 = W1.dot(a1) + B1 # 2x1
a2 = sigmoid(z2) # Hidden layer activation
z3 = W2.dot(a2) + B2 # 1x1
a3 = sigmoid(z3) # Output layer activation
if predict:
return a3
return a1, a2, a3
# Training function
def train(W1, W2, B1, B2):
cost_history = np.zeros((max_iter, 1)) # To store cost at each iteration
for i in range(max_iter):
c=0
dW1 = np.zeros_like(W1)
dW2 = np.zeros_like(W2)
dB1 = np.zeros_like(B1)
dB2 = np.zeros_like(B2)
for j in range(m):
# Forward propagation
a0 = X[j].reshape(X[j].shape[0], 1) # 2x1
z1 = W1.dot(a0) + B1 # 2x1
a1 = sigmoid(z1) # 2x1
z2 = W2.dot(a1) + B2 # 1x1
a2 = sigmoid(z2) # 1x1
# Backpropagation
dz2 = a2 - y[j] # 1x1
dW2 += dz2 * a1.T # 1x2
dB2 += dz2 # 1x1
dz1 = np.multiply(W2.T.dot(dz2), sigmoid(z1, derv=True)) # 2x1
dW1 += dz1.dot(a0.T) # 2x2
dB1 += dz1 # 2x1
# Compute cost
c += -y[j] * np.log(a2) - (1 - y[j]) * np.log(1 - a2)