Last active
August 9, 2018 12:04
-
-
Save emilemathieu/97dc38a4c0f8a4d66393c19d73df24b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sequential(Module): | |
""" Special instance of neural network which can be constructed as a sequence of layers | |
""" | |
def __init__(self, *modules): | |
self._modules = list(modules) | |
def forward(self, X): | |
for module in self._modules: | |
X = module.forward(X) | |
return X | |
def backward(self, output_grad): | |
for module in reversed(self._modules): | |
output_grad = module.backward(output_grad) | |
return output_grad | |
def has_parameters(self, module): | |
return True if isinstance(module, (Linear, Conv2d, BatchNorm2d)) else False | |
def step(self, optimizer): | |
for module in self._modules: | |
if self.has_parameters(module): | |
module.step(optimizer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment