NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

MLP (Multilayer Perceptron) for XOR gate
import numpy as np
# Activation functions
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
# def tanh(x):
# return np.tanh(x)
# def tanh_derivative(x):
# return 1 - np.tanh(x)**2
# def relu(x):
# return np.maximum(0, x)
# def relu_derivative(x):
# return np.where(x > 0, 1, 0)
# def softmax(x):
# exp_x = np.exp(x - np.max(x)) # Subtracting max for numerical
stability
# return exp_x / np.sum(exp_x, axis=1, keepdims=True)
# def softmax_derivative(x):
# s = softmax(x)
# return s * (1 - s)
# Mean Squared Error loss function
def mse_loss(y_true, y_pred):
return np.mean((y_true - y_pred) ** 2)
# Training the MLP
class MLP:
def __init__(self, input_size, hidden_size, output_size):
# Initialize weights
self.weights_input_hidden = np.random.rand(input_size,
hidden_size)
self.weights_hidden_output = np.random.rand(hidden_size,
output_size)
# Initialize biases
self.bias_hidden = np.zeros((1, hidden_size))
self.bias_output = np.zeros((1, output_size))
self.weight1_history=[]
self.weight2_history=[]
def forward(self, X):
# Forward pass
self.hidden_input = np.dot(X, self.weights_input_hidden) +
self.bias_hidden
self.hidden_output = sigmoid(self.hidden_input)
self.output_input = np.dot(self.hidden_output,
self.weights_hidden_output) + self.bias_output
output = sigmoid(self.output_input)
return output
def backward(self, X, y, output, learning_rate):
# Backward pass
output_error = y - output
output_delta = output_error * sigmoid_derivative(output)
hidden_error = output_delta.dot(self.weights_hidden_output.T)
hidden_delta = hidden_error *
sigmoid_derivative(self.hidden_output)
# Update weights and biases
self.weights_hidden_output +=
self.hidden_output.T.dot(output_delta) * learning_rate
self.bias_output += np.sum(output_delta, axis=0,
keepdims=True) * learning_rate
self.weights_input_hidden += X.T.dot(hidden_delta) *
learning_rate
self.bias_hidden += np.sum(hidden_delta, axis=0,
keepdims=True) * learning_rate
def train(self, X, y, epochs, learning_rate):
loss_history=[]
for epoch in range(epochs):
output = self.forward(X)
self.backward(X, y, output, learning_rate)
loss = mse_loss(y, output)
loss_history.append(loss)
self.weight1_history.append(self.weights_input_hidden.copy())
self.weight2_history.append(self.weights_hidden_output.copy())
# Calculate and print loss
if epoch % 100 == 0:
print(f'Epoch {epoch}, Loss: {loss}')
return loss_history
def predict(self, X):
return self.forward(X)
import matplotlib.pyplot as plt
# Data
X = np.array([[1,1], [1,0], [0,0], [0,1]])
y = np.array([[0], [1], [0], [1]]) # XOR problem
# Initialize MLP
mlp = MLP(input_size=2, hidden_size=2, output_size=1)
# Train the MLP
loss_history=mlp.train(X, y, epochs=3000, learning_rate=0.3)
plt.plot(loss_history)
plt.xlabel('epochs')
plt.ylabel('loss')
plt.show()
#weights graph
plt.plot(np.array(mlp.weight1_history)[:, 0, 0], label='w1[0,0]')
plt.plot(np.array(mlp.weight2_history)[:, 1, 0], label='w1[1,0]')
plt.xlabel('Epochs')
plt.ylabel('Weights')
plt.title('Weights from Input to Hidden Layer')
plt.legend()
plt.show()
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.