Skip to content

Instantly share code, notes, and snippets.

@emilemathieu
Last active August 9, 2018 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilemathieu/97dc38a4c0f8a4d66393c19d73df24b2 to your computer and use it in GitHub Desktop.
Save emilemathieu/97dc38a4c0f8a4d66393c19d73df24b2 to your computer and use it in GitHub Desktop.
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