-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add neural forest ensemble (#110)
* feat: add NeuralForestClassifier * feat: add NeuralForestClassifier * update code * update code * update code * Update requirements.txt * add doc * Update README.rst * update code
- Loading branch information
Showing
13 changed files
with
607 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ sphinxemoji==0.1.8 | |
sphinx-copybutton | ||
m2r2==0.2.7 | ||
mistune==0.8.4 | ||
Jinja2<3.1 | ||
Jinja2<3.1 | ||
Numpy |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import time | ||
import torch | ||
from torchvision import datasets, transforms | ||
|
||
from torchensemble import NeuralForestClassifier | ||
from torchensemble.utils.logging import set_logger | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
# Hyper-parameters | ||
n_estimators = 5 | ||
depth = 5 | ||
lamda = 1e-3 | ||
lr = 1e-3 | ||
weight_decay = 5e-4 | ||
epochs = 50 | ||
|
||
# Utils | ||
cuda = False | ||
n_jobs = 1 | ||
batch_size = 128 | ||
data_dir = "../../Dataset/mnist" # MODIFY THIS IF YOU WANT | ||
|
||
# Load data | ||
train_loader = torch.utils.data.DataLoader( | ||
datasets.MNIST( | ||
data_dir, | ||
train=True, | ||
download=True, | ||
transform=transforms.Compose( | ||
[ | ||
transforms.ToTensor(), | ||
transforms.Normalize((0.1307,), (0.3081,)), | ||
] | ||
), | ||
), | ||
batch_size=batch_size, | ||
shuffle=True, | ||
) | ||
|
||
test_loader = torch.utils.data.DataLoader( | ||
datasets.MNIST( | ||
data_dir, | ||
train=False, | ||
download=True, | ||
transform=transforms.Compose( | ||
[ | ||
transforms.ToTensor(), | ||
transforms.Normalize((0.1307,), (0.3081,)), | ||
] | ||
), | ||
), | ||
batch_size=batch_size, | ||
shuffle=True, | ||
) | ||
|
||
logger = set_logger( | ||
"classification_mnist_tree_ensemble", use_tb_logger=False | ||
) | ||
|
||
model = NeuralForestClassifier( | ||
n_estimators=n_estimators, | ||
depth=depth, | ||
lamda=lamda, | ||
cuda=cuda, | ||
n_jobs=-1, | ||
) | ||
|
||
model.set_optimizer("Adam", lr=lr, weight_decay=weight_decay) | ||
|
||
tic = time.time() | ||
model.fit(train_loader, epochs=epochs, test_loader=test_loader) | ||
toc = time.time() | ||
training_time = toc - tic |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,4 @@ exclude = ''' | |
| dist | ||
| docs | ||
)/ | ||
''' | ||
''' |
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
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
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
Oops, something went wrong.