Commit fcdb87ab authored by Johan Gudmundsson's avatar Johan Gudmundsson
Browse files

clean up tests and linitng

parent fd25cafc
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -27,14 +27,8 @@ def as_tensor(val):
        >>> as_tensor('hello')
        'hello'
    """
    # if isinstance(val, list) and all([isinstance(val, (torch.Tensor, Graph))]):
        # return [as_tensor(_val) for _val in val]

    if isinstance(val, (Number, list, ndarray)):
        try:
        val = torch.tensor(val, dtype=torch.float32)  # pylint: disable=not-callable
        except:
            import ipdb; ipdb.set_trace()
    if isinstance(val, Graph):
        return val.tensor
    return val
+1 −3
Original line number Diff line number Diff line
@@ -464,9 +464,7 @@ class Module(_Module):
                if param is None and isinstance(
                    getattr(prior, name, None), borch.RandomVariable
                ):
                    self.posterior.set_random_variable(
                        name, getattr(prior, name), None
                    )
                    self.posterior.set_random_variable(name, getattr(prior, name), None)
                    param = getattr(posterior, name)
                if isinstance(param, borch.RandomVariable):

+12 −3
Original line number Diff line number Diff line
@@ -46,7 +46,11 @@ def default_rv_factory(_):


def borch_proxy_class(
    cls, get_rv_factory=default_rv_factory, doc_prefix="", borchify_submodules=False, get_extra_baseclasses=None,
    cls,
    get_rv_factory=default_rv_factory,
    doc_prefix="",
    borchify_submodules=False,
    get_extra_baseclasses=None,
):
    """
    Create a Bayesian version of a `torch.nn.Module`
@@ -59,7 +63,9 @@ def borch_proxy_class(
    """
    if cls in BORCHIFY_REGISTRY:
        return BORCHIFY_REGISTRY[cls]
    extra_inheriors = get_extra_baseclasses(cls) if get_extra_baseclasses is not None else []
    extra_inheriors = (
        get_extra_baseclasses(cls) if get_extra_baseclasses is not None else []
    )

    @wraps(cls.__init__)
    def _init(self, *args, posterior=None, **kwargs):
@@ -81,7 +87,9 @@ def borch_proxy_class(
            _borchify_submodules_(self, rv_factory)
        sample(self, posterior=True, prior=True, redraw=False)

    new_cls = type(cls.__name__, (cls, BorchModule, *extra_inheriors), {"__init__": _init})
    new_cls = type(
        cls.__name__, (cls, BorchModule, *extra_inheriors), {"__init__": _init}
    )
    assign_docs(new_cls, cls, doc_prefix, False)
    return new_cls

@@ -216,6 +224,7 @@ class Registry:

BORCHIFY_REGISTRY = Registry()


def borchify_module(
    module: Module, rv_factory: Optional[callable] = None, posterior: Posterior = None
) -> BorchModule:
+7 −6
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ from borch.rv_factories import (
)
from borch.utils.namespace_tools import extend_module
from borch.nn.borchify import borch_classes, BORCHIFY_REGISTRY
from borch import Module as _BorchModule

DOC_PREFIX = """This is a ppl class. Please see ``help(borch.nn)`` for more information.
If one gives distribution as kwargs, where names match the parameters of the Module, they
@@ -186,20 +185,26 @@ _NO_WEIGHTS_MODULE_NAMES = [

class _RNNFlatWeights:
    """Make sure we use getattr for the parameters"""

    @property
    def _flat_weights(self):
        return [(lambda wn: getattr(self, wn) if hasattr(self, wn) else None)(wn) for wn in self._flat_weights_names]
        return [
            (lambda wn: getattr(self, wn) if hasattr(self, wn) else None)(wn)
            for wn in self._flat_weights_names
        ]

    @_flat_weights.setter
    def _flat_weights(self, val):
        pass


def get_extra_baseclasses(cls):
    """Extra base classes for torch proxies"""
    if issubclass(cls, nn.RNNBase):
        return [_RNNFlatWeights]
    return []


_MAPPINGS = borch_classes(
    nn,
    get_rv_factory=get_rv_factory,
@@ -389,7 +394,3 @@ setattr(
    "__all__",
    __all__ + list(_NEWER_MODULES.intersection(set(globals()))),
)



+0 −1
Original line number Diff line number Diff line
@@ -34,7 +34,6 @@ def filepath_to_import_path(path: str) -> str:
def _run_doctest(module, doctest_options: int = 0):
    """Run doctest on a module and raise assertion error on failure."""
    result = testmod(module, optionflags=doctest_options)
    import ipdb; ipdb.set_trace()
    assert not result.failed  # noqa: S101


Loading