Skip to content

Commit

Permalink
Revise package structure to ease usability
Browse files Browse the repository at this point in the history
This is a user-experience-focused package. Without a cogent API,
  it would seem hypocritical and insincere to make another crap
  tool that promises the world.
  • Loading branch information
stephenjfox committed Feb 12, 2019
1 parent 34dcc05 commit e0b12bb
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 33 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,32 @@ This API is undergoing wild changes as it approaches release.
* Almost every change will be a breaking change.
* __Do not__ rely on the functions as they currently are

Please feel free to look around, but bookmark which release tag it was. Master will be changing, viciously.
Please feel free to look around, but bookmark which release tag it was. Master will be changing, viciously.


---

# Understanding [MorphNet](https://arxiv.org/abs/1711.06798)

A Stephen Fox endeavor to become an Applied AI Scientist.

## Setup (to work alongside me)

`git clone https://github.com/stephenjfox/Morph.py.git`

## Requisites

### [Install Anaconda](https://www.anaconda.com/download/)
* They've made it easier with the years. If you haven't already, please give it a try

### Install Pip

1. `conda install pip`
2. Proceed as normal

### Dependencies

- Jupyter Notebook
* And a few tools to make it better on your local environment like `nb_conda`, `nbconvert`, and `nb_conda_kernels`
- Python 3.6+ because [Python 2 is dying](https://pythonclock.org/)
- PyTorch (`conda install torch torchvision`)
19 changes: 17 additions & 2 deletions demo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
from .my_morph import MorphNet
import torch
import torch.nn as nn
from torch.utils.data import TensorDataset, DataLoader

import morph
import morph.nn as net
from morph.layers.sparse import sparsify

from morph._models import EasyMnist

def main():
pass
my_model = EasyMnist()
# do one pass through the algorithm
modified = morph.once(my_model)

my_dataloader = DataLoader(TensorDataset(torch.randn(2, 28, 28)))

# get back the class that will do work
morphed = net.Morph(my_model, epochs=5, dataloader=my_dataloader)
morphed.run_training()


if __name__ == '__main__':
Expand Down
3 changes: 1 addition & 2 deletions morph/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from .morph import morph
from .nn.morph_net import *
from .nn.morph import once # facility tate "morph.once"
21 changes: 21 additions & 0 deletions morph/_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import torch
import torch.nn as nn
import torch.nn.functional as F

class EasyMnist(nn.Module):
def __init__(self):
super().__init__()
self.linear1 = nn.Linear(784, 1000)
self.linear2 = nn.Linear(1000, 30)
self.linear3 = nn.Linear(30, 10)

def forward(self, x_batch: torch.Tensor):
"""Simple ReLU-based activations through all layers of the DNN.
Simple and effectively deep neural network. No frills.
"""
_input = x_batch.view(-1, 784) # shape for our linear1
out1 = F.relu(self.linear1(x_batch))
out2 = F.relu(self.linear2(out1))
out3 = F.relu(self.linear3(out2))

return out3
2 changes: 1 addition & 1 deletion morph/layers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .sparsify import *
from .sparse import *
from .widen import widen
File renamed without changes.
10 changes: 0 additions & 10 deletions morph/morph.py

This file was deleted.

2 changes: 2 additions & 0 deletions morph/nn/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from ._morph_net import Morph
from .morph import once
25 changes: 25 additions & 0 deletions morph/nn/_morph_net.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from morph.layers.sparse import percent_waste
import torch.nn as nn
from torch.utils.data import DataLoader

class Morph(nn.Module):
"""An encapsulation of the benefits of MorphNet, namely:
1. automatically shrinking and widening, to produce a new architecture w.r.t. layer widths
2. Training of the network, to match (or beat) model performance
3.
"""

@classmethod
def shrink_out(cls, child_layer):
new_out = int(child_layer.out_features * percent_waste(child_layer))
return nn.Linear(child_layer.in_features, new_out)

def __init__(self, net: nn.Module, epochs: int, dataloader: DataLoader):
super().__init__()
self.layers = nn.ModuleList([
Morph.shrink_out(c) for c in net.children()
])

def run_training(self):
"""Performs the managed training for this instance"""
pass
12 changes: 12 additions & 0 deletions morph/nn/morph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import torch.nn as nn

def once(net: nn.Module, experimental_support=False) -> nn.Module:
"""Runs an experimental implementation of the MorphNet algorithm on `net`
producing a new network:
1. Shrink the layers o
Returns: either `net` if `experimental_support == False` or a MorphNet of
the supplied `net`.
"""
# TODO: run the algorithm
return net
15 changes: 0 additions & 15 deletions morph/nn/morph_net.py

This file was deleted.

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
name='morph-py', # How you named your package folder (MyLib)
packages=find_packages(), # Chose the same as "name"
# Start with a small number and increase it with every change you make
version='0.0.5-alpha',
version='0.0.6-beta',
# Chose a license from here: https://help.github.com/articles/licensing-a-repository
license='GNU',
# Give a short description about your library
Expand All @@ -15,7 +15,7 @@
# Provide either the link to your github or to your website
url='https://github.com/stephenjfox/Morph.py',
# I explain this later on
download_url='https://github.com/stephenjfox/Morph.py/archive/v0.0.5.tar.gz',
download_url='https://github.com/stephenjfox/Morph.py/archive/v0.0.6.tar.gz',
keywords=[
'machine learning', 'deep learning', 'nas', 'architecture',
'neural networks'
Expand Down

0 comments on commit e0b12bb

Please sign in to comment.