Sigmoid Neuron: Similar to perceptron but with a sigmoid activation function.
Activation Function: Sigmoid function:
Output is a continuous value between 0 and 1.
Can model more complex, non-linear functions.
Sigmoid
MSE
Gradient Descent
These are all modern drop-in replacements that improve the performance of the network.
from torchvision import datasets
from torchvision.transforms import ToTensor
train_data = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor(),
)
test_data = datasets.FashionMNIST(
root="data",
train=False,
download=True,
transform=ToTensor(),
)
from torch.utils.data import DataLoader
batch_size = 64
# Create data loaders (lists of batches)
train_dataloader = DataLoader(train_data, batch_size=batch_size)
test_dataloader = DataLoader(test_data, batch_size=batch_size)
from torch import nn
class MyNeuralNetwork(nn.Module):
def __init__(self):
super().__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(28*28, 512), # Flatten 28x28 image
nn.ReLU(), # ReLU activation function (instead of Sigmoid)
nn.Linear(512, 512), # 512 hidden units
nn.ReLU(), # ReLU activation function
nn.Linear(512, 10) # 10 output units
)
def forward(self, x):
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits
model = MyNeuralNetwork().to("cpu")
model = MyNeuralNetwork().to("cpu")
print(model)
MyNeuralNetwork(
(flatten): Flatten(start_dim=1, end_dim=-1)
(linear_relu_stack): Sequential(
(0): Linear(in_features=784, out_features=512, bias=True)
(1): ReLU()
(2): Linear(in_features=512, out_features=512, bias=True)
(3): ReLU()
(4): Linear(in_features=512, out_features=10, bias=True)
)
)
model = MyNeuralNetwork().to("cpu")
num_epochs = 10
for epoch in range(num_epochs):
# Train the Model
...
# Test the Model
...
print("Model is done!")
model.train() # Set model to training mode
for X, y in dataloader: # Iterate over the training batches
X = X.to("cpu")
y = y.to("cpu")
# Compute prediction error for the batch
pred = model(X)
loss = loss_fn(pred, y)
# Backpropagation
loss.backward()
optimizer.step()
optimizer.zero_grad()
model.eval() # Set model to evaluation mode
total_loss = 0 # Total loss for the test set
num_correct = 0 # Number of correct predictions
with torch.no_grad():
for X, y in test_dataloader: # Iterate over the test batches
X = X.to("cpu")
y = y.to("cpu")
pred = model(X) # Compute prediction
# Compute loss and accuracy for the batch
total_loss += loss_fn(pred, y).item()
num_correct += (pred.argmax(1) == y).type(torch.float).sum().item()
# Compute average loss and accuracy
avg_loss = total_loss / num_test_samples
accuracy = num_correct / num_test_samples
https://shorturl.at/SEf9G