AI Model Architecture
How AI models are built with layers
An AI model architecture describes how an AI model is built. It defines which layers the model has and how data flows through them.
Each layer:
- applies a mathematical rule
- changes the data a little You can think of layers like filters that data passes through.
Example: Neural Network Architecture
Here is an example of how developers define layers in code. This example is created with pytorch. A very common library used to define networks.
import torch
import torch.nn as nn
class NeuralNetwork(nn.Module):
def __init__(self):
super().__init__()
# Flattens a 28x28 image into a vector of length 784
self.flatten = nn.Flatten()
# Stack of linear layers with ReLU activation
self.linear_relu_stack = nn.Sequential(
nn.Linear(28 * 28, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU(),
nn.Linear(512, 10) # 10 output classes
)
def forward(self, x):
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logitsThis model has multiple layers stacked together:
- Flatten layer: Converts the image into a single list of numbers
- Linear layer: Applies mathematical transformations
- ReLU layer: Applies a simple rule (keep positive numbers, turn negative to zero)
- Final layer: Outputs 10 numbers (one for each class)
Learn how this works with classification.