Skip to content

Commit 1ba5dec

Browse files
committed
[EHN] Complete the package version
1 parent 0ba153f commit 1ba5dec

15 files changed

Lines changed: 131 additions & 58 deletions

‎.gitignore‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
*.pyc
2-
*.swp
2+
*.swp
3+
4+
build
5+
dist/
6+
Ensemble_PyTorch.egg-info

‎README.md‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,25 @@ Implementation of scikit-learn like ensemble methods in Pytorch.
77
* **BaggingClassifier** / **BaggingRegressor**
88
* **GradientBoostingClassifier** / **GradientBoostingRegressor**
99

10+
## Installation
11+
12+
Installing Ensemble-Pytorch package is simple. Just clone this repo and run the setup.
13+
14+
```
15+
$ git clone https://github.com/AaronX121/Ensemble-Pytorch.git
16+
$ cd Ensemble-Pytorch
17+
$ python setup.py install
18+
```
19+
1020
## Minimal example on how to use
1121
```python
1222
"""
13-
- Please see scripts in `script` for details on how to use
14-
- Please see implementations in `ensemble` for details on ensemble methods
23+
- Please see scripts in `examples` for details on how to use
24+
- Please see implementations in `torchensemble` for details on ensemble methods
1525
"""
1626

1727
import base_estimator # import base estimator
18-
from ensemble.method_module import ensemble_method # import ensemble method
28+
from torchensemble.method import ensemble_method # import ensemble method
1929

2030
model = ensemble_method(estimator=base_estimator, # type of base estimator
2131
n_estimators=10, # number of base estimators
@@ -35,13 +45,13 @@ model.fit(train_loader)
3545
model.predict(test_loader)
3646
```
3747

38-
## Experiment results
48+
## Benchmarks
3949

4050
* **Classification on CIFAR-10**
4151
* The table below presents the classification accuracy of different ensemble classifiers on the testing data of **CIFAR-10**
4252
* Each classifier uses **10** LeNet-5 model (with RELU activation and Dropout) as the base estimators
4353
* Each base estimator is trained over **100** epochs, with batch size **128**, learning rate **1e-3**, and weight decay **5e-4**
44-
* Experiment results can be reproduced by running `./script/classification_cifar10_cnn.py`
54+
* Experiment results can be reproduced by running `./examples/classification_cifar10_cnn.py`
4555

4656
| Model Name | Params (MB) | Testing Acc (%) | Improvement (%) |
4757
| ------ | ------ | ------ | ------ |
@@ -55,7 +65,7 @@ model.predict(test_loader)
5565
* The table below presents the mean squared error (MSE) of different ensemble regressors on the testing data of **YearPredictionMSD**
5666
* Each regressor uses **10** multi-layered perceptron (MLP) model (with RELU activation and Dropout) as the base estimators, and the network architecture is fixed as `Input-128-128-Output`
5767
* Each base estimator is trained over **50** epochs, with batch size **256**, learning rate **1e-3**, and weight decay **5e-4**
58-
* Experiment results can be reproduced by running `./script/regression_YearPredictionMSD_mlp.py`
68+
* Experiment results can be reproduced by running `./examples/regression_YearPredictionMSD_mlp.py`
5969

6070
| Model Name | Params (MB) | Testing MSE | Improvement |
6171
| ------ | ------ | ------ | ------ |
@@ -66,10 +76,7 @@ model.predict(test_loader)
6676
| **GradientBoostingRegressor** | 1.08 | 0.71 | - 0.12 |
6777

6878
## Package dependencies
79+
* joblib
6980
* pytorch
70-
* scikit-learn
7181
* torchvision
72-
73-
## Reference
74-
* Zhou, Zhi-Hua. "Ensemble methods: foundations and algorithms." CRC press (2012).
75-
* Friedman, Jerome H. "Greedy function approximation: a gradient boosting machine." Annals of statistics (2001).
82+
* scikit-learn
Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
""" Example on classification using CIFAR-10. """
2+
13
import sys
24
sys.path.append('../')
35

@@ -6,11 +8,11 @@
68
from torch.utils.data import DataLoader
79
from torchvision import datasets, transforms
810

9-
from model.lenet5 import LeNet5
10-
from ensemble.fusion import FusionClassifier
11-
from ensemble.voting import VotingClassifier
12-
from ensemble.bagging import BaggingClassifier
13-
from ensemble.gradient_boosting import GradientBoostingClassifier
11+
from lenet5 import LeNet5
12+
from torchensemble.fusion import FusionClassifier
13+
from torchensemble.voting import VotingClassifier
14+
from torchensemble.bagging import BaggingClassifier
15+
from torchensemble.gradient_boosting import GradientBoostingClassifier
1416

1517

1618
def display_records(records):
@@ -79,11 +81,12 @@ def display_records(records):
7981

8082
# VotingClassifier
8183
model = VotingClassifier(estimator=LeNet5,
82-
n_estimators=n_estimators,
83-
output_dim=output_dim,
84-
lr=lr,
85-
weight_decay=weight_decay,
86-
epochs=epochs)
84+
n_estimators=n_estimators,
85+
output_dim=output_dim,
86+
lr=lr,
87+
weight_decay=weight_decay,
88+
epochs=epochs,
89+
n_jobs=1)
8790

8891
tic = time.time()
8992
model.fit(train_loader)
@@ -125,11 +128,11 @@ def display_records(records):
125128

126129
# GradientBoostingClassifier
127130
model = GradientBoostingClassifier(estimator=LeNet5,
128-
n_estimators=n_estimators,
129-
output_dim=output_dim,
130-
lr=lr,
131-
weight_decay=weight_decay,
132-
epochs=epochs)
131+
n_estimators=n_estimators,
132+
output_dim=output_dim,
133+
lr=lr,
134+
weight_decay=weight_decay,
135+
epochs=epochs)
133136

134137
tic = time.time()
135138
model.fit(train_loader)
@@ -146,4 +149,5 @@ def display_records(records):
146149
evaluating_time,
147150
testing_acc))
148151

152+
# Print results on different ensemble methods
149153
display_records(records)

model/lenet5.py renamed to examples/lenet5.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
class LeNet5(nn.Module):
66

7-
def __init__(self, output_dim=10):
7+
def __init__(self):
88
super(LeNet5, self).__init__()
99

1010
self.conv1 = nn.Conv2d(3, 6, 5, padding=2)
1111
self.conv2 = nn.Conv2d(6, 16, 5)
1212
self.fc1 = nn.Linear(576, 120)
1313
self.fc2 = nn.Linear(120, 84)
14-
self.fc3 = nn.Linear(84, output_dim)
14+
self.fc3 = nn.Linear(84, 10)
1515

1616
def forward(self, X):
1717

model/MLP.py renamed to examples/mlp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
class MLP(nn.Module):
66

7-
def __init__(self, output_dim=1):
7+
def __init__(self):
88
super(MLP, self).__init__()
99

1010
self.linear1 = nn.Linear(90, 128)
1111
self.linear2 = nn.Linear(128, 128)
12-
self.linear3 = nn.Linear(128, output_dim)
12+
self.linear3 = nn.Linear(128, 1)
1313

1414
def forward(self, X):
1515
X = X.view(X.size()[0], -1)

script/regression_YearPredictionMSD_mlp.py renamed to examples/regression_YearPredictionMSD_mlp.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
1+
""" Example on univariate regression using YearPredictionMSD. """
2+
13
import sys
24
sys.path.append('../')
35

46
import time
57
import torch
8+
import numbers
69
from sklearn.preprocessing import scale
710
from sklearn.datasets import load_svmlight_file
811
from torch.utils.data import TensorDataset, DataLoader
912

10-
from model.mlp import MLP
11-
from ensemble.fusion import FusionRegressor
12-
from ensemble.voting import VotingRegressor
13-
from ensemble.bagging import BaggingRegressor
14-
from ensemble.gradient_boosting import GradientBoostingRegressor
13+
from mlp import MLP
14+
from torchensemble.fusion import FusionRegressor
15+
from torchensemble.voting import VotingRegressor
16+
from torchensemble.bagging import BaggingRegressor
17+
from torchensemble.gradient_boosting import GradientBoostingRegressor
1518

1619

1720
def load_data(batch_size):
18-
21+
1922
# The dataset can be downloaded from:
2023
# https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/regression.html#YearPredictionMSD
2124

22-
train = load_svmlight_file('../../Dataset/LIBSVM/YearPredictionMSD.bz2')
23-
test = load_svmlight_file('../../Dataset/LIBSVM/YearPredictionMSD.t.bz2')
25+
if not isinstance(batch_size, numbers.Integral):
26+
msg = '`batch_size` should be an integer, but got {} instead.'
27+
raise ValueError(msg.format(batch_size))
28+
29+
train_path = '../../Dataset/LIBSVM/YearPredictionMSD.bz2'
30+
test_path = '../../Dataset/LIBSVM/YearPredictionMSD.t.bz2'
31+
32+
train = load_svmlight_file(train_path)
33+
test = load_svmlight_file(test_path)
2434

2535
X_train, X_test = (torch.FloatTensor(train[0].toarray()),
2636
torch.FloatTensor(test[0].toarray()))
37+
2738
y_train, y_test = (torch.FloatTensor(scale(train[1]).reshape(-1, 1)),
2839
torch.FloatTensor(scale(test[1]).reshape(-1, 1)))
2940

@@ -111,11 +122,11 @@ def display_records(records):
111122

112123
# BaggingRegressor
113124
model = BaggingRegressor(estimator=MLP,
114-
n_estimators=n_estimators,
115-
output_dim=output_dim,
116-
lr=lr,
117-
weight_decay=weight_decay,
118-
epochs=epochs)
125+
n_estimators=n_estimators,
126+
output_dim=output_dim,
127+
lr=lr,
128+
weight_decay=weight_decay,
129+
epochs=epochs)
119130

120131
tic = time.time()
121132
model.fit(train_loader)
@@ -155,4 +166,5 @@ def display_records(records):
155166
evaluating_time,
156167
testing_mse))
157168

169+
# Print results on different ensemble methods
158170
display_records(records)

‎model/__init__.py‎

Whitespace-only changes.

‎requirements.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
joblib
2+
torch
3+
torchvision
4+
scikit-learn

‎setup.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from setuptools import setup, find_packages
2+
from codecs import open
3+
from os import path
4+
5+
here = path.abspath(path.dirname(__file__))
6+
7+
# Get the long description from README.md
8+
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
9+
long_description = f.read()
10+
11+
# get the dependencies and installs
12+
with open(path.join(here, 'requirements.txt'), encoding='utf-8') as f:
13+
all_reqs = f.read().split('\n')
14+
15+
install_requires = [x.strip() for x in all_reqs if 'git+' not in x]
16+
17+
cmdclass = {}
18+
19+
setup(
20+
name='Ensemble-PyTorch',
21+
version='0.0.1',
22+
author='AaronX121',
23+
24+
description=('Implementations of scikit-learn like ensemble methods in'
25+
' Pytorch'),
26+
long_description=long_description,
27+
long_description_content_type='text/markdown',
28+
url='https://github.com/AaronX121/Ensemble-Pytorch',
29+
classifiers=[
30+
'Programming Language :: Python :: 3',
31+
'License :: OSI Approved :: MIT License',
32+
'Operating System :: OS Independent',
33+
],
34+
keywords='Ensemble Learning',
35+
36+
packages=find_packages(),
37+
cmdclass=cmdclass,
38+
install_requires=install_requires,
39+
)

0 commit comments

Comments
 (0)