Skip to content

TensorFlow 2 model fitting for negative binomial noise #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 142 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
142 commits
Select commit Hold shift + click to select a range
cd8c917
added eager executing tf2 training with nb noise
picciama Oct 22, 2019
2348481
Merge pull request #90 from theislab/dev
davidsebfischer Oct 31, 2019
8f025e2
Merge branch 'tf2_compatibility' into tf2
picciama Nov 5, 2019
dbec1ce
rm trainingStrategies, integrated them into model
picciama Nov 11, 2019
b476313
Merge branch 'dev' into tf2
picciama Nov 11, 2019
e35a44f
added trainingstrategies according to tf1
picciama Nov 11, 2019
224c366
refactoring batched_model->use_batching/is_batched
picciama Nov 12, 2019
94f6568
refactoring optimizer->optim_algo
picciama Nov 12, 2019
6f4f446
Merge branch 'dev' into tf2
picciama Nov 15, 2019
161dfd0
set_floatx("float64") for layers
picciama Nov 15, 2019
0f80f0b
only create new model if not yet present
picciama Nov 15, 2019
ee4ccd1
batchsize now max(n_obs) + fix negation in loss
picciama Nov 15, 2019
88ae48a
fix default convergence criteria
picciama Nov 15, 2019
f0211b8
reset training_strategies correctly
picciama Nov 15, 2019
60027f7
convert params to numpy in finalize
picciama Nov 15, 2019
e4a5594
added api imports back in
picciama Nov 15, 2019
ee92ded
cleanup, warning logs, no final assign jac, hes
picciama Nov 29, 2019
65016d2
hessian calculation moved to its own method
picciama Nov 29, 2019
486d1ba
lower case name checks
picciama Nov 29, 2019
a97be15
added ADAM/ADAM_BATCHED to nb training strategies
picciama Nov 30, 2019
b47cfcd
added final eval run + fix featurewise dim problem
picciama Dec 12, 2019
c441463
Merge branch 'dev' into tf2
picciama Jan 12, 2020
95eae82
Merge branch 'fix_size_factors_init' into tf2
picciama Jan 12, 2020
2c26a23
bugfix: size_factors init dimension expansion
picciama Jan 12, 2020
99e07c0
Merge branch 'fix_size_factors_init' into tf2
picciama Jan 12, 2020
cc2d0e0
rm size_factors_tensor postprocessing in fetch_fn
picciama Jan 12, 2020
0e849b6
bugfix: calc_x_step: if cond never true
picciama Jan 13, 2020
1cfa504
added TRTOL_BY_FEATURE_LOC/SCALE thresholds
picciama Jan 14, 2020
7939d88
added tr convergence / reworked convergence func
picciama Jan 14, 2020
4726caf
Merge branch 'dev' into tf2
picciama Jan 14, 2020
ffe23ce
bugfix: major updates of convergence method
picciama Jan 14, 2020
2e17945
bugfix: remove curr/prev_norm_lo/scale
picciama Jan 14, 2020
c4bcdb3
bugfix: logging pattern
picciama Jan 14, 2020
f2bd91b
removed shuffling from full data model
picciama Jan 15, 2020
42181c7
bugfix: missing inversion of hessian in last run
picciama Jan 15, 2020
183a19f
bugfix: final run should return non neg ll
picciama Jan 15, 2020
f981f85
Merge branch 'master' into tf2
picciama Jan 15, 2020
10e10c2
bugfix: correct jacobian and hessians in final run
picciama Jan 16, 2020
db803b5
Custom Generator implemented (testing)
kadam0 Jan 16, 2020
6d907c5
cleanup in model, support multiple optim runs
picciama Jan 17, 2020
98b2bf1
generator inside for loop
kadam0 Jan 18, 2020
fff74e1
Merge branch 'tf2' into generator_tf2
kadam0 Jan 18, 2020
a14c1df
changed generator outputtype
kadam0 Jan 18, 2020
b2a6fc0
bugfixes in generator
picciama Jan 18, 2020
4502566
bugfixes in generator for full model
picciama Jan 19, 2020
c421139
rm additional n_obs assignment
picciama Jan 19, 2020
9ec92cb
cleanup train method
picciama Jan 19, 2020
70955f0
further cleanup and bugfixes
picciama Jan 19, 2020
3fc0e62
bugfix: convergence decision
picciama Jan 19, 2020
9361991
compute scale gradient in final run
picciama Jan 20, 2020
a64c3ea
resturcture train: support for batched mode
picciama Jan 20, 2020
4dc0f2e
Merge branch 'tf2' into generator_tf2
picciama Jan 20, 2020
a3ca215
always call input_data property not _input_data
picciama Jan 20, 2020
08d32b0
Merge pull request #101 from theislab/generator_tf2
picciama Jan 20, 2020
8407af1
log updated and converged in each step
picciama Jan 21, 2020
c9ee70f
bugfixes: full model for 2nd order eval
picciama Feb 5, 2020
e0cbb10
bugfixes: ll now summed up over obs + tr update
picciama Feb 5, 2020
81b523a
bugfix: always recalculate ll if only compute_b
picciama Feb 5, 2020
a3df1b0
bugfix: final ll calc, prefetch, map + last batch
picciama Feb 6, 2020
40543a3
style changes
picciama Feb 6, 2020
2308e09
added scale specific constants
picciama Feb 10, 2020
7096fa6
added ll by step container for benchmarking
picciama Feb 10, 2020
43d6ab7
rm T1 and T2 + proposed gain checks for TR update
picciama Feb 10, 2020
539bb5b
integrated featurewise batching in dataset gen
picciama Feb 11, 2020
8ffcbd4
forward keep_previous_params_copy everywhere
picciama Feb 11, 2020
3dc6eea
rm keep_previous_params_copy form model call
picciama Feb 11, 2020
ed47e93
FEATUREWISE_THRESHOLD for featurewise step control
picciama Feb 11, 2020
3d73491
bugfix: wrong default parameter in calc_ll
picciama Feb 11, 2020
fba55c9
bugfix: calc of conv diff to last features batch
picciama Feb 12, 2020
2cc1e8b
bugfix: loss increases when featurewise
picciama Mar 7, 2020
b925504
workaround for numeric instabilities
picciama Mar 9, 2020
128e04c
bugfix: always keeps param_copy if using ADAM
picciama Mar 9, 2020
475c93c
bugfix: ADAM still keeping params copy
picciama Mar 9, 2020
e1fa6ed
workaround: boolean mask in each run (issue #21)
picciama Mar 9, 2020
0d65ab0
bugfix: wrong parameter updates
picciama Mar 11, 2020
6791c4e
default batchsize raised to 5000
picciama Mar 14, 2020
eb87404
stricter likelihood criterion
picciama Mar 19, 2020
6df19e2
even stricter likelihood criterion
picciama Mar 19, 2020
c5c6a5e
increased default batch size to 5000
picciama Mar 20, 2020
af5e235
removed n_features argument from likelihood call
picciama Mar 20, 2020
58ce375
fallback to non featurewise if 1st order optim
picciama Mar 20, 2020
30c7d8a
added convergence and generator outside train
picciama Mar 23, 2020
498540d
weaker ll_tol
picciama Mar 24, 2020
fa969a4
bugfixes, separate loc and scale update
picciama Mar 24, 2020
54a5250
documentation for train provided in code.
picciama Mar 24, 2020
64186af
reworked spare featurewise batching method
picciama Mar 25, 2020
14fa78b
stepwsie featurewise batching implemented
picciama Mar 25, 2020
30699bf
featurewise_threshold set to 100
picciama Mar 26, 2020
0508ef7
bugfix: new epoch set but threhsold not reached
picciama Mar 26, 2020
2b35bac
documentation/cleanup new methods for featurewise
picciama Mar 27, 2020
55190f4
implemented featurewise without 2nd model call
picciama Mar 27, 2020
c2fe150
added boolean for recalc model call if featurewise
picciama Mar 27, 2020
089c2b4
Merge branch 'singular_fix' into tf2
picciama Mar 30, 2020
493e20c
started implementing linesearch
picciama Mar 31, 2020
c5ef7d9
bugfixes to get irls_gd_tr to work
picciama Mar 31, 2020
5507412
Merge branch 'singular_fix' into linesearch
picciama Mar 31, 2020
e55565c
Merge branch 'tf2' into linesearch
picciama Mar 31, 2020
f7f6184
added irls_ar_tr to known optimizers
picciama Apr 12, 2020
0ae2215
added irls_ar_tr to known optimizers
picciama Apr 12, 2020
1ededb8
implemented first version of update_b_ar
picciama Apr 12, 2020
d71ecfa
added WOLFE C1 and C2
picciama Apr 13, 2020
28c6af3
make compute_a and compute_b function args
picciama Apr 13, 2020
2096829
cleanup and added return val to trust_region_ops
picciama Apr 13, 2020
3a02a57
divided loc and scale update calls
picciama Apr 13, 2020
4ab9cce
removed compute_a/b from gradient layer init
picciama Apr 13, 2020
c8575dd
cleanup and update_b in while loop with maxiter
picciama Apr 13, 2020
69cbdbb
added support for b_update_freq
picciama Apr 15, 2020
def51e4
bugfix: wrong positions of defautl args
picciama Apr 15, 2020
44abc64
rm or equal cond for updated_b
picciama Apr 15, 2020
561306f
init updated vectors with False
picciama Apr 15, 2020
54b0bfb
added support for b_update_freq and maxiter
picciama Apr 15, 2020
442572a
get update_b loop to work
picciama Apr 15, 2020
b18bc5b
bugfix: catch errors in fisher inv
picciama Apr 15, 2020
9d5f458
bugfix: wrong method call to jacobian
picciama Apr 22, 2020
5205d4e
set to updated if proposed vector smaller TR_TOL
picciama Apr 22, 2020
2407938
Merge branch 'dev' into linesearch_tf2
picciama Apr 22, 2020
4a3491c
Merge branch 'dev' into tf2
picciama Apr 22, 2020
bece862
make TRTOL same as XTOL
picciama May 5, 2020
20e7188
cleanup train/base, rm Loss class
picciama May 5, 2020
83648d2
rm layers as args, Loss + add layers in base class
picciama May 5, 2020
dc4fd93
cleanup glm_norm folder, rm Loss
picciama May 5, 2020
1a46283
cleanup glm_beta folder, rm Loss
picciama May 5, 2020
ae9d864
ll_dtype->self.dtype in likelihood
picciama May 5, 2020
3fc33b5
rm Loss
picciama May 5, 2020
7ab343e
add support for irls(_tr)_gd_tr, cleanup
picciama May 5, 2020
98d3fe6
cleanup, revert updates not only if tr_mode
picciama May 5, 2020
75ed5b8
pass dtype to keras superclass for all layers
picciama May 5, 2020
2e1557c
jumpt to scale update if loc already converged
picciama May 5, 2020
a61b0dd
add support for irls(_tr)_gd_tr
picciama May 5, 2020
7264bf8
rm Loss
picciama May 5, 2020
a7539b0
alpha0 addet for armijo line search
picciama May 8, 2020
b0e6bd4
bugfix: scale grads zero if b_update_freq change
picciama May 8, 2020
7c8c0ba
updated known optimizer strings
picciama May 8, 2020
0082b82
style issue fix
picciama May 8, 2020
621083d
rm IRLS_LS strategy
picciama May 8, 2020
fd7ca8c
bugfix: NaNs in armijo steplength
picciama May 8, 2020
e848d00
Merge branch 'linesearch_tf2' into tf2
picciama May 8, 2020
ba7e1a9
default b_update-freq set to 1
picciama May 8, 2020
64f46c6
bugfix for #44: fisher_inv not saved in property
picciama Jun 11, 2020
5d03ee0
removed TODO after bugfix #44
picciama Jun 11, 2020
699d26d
simplify masking
picciama Sep 27, 2021
e85a881
cleanup
picciama Sep 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions batchglm/api/models/tf2/glm_beta.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#from batchglm.models.glm_beta import InputDataGLM, Model, Simulator
#from batchglm.train.tf2.glm_beta import Estimator
from batchglm.models.glm_beta import InputDataGLM, Model, Simulator
from batchglm.train.tf2.glm_beta import Estimator
4 changes: 2 additions & 2 deletions batchglm/api/models/tf2/glm_norm.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#from batchglm.models.glm_norm import InputDataGLM, Model, Simulator
#from batchglm.train.tf2.glm_norm import Estimator
from batchglm.models.glm_norm import InputDataGLM, Model, Simulator
from batchglm.train.tf2.glm_norm import Estimator
18 changes: 15 additions & 3 deletions batchglm/pkg_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,34 @@

# Trust region hyper parameters:
TRUST_REGION_RADIUS_INIT = 100.
TRUST_REGION_RADIUS_INIT_SCALE = 1.
TRUST_REGION_ETA0 = 0.
TRUST_REGION_ETA1 = 0.25
TRUST_REGION_ETA2 = 0.25
TRUST_REGION_T1 = 0.5 # Fast collapse to avoid trailing.
TRUST_REGION_T2 = 1.5 # Allow expansion if not shrinking.
TRUST_REGION_UPPER_BOUND = 1e5
TRUST_REGION_UPPER_BOUND = 1e40

TRUST_REGIONT_T1_IRLS_GD_TR_SCALE = 1
TRUST_REGIONT_T1_IRLS_GD_TR_SCALE = 0.5
TRUST_REGIONT_T2_IRLS_GD_TR_SCALE = 1.5

# Convergence hyper-parameters:
LLTOL_BY_FEATURE = 1e-10
LLTOL_BY_FEATURE = 1e-12
XTOL_BY_FEATURE_LOC = 1e-8
XTOL_BY_FEATURE_SCALE = 1e-6
GTOL_BY_FEATURE_LOC = 1e-8
GTOL_BY_FEATURE_SCALE = 1e-8

TRTOL_BY_FEATURE_LOC = 1e-8
TRTOL_BY_FEATURE_SCALE = 1e-6

FEATUREWISE_THRESHOLD = 10 # the minimal number of features to converge before next featurewise batch
FEATUREWISE_RECALCULATE = False # if set to True, recalculate the results from the previous train step

WOLFE_C1 = 1e-3
WOLFE_C2 = 0.99
ALPHA0 = 100

try:
import tensorflow as tf

Expand Down
3 changes: 3 additions & 0 deletions batchglm/train/tf2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import glm_nb as nb
from . import glm_norm as norm
from . import glm_beta as beta
3 changes: 3 additions & 0 deletions batchglm/train/tf2/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .estimator import TFEstimator
from .model import ProcessModelBase, ModelBase
from .optim import OptimizerBase
28 changes: 28 additions & 0 deletions batchglm/train/tf2/base/estimator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from .model import ModelBase
import tensorflow as tf


class TFEstimator:
model: ModelBase

def __init__(self, input_data, dtype):

self._input_data = input_data
self.dtype = dtype

def _train(
self,
is_batched: bool,
batch_size: int,
optimizer_object: tf.keras.optimizers.Optimizer,
convergence_criteria: str,
stopping_criteria: int,
autograd: bool,
featurewise: bool,
benchmark: bool,
optimizer: str
):
pass

def fetch_fn(self, idx):
pass
4 changes: 4 additions & 0 deletions batchglm/train/tf2/base/external.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#from batchglm.models.base import _Estimator_Base
#from batchglm.xarray_sparse import SparseXArrayDataArray, SparseXArrayDataSet
#import batchglm.utils.stats as stat_utils
from batchglm import pkg_constants
47 changes: 47 additions & 0 deletions batchglm/train/tf2/base/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import abc
import logging
import tensorflow as tf
import numpy as np

logger = logging.getLogger(__name__)


class ModelBase(tf.keras.Model, metaclass=abc.ABCMeta):

def __init__(self, dtype):
super(ModelBase, self).__init__(dtype=dtype)

@abc.abstractmethod
def call(self, inputs, training=False, mask=None):
pass


class ProcessModelBase:

@abc.abstractmethod
def param_bounds(self, dtype):
pass

def tf_clip_param(
self,
param,
name
):
bounds_min, bounds_max = self.param_bounds(param.dtype)
return tf.clip_by_value(
param,
bounds_min[name],
bounds_max[name]
)

def np_clip_param(
self,
param,
name
):
bounds_min, bounds_max = self.param_bounds(param.dtype)
return np.clip(
param,
bounds_min[name],
bounds_max[name]
)
52 changes: 52 additions & 0 deletions batchglm/train/tf2/base/optim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import abc
import logging
import tensorflow as tf

logger = logging.getLogger("batchglm")


class OptimizerBase(tf.keras.optimizers.Optimizer, metaclass=abc.ABCMeta):

def __init__(self, name):
super(OptimizerBase, self).__init__(name=name)

@abc.abstractmethod
def _resource_apply_dense(self, grad, handle):
pass

@abc.abstractmethod
def _resource_apply_sparse(self, grad, handle, apply_state):
pass

@abc.abstractmethod
def _create_slots(self):
pass

"""
@property
@abc.abstractmethod
def vars(self):
pass

@property
@abc.abstractmethod
def gradients(self):
return None

@property
@abc.abstractmethod
def hessians(self):
pass

@property
@abc.abstractmethod
def fims(self):
pass

@abc.abstractmethod
def step(self, learning_rate):
pass
"""
@abc.abstractmethod
def get_config(self):
pass
2 changes: 2 additions & 0 deletions batchglm/train/tf2/base_glm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Classes with GLM specific code.
All noise models that are in the GLM category inherit all of these classes.
12 changes: 12 additions & 0 deletions batchglm/train/tf2/base_glm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from .processModel import ProcessModelGLM
from .model import GLM

from .estimator import Estimator
from .vars import ModelVarsGLM
from .layers import LinearLocGLM, LinearScaleGLM, LinkerLocGLM, LinkerScaleGLM
from .layers import LikelihoodGLM, UnpackParamsGLM
from .layers_gradients import JacobianGLM, HessianGLM, FIMGLM
from .optim import NR, IRLS

from .generator import DataGenerator
from .convergence import ConvergenceCalculator
Loading