How to Swap Between Different ML Frameworks in ML-For-Beginners Lessons
You swap between ML frameworks in the ML-For-Beginners curriculum by opening the framework-specific notebook that corresponds to your desired library, as each lesson contains separate notebooks for TensorFlow, PyTorch, scikit-learn, and others that share identical data loading and evaluation logic.
The Microsoft ML-For-Beginners repository provides a modular curriculum where every lesson is implemented across multiple machine-learning frameworks. This architecture allows you to swap between different ML frameworks in lessons simply by selecting the notebook that targets your preferred library, ensuring the educational narrative remains consistent while the API implementation changes.
Repository Architecture for Multi-Framework Lessons
The repository organizes content into self-contained Jupyter notebooks that follow a strict naming convention. Each lesson resides in a dedicated directory containing framework-specific variants of the same instructional content.
According to the source code structure, the key components include:
- Framework-Specific Notebooks: Separate files implementing identical algorithms with different libraries. All notebooks in a lesson share the same base filename, differing only by the framework suffix (e.g.,
03-Regression-TensorFlow.ipynb,03-Regression-PyTorch.ipynb,03-Regression-ScikitLearn.ipynb). - Shared Utilities: The
utils/directory contains framework-agnostic helper functions for data loading, preprocessing, and visualization that every notebook imports. - Requirements Files:
requirements.txtlists common dependencies, while framework-specific packages (liketensorflowortorch) are installed separately as needed.
This design ensures that when you swap frameworks, only the import statements and API calls change; the surrounding lesson narrative, data paths, and evaluation metrics remain identical.
Steps to Swap Between Frameworks in a Lesson
Follow this workflow to switch from one machine-learning library to another within the same lesson:
-
Locate the Lesson Index – Open the top-level
README.mdto find the lesson you want to study. The index lists every framework available for that specific topic. -
Select the Framework-Specific Notebook – Navigate to the lesson folder and open the notebook whose filename ends with your desired framework:
- TensorFlow ⇒
…-TensorFlow.ipynb - PyTorch ⇒
…-PyTorch.ipynb - scikit-learn ⇒
…-ScikitLearn.ipynb
- TensorFlow ⇒
-
Install Dependencies – Set up the environment using the provided
requirements.txtand add the framework-specific package. For example:pip install -r requirements.txt pip install tensorflow==2.14 # or pip install torch torchvision -
Execute the Notebook – Run all cells. Because data loading and evaluation logic are abstracted into the
utilsmodule, you will see identical results across frameworks, allowing you to compare API ergonomics and performance directly. -
Optional: Convert Models – If you need to export a model to a different format (e.g., TensorFlow to ONNX), look for notebooks with "Export" or "Convert" in the title, such as
01-Introduction/03-Deep Learning/03-2-Deep Learning - ONNX Export.ipynb.
Framework-Specific Implementation Examples
The following snippets demonstrate how the same linear regression task is implemented in three different frameworks. These examples are taken from the 02-Regression/03-Regression/ directory.
TensorFlow Implementation
In 02-Regression/03-Regression/03-Regression-TensorFlow.ipynb, the model is built using tf.keras.Sequential:
import tensorflow as tf
from utils import load_data, plot_results
X_train, y_train, X_test, y_test = load_data()
model = tf.keras.Sequential([
tf.keras.layers.Dense(1, input_shape=(X_train.shape[1],))
])
model.compile(optimizer='adam', loss='mse')
model.fit(X_train, y_train, epochs=100, verbose=0)
preds = model.predict(X_test)
plot_results(y_test, preds, title="TensorFlow Linear Regression")
PyTorch Implementation
The PyTorch variant in 02-Regression/03-Regression/03-Regression-PyTorch.ipynb uses nn.Linear and explicit training loops:
import torch
import torch.nn as nn
from utils import load_data, plot_results
X_train, y_train, X_test, y_test = load_data()
X_train = torch.from_numpy(X_train).float()
y_train = torch.from_numpy(y_train).float()
model = nn.Linear(X_train.shape[1], 1)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
for epoch in range(100):
optimizer.zero_grad()
outputs = model(X_train)
loss = criterion(outputs.squeeze(), y_train)
loss.backward()
optimizer.step()
preds = model(torch.from_numpy(X_test).float()).detach().numpy()
plot_results(y_test, preds, title="PyTorch Linear Regression")
scikit-learn Implementation
The scikit-learn version in 02-Regression/03-Regression/03-Regression-ScikitLearn.ipynb provides the most concise implementation:
from sklearn.linear_model import LinearRegression
from utils import load_data, plot_results
X_train, y_train, X_test, y_test = load_data()
model = LinearRegression()
model.fit(X_train, y_train)
preds = model.predict(X_test)
plot_results(y_test, preds, title="scikit-learn Linear Regression")
Key Files for Framework Management
| File | Description | Path |
|---|---|---|
README.md |
Master index listing all lessons and supported frameworks. | README.md |
utils/ |
Shared helper functions for data loading and visualization. | utils/ |
requirements.txt |
Common Python dependencies for all lessons. | requirements.txt |
03-2-Deep Learning - TensorFlow.ipynb |
Example TensorFlow deep learning notebook. | 01-Introduction/03-Deep Learning/03-2-Deep Learning - TensorFlow.ipynb |
03-2-Deep Learning - PyTorch.ipynb |
Equivalent PyTorch implementation. | 01-Introduction/03-Deep Learning/03-2-Deep Learning - PyTorch.ipynb |
Summary
- The ML-For-Beginners curriculum uses framework-specific notebooks that share a common naming convention, allowing you to swap between TensorFlow, PyTorch, and scikit-learn by simply opening the corresponding file.
- All notebooks import shared utility functions from the
utils/directory, ensuring that data loading, preprocessing, and evaluation remain identical across frameworks. - To switch frameworks, install the specific requirements (e.g.,
pip install tensorflow), open the appropriately suffixed notebook (e.g.,…-TensorFlow.ipynb), and execute the cells to compare API implementations side-by-side.
Frequently Asked Questions
How do I know which frameworks are supported in a specific lesson?
Check the top-level README.md or the lesson folder's index. Each lesson entry lists all available framework implementations, and the notebook files follow a strict naming pattern where the framework name appears as a suffix (e.g., -TensorFlow.ipynb, -PyTorch.ipynb, -ScikitLearn.ipynb).
Can I mix frameworks within the same lesson?
While each notebook is self-contained and uses only one framework, you can run multiple notebooks from the same lesson sequentially. Because all variants import the same load_data and plot_results functions from utils/, you can compare outputs across TensorFlow, PyTorch, and scikit-learn without data inconsistencies.
Do I need to reinstall dependencies every time I switch frameworks?
You only need to install a framework's specific packages once per environment. The base requirements.txt covers common dependencies like numpy and pandas, while framework-specific packages (e.g., tensorflow, torch) are installed separately. Once installed, you can switch between notebooks freely without reinstallation.
How do I convert a trained model from one framework to another?
The repository includes advanced lessons demonstrating cross-framework conversion. Look for notebooks with "Export" or "Convert" in the title, such as 01-Introduction/03-Deep Learning/03-2-Deep Learning - ONNX Export.ipynb, which shows how to export a TensorFlow model to ONNX format for interoperability with other frameworks.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →