Skip to content

Commit

Permalink
final_commission
Browse files Browse the repository at this point in the history
  • Loading branch information
dut-media-lab committed Sep 27, 2020
1 parent 0375e6c commit 903769e
Show file tree
Hide file tree
Showing 25 changed files with 476 additions and 145 deletions.
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ script:
- coverage run -p test_meta_init.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=200 --T=1 --meta-lr=0.001 --lr=0.1 --method=MetaInit --inner_method=Simple --outer_method=Simple --learn_lr=True --logdir=../tmp/ --clip_value=10.0 --first_order=True --print-interval=100 --save_interval=100 --use_t=True --Notes=toolkit_TNet
- coverage run -p test_meta_init.py --mode=test --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=200 --T=1 --meta-lr=0.001 --lr=0.1 --method=MetaInit --inner_method=Simple --outer_method=Simple --logdir=../tmp/ --expdir=../tmp/ --clip_value=10.0 --iterations_to_test=100 --print-interval=100 --save_interval=100 --use_t=True --Notes=toolkit_TNet
- coverage run -p test_meta_init.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=10 --T=2 --meta-lr=0.001 --lr=0.1 --method=MetaInit --inner_method=Simple --outer_method=Simple --logdir=../tmp/ --expdir=../tmp/ --print-interval=100 --first_order=True --save_interval=100 --use_warp=True --Notes=toolkit_warp
- coverage run -p test_meta_repr.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=100 --T=2 --meta-lr=0.001 --lr=0.1 --method=MetaRepr --inner_method=Trad --outer_method=Reverse --inner_opt=Adam --outer_opt=Adam --clip_value=10.0 --logdir=../tmp/ --print-interval=100 --truncate_iter=1 --save_interval=100 --Notes=toolkit_test_TRHG
- coverage run -p test_meta_repr.py --mode=test --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=100 --T=2 --meta-lr=0.001 --lr=0.1 --method=MetaRepr --inner_method=Trad --outer_method=Reverse --inner_opt=Adam --outer_opt=Adam --clip_value=10.0 --logdir=../tmp/ --expdir=../tmp/ --print-interval=100 --truncate_iter=1 --save_interval=100 --Notes=toolkit_test_TRHG
- coverage run -p test_meta_repr.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=10 --T=1 --meta-lr=0.001 --lr=0.1 --method=MetaRepr --inner_method=Aggr --learn_alpha_itr=True --learn_st=True --outer_method=Reverse --inner_opt=Momentum --outer_opt=Momentum --logdir=../tmp/ --alpha=0.7 --print-interval=100 --save_interval=100 --Notes=toolkit_test_BDA
- coverage run -p test_meta_repr.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=10 --T=1 --meta-lr=0.001 --lr=0.1 --method=MetaRepr --inner_method=Aggr --learn_alpha=True --learn_st=True --outer_method=Reverse --inner_opt=Momentum --outer_opt=Momentum --logdir=../tmp/ --alpha=0.3 --print-interval=100 --save_interval=100 --Notes=toolkit_test_BDA_alpha_iter
- coverage run -p test_meta_repr.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=2 --meta_train_iterations=10 --T=1 --meta-lr=0.001 --lr=0.1 --method=MetaRepr --inner_method=Aggr --outer_method=Darts --alpha=0.7 --logdir=../tmp/ --print-interval=100 --save_interval=100 --Notes=toolkit_test_DARTS
- coverage run -p test_meta_repr.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=2 --meta_train_iterations=10 --T=1 --meta-lr=0.001 --lr=0.1 --method=MetaRepr --inner_method=Aggr --outer_method=Darts --alpha=0.7 --learn_alpha_itr=True --logdir=../tmp/ --print-interval=100 --save_interval=100 --Notes=toolkit_test_DARTS_alpha_iter
- coverage run -p test_meta_repr.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=10 --T=1 --meta-lr=0.001 --lr=0.1 --method=MetaRepr --inner_method=Trad --outer_method=Implicit --inner_opt=SGD --outer_opt=Adam --logdir=../tmp/ --print-interval=100 --save_interval=100 --Notes=toolkit_test_Implicit
- coverage run -p test_meta_feat.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=100 --T=2 --meta-lr=0.001 --lr=0.1 --method=MetaFeat --inner_method=Trad --outer_method=Reverse --inner_opt=Adam --outer_opt=Adam --clip_value=10.0 --logdir=../tmp/ --print-interval=100 --truncate_iter=1 --save_interval=100 --Notes=toolkit_test_TRHG
- coverage run -p test_meta_feat.py --mode=test --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=100 --T=2 --meta-lr=0.001 --lr=0.1 --method=MetaFeat --inner_method=Trad --outer_method=Reverse --inner_opt=Adam --outer_opt=Adam --clip_value=10.0 --logdir=../tmp/ --expdir=../tmp/ --print-interval=100 --truncate_iter=1 --save_interval=100 --Notes=toolkit_test_TRHG
- coverage run -p test_meta_feat.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=10 --T=1 --meta-lr=0.001 --lr=0.1 --method=MetaFeat --inner_method=Aggr --learn_alpha_itr=True --learn_st=True --outer_method=Reverse --inner_opt=Momentum --outer_opt=Momentum --logdir=../tmp/ --alpha=0.7 --print-interval=100 --save_interval=100 --Notes=toolkit_test_BDA
- coverage run -p test_meta_feat.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=10 --T=1 --meta-lr=0.001 --lr=0.1 --method=MetaFeat --inner_method=Aggr --learn_alpha=True --learn_st=True --outer_method=Reverse --inner_opt=Momentum --outer_opt=Momentum --logdir=../tmp/ --alpha=0.3 --print-interval=100 --save_interval=100 --Notes=toolkit_test_BDA_alpha_iter
- coverage run -p test_meta_feat.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=2 --meta_train_iterations=10 --T=1 --meta-lr=0.001 --lr=0.1 --method=MetaFeat --inner_method=Aggr --outer_method=Darts --alpha=0.7 --logdir=../tmp/ --print-interval=100 --save_interval=100 --Notes=toolkit_test_DARTS
- coverage run -p test_meta_feat.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=2 --meta_train_iterations=10 --T=1 --meta-lr=0.001 --lr=0.1 --method=MetaFeat --inner_method=Aggr --outer_method=Darts --alpha=0.7 --learn_alpha_itr=True --logdir=../tmp/ --print-interval=100 --save_interval=100 --Notes=toolkit_test_DARTS_alpha_iter
- coverage run -p test_meta_feat.py --mode=train --dataset=omniglot --classes=5 --examples_train=1 --examples_test=1 --meta_batch_size=1 --meta_train_iterations=10 --T=1 --meta-lr=0.001 --lr=0.1 --method=MetaFeat --inner_method=Trad --outer_method=Implicit --inner_opt=SGD --outer_opt=Adam --logdir=../tmp/ --print-interval=100 --save_interval=100 --Notes=toolkit_test_Implicit
- coverage combine

after_success:
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

BOML is a modularized optimization library that unifies several ML algorithms into a common bilevel optimization framework. It provides interfaces to implement popular bilevel optimization algorithms, so that you could quickly build your own meta learning neural network and test its performance.

ReadMe.md file contains brief introduction to implement meta-initialization-based and meta-feature-based methods in few-shot learning field. Except for algorithms which have been proposed, various combinations of lower level and upper level strategies are available.
ReadMe.md contains brief introduction to implement meta-initialization-based and meta-feature-based methods in few-shot classification field. Except for algorithms which have been proposed, various combinations of lower level and upper level strategies are available.

## Meta Learning

Expand Down Expand Up @@ -54,7 +54,7 @@ ex = boml.BOMLExperiment(dataset)

```

### Build network structure and define hyperparameters
### Build network structure and define parameters for meta-learner and base-learner
```python
boml_ho = boml.BOMLOptimizer(
method="MetaInit", inner_method="Simple", outer_method="Simple"
Expand All @@ -76,7 +76,7 @@ inner_grad = boml_ho.ll_problem(
```
### Define UL objectives and UL calculation process
```python
loss_outer = utils.cross_entropy(pred=ex.model.re_forward(ex.x_).out, label=ex.y_)
loss_outer = utils.cross_entropy(pred=ex.model.re_forward(ex.x_).out, label=ex.y_) # loss function
boml_ho.ul_problem(
outer_objective=loss_outer,
meta_learning_rate=args.meta_lr,
Expand Down Expand Up @@ -108,14 +108,14 @@ with tf.Session() as sess:
## Related Methods
- [Hyperparameter optimization with approximate gradient(HOAG)](https://arxiv.org/abs/1602.02355)
- [Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks(MAML)](https://arxiv.org/abs/1703.03400)
- [On First-Order Meta-Learning Algorithms(FOMAML)](https://arxiv.org/abs/1803.02999)
- [On First-Order Meta-Learning Algorithms(FMAML)](https://arxiv.org/abs/1703.03400)
- [Meta-SGD: Learning to Learn Quickly for Few-Shot Learning(Meta-SGD)](https://arxiv.org/pdf/1707.09835.pdf)
- [Bilevel Programming for Hyperparameter Optimization and Meta-Learning(RHG)](http://export.arxiv.org/pdf/1806.04910)
- [Truncated Back-propagation for Bilevel Optimization(TG)](https://arxiv.org/pdf/1810.10667.pdf)
- [Gradient-Based Meta-Learning with Learned Layerwise Metric and Subspace(MT-net)](http://proceedings.mlr.press/v80/lee18a/lee18a.pdf)
- [Meta-Learning with warped gradient Descent(WarpGrad))](https://arxiv.org/abs/1909.00025)
- [DARTS: Differentiable Architecture Search(DARTS)](https://arxiv.org/pdf/1806.09055.pdf)
- [A Generic First-Order Algorithmic Framework for Bi-Level Programming Beyond Lower-Level Singleton(BA)](https://arxiv.org/pdf/2006.04045.pdf)
- [A Generic First-Order Algorithmic Framework for Bi-Level Programming Beyond Lower-Level Singleton(BDA)](https://arxiv.org/pdf/2006.04045.pdf)



Expand Down
17 changes: 12 additions & 5 deletions boml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
try:
import tensorflow as tf
except ImportError:
print("No compatible version of TensorFlow has been installed!")

from boml.load_data.experiment import BOMLExperiment
from boml.load_data import (
meta_mini_imagenet,
Expand All @@ -12,17 +17,17 @@
BOMLNetMiniMetaInitV1,
)
from boml.setup_model.meta_repr_v1 import (
BOMLNetMetaReprV1,
BOMLNetMiniMetaReprV1,
BOMLNetOmniglotMetaReprV1,
BOMLNetMetaFeatV1,
BOMLNetMiniMetaFeatV1,
BOMLNetOmniglotMetaFeatV1,
)
from boml.setup_model.meta_init_v2 import (
BOMLNetOmniglotMetaInitV2,
BOMLNetMiniMetaInitV2,
)
from boml.setup_model.meta_repr_v2 import (
BOMLNetOmniglotMetaReprV2,
BOMLNetMiniMetaReprV2,
BOMLNetOmniglotMetaFeatV2,
BOMLNetMiniMetaFeatV2,
)
from boml.setup_model.network import BOMLNet
from boml.setup_model.feedforward import BOMLNetFeedForward
Expand All @@ -31,3 +36,5 @@
from boml.extension import get_outerparameter
from boml import utils
from boml import extension


43 changes: 22 additions & 21 deletions boml/boml_optimizer/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class BOMLOptimizer(object):
"""
Wrapper for performing gradient-based metaparameter optimization
Wrapper for performing gradient-based parameter optimization
"""

def __init__(
Expand All @@ -32,26 +32,27 @@ def __init__(
experiments=[],
):
"""
BMLHOptimizer is the main class in `pybml`, which takes responsibility for
BMLHOptimizer is the main class in `boml`, which takes responsibility for
the whole process of model construnction and back propagation.
:param Method: define basic method for following training process, it should be included in ['MetaInit', 'MetaRepr'],
'MetaInit' type includes meta-initialization-based methods like 'MAML, FOMAML, MT-net, WarpGrad';
'MetaRepr' type includes meta-feature-based methods like
'BDA, RHG, Truncated, Implicit HG, DARTS';
:param inner_method: method chosen for solving LLproblem, including ['Trad' ,'Simple', 'Aggr'], 'MetaRepr' type choose
either 'Trad' for traditional optimization strategies or 'Aggr' for Gradient Aggragation optimization 'MetaInit' type
should choose 'Simple', and set specific parameters for detailed method choices like FOMAML or TNet.
:param outer_method: method chosen for solving LLproblem, including ['Reverse' ,'Simple', 'Forward', 'Implcit'],
'MetaInit' type should choose Simple, and set specific parameters for detailed method choices like FOMAML
:param method: define basic method for following training process, it should be included in
['MetaInit', 'MetaFeat'], 'MetaInit' type includes meta-initialization-based methods like
'MAML, FOMAML, MT-net, WarpGrad'; 'MetaFeat' type includes meta-feature-based methods like
'BDA, RHG, TRHG, Implicit HG, DARTS';
:param inner_method: method chosen for solving LLproblem, including ['Trad' ,'Simple', 'Aggr'],
'MetaFeat' type choose either 'Trad' for traditional optimization strategies or 'Aggr' for
Gradient Aggregation optimization 'MetaInit' type
should choose 'Simple', and set specific parameters for detailed method choices like FMAML or TNet.
:param outer_method: method chosen for solving LLproblem, including ['Reverse' ,'Simple', 'Forward', 'Implicit'],
'MetaInit' type should choose Simple, and set specific parameters for detailed method choices like FMAML
:param truncate_iter: specific parameter for Truncated Reverse method, defining number of iterations to truncate
in the Back propagation process
:param experiments: list of experiment objects that has already been initialized
:return :an initialized instance of BMLHOptimizer
"""
assert method in ("MetaRepr", "MetaInit"), (
"initialize method arguement, should be in list \an [MetaRepr,MetaInitl] "
"MetaRepr based methods include [BDA,FHG,RHG,TRHG],"
assert method in ("MetaFeat", "MetaInit"), (
"initialize method arguement, should be in list \an [MetaFeat,MetaInitl] "
"MetaFeat based methods include [BDA,FHG,RHG,TRHG],"
"HperOptim based methods include [MAML,FOMAML,MSGD]"
)
self._method = method
Expand Down Expand Up @@ -172,7 +173,7 @@ def base_learner(
:param _input: orginal input for neural network construction of task-specific module
:param meta_learner: returned value of Meta_model function, which is a instance of BMLNet or its child classes
:param name: name for Base model modules used for BMLNet initialization
:param weights_initializer: initializer function for task_specific network, called by 'MetaRepr' method
:param weights_initializer: initializer function for task_specific network, called by 'MetaFeat' method
:return: task-specific model part
"""
if self.method == "MetaInit":
Expand All @@ -194,7 +195,7 @@ def base_learner(
outer_method=self.outer_method,
use_warp=meta_learner.use_warp,
)
elif self.method == "MetaRepr":
elif self.method == "MetaFeat":
base_learner = getattr(boml_networks, "BOMLNetFeedForward")(
_input=_input,
dims=self.data_set.train.dim_target,
Expand All @@ -203,7 +204,7 @@ def base_learner(
)
else:
print(
"initialize method arguement, should be in list \an [MetaRepr,MetaInitl]"
"initialize method arguement, should be in list \an [MetaFeat,MetaInitl]"
)
raise AssertionError
return base_learner
Expand All @@ -214,6 +215,7 @@ def ll_problem(
learning_rate,
T,
var_list=None,
experiment=None,
inner_objective_optimizer="SGD",
outer_objective=None,
learn_lr=False,
Expand All @@ -226,7 +228,6 @@ def ll_problem(
first_order=False,
loss_func=utils.cross_entropy,
momentum=0.5,
experiment=None,
**inner_kargs
):
"""
Expand Down Expand Up @@ -288,10 +289,10 @@ def ll_problem(
"found {} instead".format(type(self.io_opt))
)
assert self._method in (
"MetaRepr",
"MetaFeat",
"MetaInit",
), "illegal initialization value for argument:method, should be in [MetaRepr, MetaInit]"
if self.method == "MetaRepr":
), "illegal initialization value for argument:method, should be in [MetaFeat, MetaInit]"
if self.method == "MetaFeat":
if self.inner_method == "Aggr":
assert (
outer_objective is not None
Expand Down
2 changes: 1 addition & 1 deletion boml/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from boml import utils

Meta_Repr_Method = ["MAML", "FOMAML", "Meta-SGD", "MT-net", "WarpGrad"]
Meta_Repr_Method = ["MAML", "FMAML", "Meta-SGD", "MT-net", "WarpGrad"]
Meta_Init_Method = ["RHG", "TRHG", "DARTS", "BDA", "Implicit"]
METHOD_COLLECTIONS = [Meta_Repr_Method, Meta_Init_Method]

Expand Down
48 changes: 37 additions & 11 deletions boml/load_data/datasets/dl_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from functools import reduce

import numpy as np
Expand All @@ -8,6 +7,12 @@


def get_indices_balanced_classes(n_examples, labels, forbidden_indices):
"""
:param n_examples: number of examples
:param labels: paired labels
:param forbidden_indices:
:return:
"""
N = len(labels)
n_classes = len(labels[0])

Expand All @@ -28,6 +33,10 @@ def get_indices_balanced_classes(n_examples, labels, forbidden_indices):


def test_if_balanced(dataset):
"""
:param dataset:
:return: just for testing
"""
labels = dataset.target
n_classes = len(labels[0])
class_counter = [0] * n_classes
Expand All @@ -37,10 +46,19 @@ def test_if_balanced(dataset):


def maybe_cast_to_scalar(what):
"""
:param what: input
:return: return the scalar of input if the length of input equals 1.
"""
return what[0] if len(what) == 1 else what


def pad(_example, _size):
"""
:param _example:
:param _size:
:return: performs concatenation
"""
return np.concatenate([_example] * _size)


Expand Down Expand Up @@ -68,6 +86,10 @@ def vstack(lst):


def convert_sparse_matrix_to_sparse_tensor(X):
"""
:param X: sparse matrix
:return: sparse tensor of X
"""
if isinstance(X, sc_sp.csr.csr_matrix):
coo = X.tocoo()
indices = np.mat([coo.row, coo.col]).transpose()
Expand All @@ -78,6 +100,10 @@ def convert_sparse_matrix_to_sparse_tensor(X):


def get_data(d_set):
"""
:param d_set: instance of dataset
:return: inputs of datasets
"""
if hasattr(d_set, "images"):
data = d_set.images
elif hasattr(d_set, "data"):
Expand All @@ -88,6 +114,10 @@ def get_data(d_set):


def get_targets(d_set):
"""
:param d_set: instance of dataset
:return: labels of datasets
"""
if hasattr(d_set, "labels"):
return d_set.labels
elif hasattr(d_set, "target"):
Expand Down Expand Up @@ -147,18 +177,14 @@ def as_tuple_or_list(obj):


def to_one_hot_enc(seq, dimension=None):
"""
:param seq: sequence
:param dimension: the dimension of output
:return: output of one-hot encoding
"""
da_max = dimension or int(np.max(seq)) + 1
_tmp = np.zeros((len(seq), da_max))
_tmp[range(len(_tmp)), np.array(seq, dtype=int)] = 1
return _tmp
#
# def create_and_set(_p):
# _tmp = np.zeros(da_max)
# _tmp[int(_p)] = 1
# return _tmp
#
# return np.array([create_and_set(_v) for _v in seq])


from_env = os.getenv("DATASETS_FOLDER")
DATA_FOLDER = from_env
8 changes: 1 addition & 7 deletions boml/load_data/datasets/load_full_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,8 @@

print("Data folder is", DATA_FOLDER)

# kind of private
TIMIT_DIR = os.path.join(DATA_FOLDER, "timit4python")
XRMB_DIR = os.path.join(DATA_FOLDER, "XRMB")
IROS15_BASE_FOLDER = os.path.join(
DATA_FOLDER, os.path.join("dls_collaboration", "Learning")
)

# easy to find!
# easy to find
IRIS_TRAINING = os.path.join(DATA_FOLDER, "iris", "training.csv")
IRIS_TEST = os.path.join(DATA_FOLDER, "iris", "test.csv")
MNIST_DIR = os.path.join(DATA_FOLDER, "mnist_data")
Expand Down
Loading

0 comments on commit 903769e

Please sign in to comment.