Exemplo n.º 1
0
def tensor_to_arraybox(x, *args):
    """Convert a :class:`~.tensor` to an Autograd ``ArrayBox``.

    Args:
        x (array_like): Any data structure in any form that can be converted to
            an array. This includes lists, lists of tuples, tuples, tuples of tuples,
            tuples of lists and ndarrays.

    Returns:
        autograd.numpy.numpy_boxes.ArrayBox: Autograd ArrayBox instance of the array

    Raises:
        NonDifferentiableError: if the provided tensor is non-differentiable
    """
    if isinstance(x, tensor):
        if x.requires_grad:
            return ArrayBox(x, *args)

        raise NonDifferentiableError(
            f"{x} is non-differentiable. Set the requires_grad attribute to True."
        )

    return ArrayBox(x, *args)
Exemplo n.º 2
0
        new_state = pickled_state[2] + (self.__dict__,)
        # Return a tuple that replaces the parent's __setstate__ tuple with our own
        return (pickled_state[0], pickled_state[1], new_state)

    def __setstate__(self, state):
        self.__dict__.update(state[-1])  # Update the internal dict from state
        # Call the parent's __setstate__ with the other tuple elements.
        super().__setstate__(state[0:-1])

    @property
    def _data(self):
        return self.view(np.ndarray)

    @property
    def is_finite(self):
        """Check if parameter values are all finite
        """
        return np.isfinite(self._data).all()


# autograd needs to consider Parameter a class that in can compute gradients for
# in that regard, it behaves like an ordinary ndarray
ArrayBox.register(Parameter)
VSpace.register(Parameter, vspace_maker=VSpace.mappings[np.ndarray])


def relative_step(X, it, factor=0.1, axis=None):
    """Step size set at `factor` times the mean of `X` in direction `axis`
    """
    return factor * X.mean(axis=axis)
Exemplo n.º 3
0
import types

import dataclasses

from autograd.extend import primitive
from autograd.numpy.numpy_boxes import ArrayBox
from autograd.tracer import Box, isbox, new_box, Node, trace_stack
import networkx as nx
import numpy as np

import whynot as wn

# Allow tracing to use ints and bools (since we only care about the forward
# pass and not derivatives).
for type_ in [bool, np.bool, np.bool_, int, np.int32, np.int64]:
    ArrayBox.register(type_)


class FunctionBox(Box):
    """Generic box class to encapsulate functions and record their invocation."""
    @primitive
    def __call__(func, *args, **kwargs):
        """Execute function and record its invocation in the graph."""
        return func(*args, **kwargs)


FunctionBox.register(types.FunctionType)


class TracerNode(Node):
    """Base case for constructing causal graphs via computation graphs.