NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import pandas as pd
import matplotlib.pyplot as plt

# Define the MLP model
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.fc1 = nn.Linear(2, 8) # First hidden layer
self.fc2 = nn.Linear(8, 8) # Second hidden layer
self.fc3 = nn.Linear(8, 1) # Output layer

def forward(self, x):
x = torch.relu(self.fc1(x)) # ReLU activation
x = torch.relu(self.fc2(x)) # ReLU activation
x = torch.sigmoid(self.fc3(x)) # Sigmoid activation
return x

# Initialize the model, loss function, and optimizer
model = MLP()
criterion = nn.BCELoss() # Binary Cross-Entropy Loss
optimizer = optim.Adam(model.parameters(), lr=0.01)

# Define the XOR gate input and output
X_train = torch.tensor([[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]])
y_train = torch.tensor([[0.0], [1.0], [1.0], [0.0]])

# Create a DataLoader
dataset = TensorDataset(X_train, y_train)
dataloader = DataLoader(dataset, batch_size=4, shuffle=True)

# Train the model
num_epochs = 1000
for epoch in range(num_epochs):
for batch_X, batch_y in dataloader:
optimizer.zero_grad()
outputs = model(batch_X)
loss = criterion(outputs, batch_y)
loss.backward()
optimizer.step()

if (epoch+1) % 100 == 0:
print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item()}')

# Evaluate the model
with torch.no_grad():
model.eval()
outputs = model(X_train)
binary_predictions = (outputs > 0.5).float().numpy().flatten() # Apply threshold for binary output
y_train = y_train.numpy().flatten()

# Create a DataFrame to display results
results_df = pd.DataFrame({
'Input': [f'{x[0]}, {x[1]}' for x in X_train.numpy()],
'Actual Output': y_train,
'Predicted Output': binary_predictions
})

print(results_df)

# Plotting the results
plt.figure(figsize=(10, 6))
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, s=100, edgecolor='k', label='Actual Output', cmap='viridis')
plt.scatter(X_train[:, 0], X_train[:, 1], c=binary_predictions, marker='x', s=100, label='Predicted Output', cmap='coolwarm')
plt.xlabel('Input Feature 1')
plt.ylabel('Input Feature 2')
plt.title('XOR Gate: Actual vs. Predicted Outputs')
plt.legend()
plt.grid(True)
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.