This guide walks through the provided examples, showing different usage patterns and when to use each one.
If you're new to MicrogradCpp, start with easy.cpp → easy_adam.cpp → easy_df.cpp → main.cpp.
Use this if: You want the simplest possible example using the in-memory Iris dataset.
What it demonstrates:
- Loading the Iris dataset with
get_iris() - Creating an MLP model
- Training with the default SGD optimizer
Key code:
DatasetType dataset = get_iris();
shuffle(dataset);
MLP model(4, {10, 10, 3});
train_eval(dataset, 0.8, model, 0.01, 100);Architecture: 4 inputs → [10, 10] hidden layers → 3 outputs Optimizer: SGD Learning rate: 0.01 Epochs: 100
Compile:
g++ -std=c++17 -Iinclude -O2 -o easy easy.cpp && ./easy
# or
make run # (if Makefile is configured for easy.cpp)Use this if: You want to see how to use the Adam optimizer instead of SGD.
What it demonstrates:
- Same setup as easy.cpp
- Extracting model parameters with
model.parameters() - Initializing and using the
AdamOptimizer
Key code:
MLP model(4, {10, 10, 3});
auto params = model.parameters();
AdamOptimizer optimizer(params, 0.01);
train_eval(dataset, 0.8, model, optimizer, 100);Differences from easy.cpp:
- Uses Adam optimizer (often converges faster)
- Same hyperparameters for fair comparison
Compile:
g++ -std=c++17 -Iinclude -O2 -o easy_adam easy_adam.cpp && ./easy_adamUse this if: You want to load data from a CSV file and use data preprocessing features.
What it demonstrates:
- Loading CSV with
DataFrame::from_csv() - Data normalization with
df.normalize() - Categorical encoding with
df.encode_column() - Shuffling with
df.shuffle()
Key code:
DataFrame df;
df.from_csv("./data/iris.csv");
df.normalize();
df.encode_column("variety");
df.shuffle();
MLP model(4, {10, 10, 3});
train_eval(df, 0.8, model, 0.01, 100);Benefits:
- Automated data preprocessing
- Works with any CSV (not just Iris)
- Normalization improves training stability
Compile:
g++ -std=c++17 -Iinclude -O2 -o easy_df easy_df.cpp && ./easy_dfUse this if: You want a complete example with interactive configuration and Adam optimizer.
What it demonstrates:
- DataFrame preprocessing
- Adam optimizer usage
- Interactive configuration (user enters epoch count)
Key code:
DataFrame df;
df.from_csv("./data/iris.csv");
df.normalize();
df.encode_column("variety");
df.shuffle();
MLP model(4, {10, 10, 3});
auto params = model.parameters();
int epochs;
std::cout << "Enter number of epochs: ";
std::cin >> epochs;
AdamOptimizer optimizer(params, 0.01);
train_eval(df, 0.8, model, optimizer, epochs);Includes:
- Complete data preprocessing pipeline
- Adam optimizer (often better convergence)
- Interactive input for experimentation
Compile:
g++ -std=c++17 -Iinclude -O2 -o main main.cpp && ./main
# or
make run # (builds and runs main.cpp)| Example | Dataset | Optimizer | Preprocessing | Interactive |
|---|---|---|---|---|
| easy.cpp | In-memory | SGD | No | No |
| easy_adam.cpp | In-memory | Adam | No | No |
| easy_df.cpp | CSV | SGD | Yes | No |
| main.cpp | CSV | Adam | Yes | Yes |
All examples use consistent settings for fair comparison:
- Model architecture: 4 inputs → [10, 10] hidden → 3 outputs
- Learning rate: 0.01
- Epochs: 100 (configurable in main.cpp)
- Train/test split: 80/20
To experiment with different hyperparameters, modify the values in your chosen example.
After running these examples:
- Modify hyperparameters in easy.cpp and observe the effects
- Compare SGD vs Adam (easy.cpp vs easy_adam.cpp)
- Try different architectures: Change
{10, 10}to{16, 16}or{5, 5} - Load your own CSV: Replace the Iris dataset path in easy_df.cpp
- Explore the library: Check the
include/directory for more features