Module¤
Base class for all neural network layers.
Module¤
from minitorch import Module, Linear, ReLU
class MyModel(Module):
def __init__(self):
super().__init__()
self.fc1 = Linear(784, 128)
self.relu = ReLU()
self.fc2 = Linear(128, 10)
def forward(self, x):
return self.fc2(self.relu(self.fc1(x)))
Methods¤
forward(*args)- override in subclassparameters()- auto-collects all trainable Tensorstrain()/eval()- switch training/eval modestate_dict()- returns dict of parameter names to numpy arraysload_state_dict(state)- loads parameters from dict
Save and Load¤
import numpy as np
# save
state = model.state_dict()
np.savez("model.npz", **state)
# load
data = np.load("model.npz")
model.load_state_dict(dict(data))
Sequential(*layers)¤
Container that chains layers in order: