Understanding the OwnFramework Relationship with Keras/TensorFlow in Microsoft's AI For Beginners
The OwnFramework module teaches neural network fundamentals by implementing forward and backward passes manually in NumPy, while the Keras/TensorFlow lessons demonstrate how production frameworks automate these operations through high-level APIs and automatic differentiation.
The microsoft/AI-For-Beginners curriculum introduces OwnFramework as a pedagogical tool to demystify deep learning internals before students transition to industrial-strength libraries. Understanding the OwnFramework relationship with Keras/TensorFlow helps learners grasp exactly how high-level abstractions map to underlying mathematical operations.
What is OwnFramework?
OwnFramework is a minimal neural-network library built from scratch using pure NumPy, located in lessons/3-NeuralNetworks/04-OwnFramework/OwnFramework.ipynb. Unlike production frameworks, it requires learners to manually define tensor operations, explicit weight updates, and hand-coded forward and backward passes. This low-level approach exposes the mechanics of gradient flow and backpropagation that are typically hidden behind framework abstractions.
Architectural Differences: OwnFramework vs. Keras/TensorFlow
Abstraction Level
OwnFramework operates at a low abstraction level. Learners initialize weights manually (e.g., W = np.random.randn(2,1) * 0.01), implement activation functions like sigmoid(), and write explicit parameter update rules such as W -= lr * dW. Every matrix multiplication and gradient calculation is visible in the source code.
Keras (TensorFlow) provides a high-level API that automatically builds computation graphs, manages variables, and applies optimizer updates. Users define layers declaratively, and the framework handles the underlying linear algebra and gradient tracking internally.
Gradient Computation and Automatic Differentiation
In OwnFramework, the backward() method must be implemented manually for each layer. Learners apply the chain rule explicitly to compute gradients of the loss with respect to weights, as shown in the training loop where dW = X.T @ dz / X.shape[0] calculates the weight gradients for a dense layer.
Keras leverages TensorFlow’s automatic differentiation via tf.GradientTape. When model.fit() is called, the framework records operations on trainable variables and automatically computes gradients without requiring manual derivative implementations. Optimizers in tf.keras.optimizers then apply these gradients using algorithms like SGD or Adam.
Computational Graph Model
OwnFramework uses immediate execution with no static graph; every forward call computes outputs directly through Python operators. This mirrors Python’s standard imperative programming style but lacks optimization opportunities.
TensorFlow 2.x defaults to eager execution similar to OwnFramework’s approach, but can compile static computation graphs using the @tf.function decorator. This enables graph optimizations, device placement caching, and exportable SavedModel formats for production deployment.
Device Placement and Hardware Acceleration
OwnFramework leaves hardware management entirely to the user. GPU acceleration falls outside the scope of the notebook, though NumPy operations could theoretically run on GPU through alternative libraries like CuPy.
Keras and TensorFlow provide automatic device placement. Tensors are automatically placed on available GPUs, and the framework manages data transfer between CPU and GPU memory through tf.device contexts and placement strategies.
Code Comparison: XOR Problem Implementation
Manual Implementation with OwnFramework
The following excerpt from lessons/3-NeuralNetworks/04-OwnFramework/OwnFramework.ipynb demonstrates a single-layer perceptron solving the XOR problem using only NumPy:
import numpy as np
# Dummy data
X = np.array([[0,0],[0,1],[1,0],[1,1]]) # shape (4,2)
y = np.array([[0],[1],[1],[0]]) # XOR targets
# Initialise weights
W = np.random.randn(2,1) * 0.01
b = np.zeros((1,))
# Hyper-parameters
lr = 0.1
epochs = 1000
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def sigmoid_prime(z):
s = sigmoid(z)
return s * (1 - s)
# Training loop – forward + backward
for epoch in range(epochs):
# Forward
z = X @ W + b # Linear part
a = sigmoid(z) # Activation
# Compute loss (binary cross-entropy)
loss = -(y*np.log(a) + (1-y)*np.log(1-a)).mean()
# Backward (gradient of loss w.r.t. W and b)
dz = a - y
dW = X.T @ dz / X.shape[0]
db = dz.mean(axis=0)
# Parameter update
W -= lr * dW
b -= lr * db
High-Level Implementation with Keras/TensorFlow
The equivalent implementation using TensorFlow 2.x and Keras, as shown in lessons/3-NeuralNetworks/05-Frameworks/IntroKerasTF.ipynb, abstracts away the manual gradient calculations:
import tensorflow as tf
from tensorflow import keras
# Build model
model = keras.Sequential([
keras.layers.Dense(1, activation='sigmoid', input_shape=(2,))
])
# Compile with optimizer and loss
model.compile(optimizer='sgd',
loss='binary_crossentropy',
metrics=['accuracy'])
# Train
model.fit(X, y, epochs=1000, verbose=0)
# Evaluate
loss, acc = model.evaluate(X, y, verbose=0)
print(f'Loss: {loss:.4f}, Accuracy: {acc:.2f}')
Both snippets train an identical network architecture on the XOR dataset, but the OwnFramework implementation requires explicit mathematical notation for every operation, while Keras delegates gradient computation to tf.GradientTape and parameter updates to the built-in SGD optimizer.
Pedagogical Purpose and Learning Outcomes
The curriculum deliberately juxtaposes OwnFramework with Keras to achieve specific educational objectives. By implementing the framework manually, students build intuition about how activations, loss functions, and gradients interconnect. The manual implementation reveals why weight initialization matters, how learning rates affect convergence, and how backpropagation flows through the network.
Subsequently, transitioning to Keras allows students to validate their understanding by reproducing the same model architecture in a mature library. They can compare loss curves and accuracy metrics to confirm their manual implementation matches the industrial standard, reinforcing that both approaches solve the same mathematical optimization problem.
Key Files in the Repository
The following source files define the OwnFramework relationship with Keras/TensorFlow throughout the curriculum:
lessons/3-NeuralNetworks/04-OwnFramework/OwnFramework.ipynb— Step-by-step notebook building the custom framework with manual forward and backward passes.lessons/3-NeuralNetworks/04-OwnFramework/README.md— Overview of learning objectives and lesson structure for the custom framework module.lessons/3-NeuralNetworks/04-OwnFramework/lab/README.md— Lab instructions applying OwnFramework to MNIST digit classification.lessons/3-NeuralNetworks/05-Frameworks/IntroKerasTF.ipynb— Parallel implementation using TensorFlow/Keras demonstrating high-level equivalents to the manual operations.lessons/3-NeuralNetworks/05-Frameworks/IntroPyTorch.ipynb— Additional reference showing the same concepts in PyTorch for cross-framework comparison.
Summary
- OwnFramework implements neural networks from scratch in NumPy to expose underlying mechanics of forward propagation, backpropagation, and gradient descent.
- Keras/TensorFlow abstracts these mechanics through automatic differentiation (
tf.GradientTape), high-level layer APIs, and managed optimizers. - Both frameworks solve identical mathematical problems (minimizing loss functions), but OwnFramework makes every tensor operation explicit while Keras automates them.
- The microsoft/AI-For-Beginners curriculum uses both approaches sequentially to bridge theoretical understanding with production-ready implementation skills.
Frequently Asked Questions
Does OwnFramework use TensorFlow or PyTorch internally?
No. OwnFramework is built entirely on NumPy and standard Python. The implementation intentionally avoids external deep learning frameworks to ensure learners code every mathematical operation manually, including matrix multiplications, activation functions, and gradient calculations.
Why does the curriculum teach a custom framework before introducing Keras?
Starting with OwnFramework forces students to implement forward passes, backward propagation, and parameter updates explicitly. This approach builds fundamental intuition about how loss gradients flow through network architectures before the complexity of automatic differentiation and framework internals is abstracted away by high-level APIs.
Can models trained in OwnFramework be converted to TensorFlow or Keras?
No direct conversion path exists because OwnFramework lacks a serialization format and uses NumPy arrays rather than TensorFlow tensors. However, students can manually transpose learned weights and architecture definitions into a Keras Sequential model to verify equivalent performance on identical datasets, confirming their manual implementation correctness.
How does OwnFramework handle GPU acceleration compared to TensorFlow?
OwnFramework does not implement automatic GPU management. While NumPy operations can run on GPU through alternative libraries like CuPy, the notebook focuses on CPU execution to emphasize algorithmic clarity. In contrast, TensorFlow automatically places tensors on available GPUs, manages memory allocation, and handles data transfer between CPU and GPU devices transparently.
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 →