Exemplo n.º 1
0
    def test_works_with_registered(self):
        class CustomClass(object):
            def value(self):
                return tf.convert_to_tensor(42.)

        tf.register_tensor_conversion_function(
            CustomClass, lambda value, **_: value.value())

        tf_utils.register_symbolic_tensor_type(CustomClass)

        if tf.executing_eagerly():
            self.assertFalse(
                tf_utils.is_symbolic_tensor(
                    tf.Variable(name='blah', initial_value=0.)))
            self.assertFalse(
                tf_utils.is_symbolic_tensor(tf.convert_to_tensor(0.)))
            self.assertFalse(
                tf_utils.is_symbolic_tensor(
                    tf.SparseTensor(indices=[[0, 0], [1, 2]],
                                    values=[1, 2],
                                    dense_shape=[3, 4])))
            self.assertFalse(tf_utils.is_symbolic_tensor(CustomClass()))
        else:
            self.assertTrue(
                tf_utils.is_symbolic_tensor(
                    tf.Variable(name='blah', initial_value=0.)))
            self.assertTrue(
                tf_utils.is_symbolic_tensor(tf.convert_to_tensor(0.)))
            self.assertTrue(
                tf_utils.is_symbolic_tensor(
                    tf.SparseTensor(indices=[[0, 0], [1, 2]],
                                    values=[1, 2],
                                    dense_shape=[3, 4])))
            self.assertTrue(tf_utils.is_symbolic_tensor(CustomClass()))
Exemplo n.º 2
0
def register_tensor_wrapper_class(cls):
    """
    Register a sub-class of :class:`TensorWrapper` into TensorFlow type system.

    Args:
        cls: The subclass of :class:`TensorWrapper` to be registered.
    """
    if not isinstance(cls, six.class_types) or \
            not issubclass(cls, TensorWrapper):
        raise TypeError('`{}` is not a type, or not a subclass of '
                        '`TensorWrapper`'.format(cls))

    def to_tensor(value, dtype=None, name=None, as_ref=False):
        if dtype and not dtype.is_compatible_with(value.dtype):
            raise ValueError('Incompatible type conversion requested to type '
                             '{} for tensor of type {}'.format(
                                 dtype.name, value.dtype.name))
        if as_ref:  # pragma: no cover
            raise ValueError('{!r}: Ref type not supported'.format(value))
        return value.tensor

    tf.register_tensor_conversion_function(cls, to_tensor)

    # bring support for session.run(StochasticTensor), and for using as keys
    # in feed_dict.
    register_session_run_conversion_functions(
        cls,
        fetch_function=lambda t: ([t.tensor], lambda val: val[0]),
        feed_function=lambda t, v: [(t.tensor, v)],
        feed_function_for_partial_run=lambda t: [t.tensor])
Exemplo n.º 3
0
def register_tensor_wrapper_class(cls):
    """Register the specified TensorWrapper type into TensorFlow type system.

    Parameters
    ----------
    cls : type
        A subclass of `TensorWrapper`, which is to be registered.
    """
    if not isinstance(cls, six.class_types) or \
            not issubclass(cls, TensorWrapper):
        raise TypeError('`%s` is not a type, or not a subclass of '
                        '`TensorWrapper`.' % (cls, ))

    def to_tensor(value, dtype=None, name=None, as_ref=False):
        if dtype and not dtype.is_compatible_with(value.dtype):
            raise ValueError('Incompatible type conversion requested to type '
                             '%s for tensor of type %s' %
                             (dtype.name, value.dtype.name))
        if as_ref:
            raise ValueError('%r: Ref type not supported.' % value)
        return value.__wrapped__

    tf.register_tensor_conversion_function(cls, to_tensor)

    # bring support for session.run(StochasticTensor), and for using as keys
    # in feed_dict.
    register_session_run_conversion_functions(
        cls,
        fetch_function=lambda t:
        ([getattr(t, '__wrapped__')], lambda val: val[0]),
        feed_function=lambda t, v: [(getattr(t, '__wrapped__'), v)],
        feed_function_for_partial_run=lambda t: [getattr(t, '__wrapped__')])
 def __new__(mcs, name, bases, attrs):
   # Embed Tensor-like attributes into the mcs class.
   attrs.update(_get_tensor_like_attributes())
   attrs['_value'] = _value
   cls = super(_TensorCoercibleMeta, mcs).__new__(mcs, name, bases, attrs)
   def _tensorize(d, dtype=None, name=None, as_ref=False):
     return d._value(dtype, name, as_ref)  # pylint: disable=protected-access
   tf.register_tensor_conversion_function(cls, conversion_func=_tensorize)
   return cls
Exemplo n.º 5
0
 def testFullDelegationControlUsingRegistry(self):
   class NumpyArraySubclass(np.ndarray):
     def __radd__(self, lhs):
       return "Works!"
   def raise_to_delegate(value, dtype=None, name=None, as_ref=False):
     raise TypeError
   register_tensor_conversion_function(NumpyArraySubclass, raise_to_delegate,
                                       priority=0)
   tensor = ops.convert_to_tensor([[10.0, 20.0]])
   rhs = NumpyArraySubclass(shape=(1,2), buffer=np.array([1.0, 2.0]))
   res = tensor + rhs
   self.assertEqual(res, "Works!")
Exemplo n.º 6
0
    def testFullDelegationControlUsingRegistry(self):
        class NumpyArraySubclass(np.ndarray):
            def __radd__(self, lhs):
                return "Works!"

        def raise_to_delegate(value, dtype=None, name=None, as_ref=False):
            raise TypeError

        register_tensor_conversion_function(NumpyArraySubclass,
                                            raise_to_delegate,
                                            priority=0)
        tensor = ops.convert_to_tensor([[10.0, 20.0]])
        rhs = NumpyArraySubclass(shape=(1, 2), buffer=np.array([1.0, 2.0]))
        res = tensor + rhs
        self.assertEqual(res, "Works!")
Exemplo n.º 7
0
        functools.update_wrapper(_run_op, tensor_oper)
        setattr(cls, operator, _run_op)

    # NOTE(mrry): This enables the Variable's overloaded "right" binary
    # operators to run when the left operand is an ndarray, because it
    # accords the Variable class higher priority than an ndarray, or a
    # numpy matrix.
    # TODO(mrry): Convert this to using numpy's __numpy_ufunc__
    # mechanism, which allows more control over how Variables interact
    # with ndarrays.
    __array_priority__ = 100


# Parameter._OverloadAllOperators()
tf.register_tensor_conversion_function(Parameter,
                                       lambda x, *args, **kwds: x.value)

# def _cast_to_dtype(
#     value: TensorData, dtype: Optional[DType] = None
# ) -> Union[tf.Tensor, tf.Variable]:
#     if dtype is None:
#         dtype = default_float()

#     if tf.is_tensor(value):
#         # NOTE(awav) TF2.2 resolves issue with cast.
#         # From TF2.2, `tf.cast` can be used alone instead of this auxiliary function.
#         # workaround for https://github.com/tensorflow/tensorflow/issues/35938
#         return tf.cast(value, dtype)
#     else:
#         return tf.convert_to_tensor(value, dtype=dtype)
Exemplo n.º 8
0
def train_and_evaluate(hparams, model_dir, train_source_files,
                       train_target_files, eval_source_files,
                       eval_target_files, use_multi_gpu):

    if hparams.half_precision:

        def conversion_func(value, dtype=None, name=None, as_ref=False):
            if dtype and dtype == tf.float16 and not dtype.is_compatible_with(
                    value.dtype):
                tf.logging.info(
                    f"Automatic tensor conversion into {dtype.name} is performed for {str(value)}"
                )
                return tf.cast(value, dtype=dtype)
            else:
                return NotImplemented

        tf.register_tensor_conversion_function((tf.Variable, tf.Tensor),
                                               conversion_func,
                                               priority=-1)
        backend.set_epsilon(1e-4)

    interleave_parallelism = get_parallelism(
        hparams.interleave_cycle_length_cpu_factor,
        hparams.interleave_cycle_length_min,
        hparams.interleave_cycle_length_max)

    tf.logging.info("Interleave parallelism is %d.", interleave_parallelism)

    def train_input_fn():
        source_and_target_files = list(
            zip(train_source_files, train_target_files))
        shuffle(source_and_target_files)
        source = [s for s, _ in source_and_target_files]
        target = [t for _, t in source_and_target_files]

        dataset = create_from_tfrecord_files(
            source,
            target,
            hparams,
            cycle_length=interleave_parallelism,
            buffer_output_elements=hparams.interleave_buffer_output_elements,
            prefetch_input_elements=hparams.interleave_prefetch_input_elements)

        zipped = dataset.prepare_and_zip()
        batched = zipped.filter_by_max_output_length().repeat(
            count=None).shuffle(
                hparams.suffle_buffer_size).group_by_batch().prefetch(
                    hparams.prefetch_buffer_size)
        batched = batched.half_precision(
        ) if hparams.half_precision else batched
        return batched.dataset

    def eval_input_fn():
        source_and_target_files = list(
            zip(eval_source_files, eval_target_files))
        shuffle(source_and_target_files)
        source = tf.data.TFRecordDataset(
            [s for s, _ in source_and_target_files])
        target = tf.data.TFRecordDataset(
            [t for _, t in source_and_target_files])

        dataset = dataset_factory(source, target, hparams)
        zipped = dataset.prepare_and_zip()
        batched = zipped.filter_by_max_output_length().repeat().group_by_batch(
            batch_size=1)
        dataset = batched.half_precision(
        ) if hparams.half_precision else batched
        return dataset.dataset

    distribution = tf.contrib.distribute.MirroredStrategy(
    ) if use_multi_gpu else None

    run_config = tf.estimator.RunConfig(
        save_summary_steps=hparams.save_summary_steps,
        save_checkpoints_steps=hparams.save_checkpoints_steps,
        keep_checkpoint_max=hparams.keep_checkpoint_max,
        log_step_count_steps=hparams.log_step_count_steps,
        train_distribute=distribution)

    ws = tf.estimator.WarmStartSettings(
        ckpt_to_initialize_from=hparams.ckpt_to_initialize_from,
        vars_to_warm_start=hparams.vars_to_warm_start
    ) if hparams.warm_start else None

    estimator = model_factory(hparams, model_dir, run_config, ws)

    train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn)
    eval_spec = tf.estimator.EvalSpec(
        input_fn=eval_input_fn,
        steps=hparams.num_evaluation_steps,
        throttle_secs=hparams.eval_throttle_secs,
        start_delay_secs=hparams.eval_start_delay_secs)

    tf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)
Exemplo n.º 9
0
        return operator.__floordiv__(self._tensor, value)

    def __pow__(self, value):
        return operator.__pow__(self._tensor, value)

    def __hash__(self):
        raise TypeError(f'TensorVariable {self!r} is unhashable. Instead, use '
                        'tensorvariable.ref() as the key.')

    def __repr__(self) -> str:
        return f'<TensorVariable: {self._tensor}>'

    def __array__(self):
        return np.array(self._tensor)


def create_tensor_variable(next_creator_fn, **kwargs):
    del next_creator_fn  # Unused.
    initial_value = kwargs.pop('initial_value')
    return TensorVariable(initial_value, **kwargs)


def _convert_tensor_variable_to_tensor(value, *args, **kwargs):
    del args  # unused
    del kwargs  # unused
    return value.read_value()


tf.register_tensor_conversion_function(TensorVariable,
                                       _convert_tensor_variable_to_tensor)
Exemplo n.º 10
0
            self.assertIs(type(kernel), type(new_kernel))
            if 'store_parameters_in_results' in new_kernel.parameters:
                self.assertTrue(
                    new_kernel.parameters['store_parameters_in_results'])

    def testNoParameters(self):
        kernel = FakeInnerNoParameters()
        new_kernel = util.enable_store_parameters_in_results(kernel)
        self.assertIs(kernel, new_kernel)


class TensorConvertible(object):
    pass


tf.register_tensor_conversion_function(
    TensorConvertible, conversion_func=lambda *args: tf.constant(0))


class SimpleTensorWarningTest(tf.test.TestCase, parameterized.TestCase):

    # We must defer creating the TF objects until the body of the test.
    # pylint: disable=unnecessary-lambda
    @parameterized.parameters([lambda: tf.Variable(0)],
                              [lambda: tf.compat.v2.Variable(0)],
                              [lambda: TensorConvertible()])
    def testWarn(self, tensor_callable):
        tensor = tensor_callable()
        warnings.simplefilter('always')
        with warnings.catch_warnings(record=True) as triggered:
            util.warn_if_parameters_are_not_simple_tensors({'a': tensor})
        self.assertTrue(
Exemplo n.º 11
0
                             "'{}' for variable of type '{}'".format(
                                 dtype.name, value.dtype.name))
        if as_ref:
            raise ValueError("{}: Ref type not supported.".format(value))
        return value.tensor

    def _as_graph_element(self, allow_tensor=True, allow_operation=True):
        # this method brings support to ``session.run(...)`` and other
        # related methods which expects a graph element.
        if not allow_tensor:
            raise RuntimeError('Can not convert a `StochasticTensor` into a '
                               'Operation.')
        return self.tensor


tf.register_tensor_conversion_function(StochasticTensor,
                                       StochasticTensor._to_tensor)


class BayesianNet(Context):
    """
    The :class:`BayesianNet` class is a context class supporting model
    construction in ZhuSuan as Bayesian Networks (Directed graphical models).
    A `BayesianNet` represents a DAG with two kinds of nodes:

    * Deterministic nodes, made up of any tensorflow operations.
    * Stochastic nodes, constructed by :class:`StochasticTensor`.

    To start a :class:`BayesianNet` context::

        import zhusuan as zs
        with zs.BayesianNet() as model:
Exemplo n.º 12
0
    @property
    def shape(self):
        return self.data.shape

    @property
    def dtype(self):
        return self.data.dtype


def fail_on_convert(x, **kwargs):
    _ = x
    _ = kwargs
    raise TypeError('Cannot convert DummyArrayLike to a tensor')


tf.register_tensor_conversion_function(DummyArrayLike, fail_on_convert)


class DataAdapterTestBase(keras_parameterized.TestCase):
    def setUp(self):
        super(DataAdapterTestBase, self).setUp()
        self.batch_size = 5
        self.numpy_input = np.zeros((50, 10))
        self.numpy_target = np.ones(50)
        self.tensor_input = tf.constant(2.0, shape=(50, 10))
        self.tensor_target = tf.ones((50, ))
        self.arraylike_input = DummyArrayLike(self.numpy_input)
        self.arraylike_target = DummyArrayLike(self.numpy_target)
        self.dataset_input = tf.data.Dataset.from_tensor_slices(
            (self.numpy_input,
             self.numpy_target)).shuffle(50).batch(self.batch_size)
Exemplo n.º 13
0

def _session_run_conversion_fetch_function(tensor):
    return ([tensor.value], lambda val: val[0])


def _session_run_conversion_feed_function(feed, feed_val):
    return [(feed.value, feed_val)]


def _session_run_conversion_feed_function_for_partial_run(feed):
    return [feed.value]


def _tensor_conversion_function(v, dtype=None, name=None, as_ref=False):
    del name, as_ref  # unused
    if dtype and not dtype.is_compatible_with(v.dtype):
        raise ValueError(
            "Incompatible type conversion requested to type '%s' for variable "
            "of type '%s'" % (dtype.name, v.dtype.name))
    return v.value


tf_session.register_session_run_conversion_functions(  # enable sess.run, eval
    RandomVariable, _session_run_conversion_fetch_function,
    _session_run_conversion_feed_function,
    _session_run_conversion_feed_function_for_partial_run)

tf.register_tensor_conversion_function(  # enable tf.convert_to_tensor
    RandomVariable, _tensor_conversion_function)
Exemplo n.º 14
0
def _session_run_conversion_fetch_function(tensor):
  return ([tensor.value], lambda val: val[0])


def _session_run_conversion_feed_function(feed, feed_val):
  return [(feed.value, feed_val)]


def _session_run_conversion_feed_function_for_partial_run(feed):
  return [feed.value]


def _tensor_conversion_function(v, dtype=None, name=None, as_ref=False):
  del name, as_ref  # unused
  if dtype and not dtype.is_compatible_with(v.dtype):
    raise ValueError(
        "Incompatible type conversion requested to type '%s' for variable "
        "of type '%s'" % (dtype.name, v.dtype.name))
  return v.value


tf_session.register_session_run_conversion_functions(  # enable sess.run, eval
    RandomVariable,
    _session_run_conversion_fetch_function,
    _session_run_conversion_feed_function,
    _session_run_conversion_feed_function_for_partial_run)

tf.register_tensor_conversion_function(  # enable tf.convert_to_tensor
    RandomVariable, _tensor_conversion_function)
Exemplo n.º 15
0
def tensor_to_ndarray(tensor):
  return ndarray(tensor._shape_tuple(), dtype=tensor.dtype, buffer=tensor)  # pylint: disable=protected-access


def ndarray_to_tensor(arr, dtype=None, name=None, as_ref=False):
  if as_ref:
    raise ValueError('as_ref is not supported.')
  if dtype and tf.as_dtype(arr.dtype) != dtype:
    return tf.cast(arr.data, dtype)
  result_t = arr.data
  if name:
    result_t = tf.identity(result_t, name=name)
  return result_t


tf.register_tensor_conversion_function(ndarray, ndarray_to_tensor)


# Don't use a namedtuple since nest considers that a tuple and unflattens and
# flattens it.
class ShardedNdArray(object):
  """Wrapper over ndarray that can contain tensors on multiple devices.

    This is returned by extensions.pmap, and contains the individual tensors on
    different devices.
  """

  def __init__(self, tensors):
    """Initializes the ShardedNdArray.

    Note that the tensors should be ordered in the way the pmap producing these
Exemplo n.º 16
0
    '''Loss that punishes values being larger than a given value.

    .. math::
        loss = func(x - reference)
    '''
    def loss_eval(self, x, reference, mask):
        return self._func(x - reference)

    def __init__(self,
                 x,
                 reference,
                 mask=None,
                 func=tf.nn.softplus,
                 name='GreaterThanLoss'):
        super(GreaterThan, self).__init__(x=x,
                                          reference=reference,
                                          func=func,
                                          name=name)


def convert_loss_to_tensor(value, dtype=None, name=None, as_ref=False):
    if dtype is None:
        return value.value
    else:
        return tf.convert_to_tensor(value.value, dtype=dtype, name=name)


tf.register_tensor_conversion_function((Loss, EmpiricalLossWrapper),
                                       conversion_func=convert_loss_to_tensor,
                                       priority=100)
Exemplo n.º 17
0
        Return the Tensor representing the value of the variational objective.

        :return: A Tensor.
        """
        if not hasattr(self, '_tensor'):
            self._tensor = self._objective()
        return self._tensor

    @staticmethod
    def _to_tensor(value, dtype=None, name=None, as_ref=False):
        tensor = value.tensor
        if dtype and not dtype.is_compatible_with(tensor.dtype):
            raise ValueError("Incompatible type conversion requested to type "
                             "'{}' for variable of type '{}'".format(
                                 dtype.name, tensor.dtype.name))
        if as_ref:
            raise ValueError("{}: Ref type not supported.".format(value))
        return tensor


tf.register_tensor_conversion_function(VariationalObjective,
                                       VariationalObjective._to_tensor)

# bring support for session.run(VariationalObjective), and for using as keys
# in feed_dict.
register_session_run_conversion_functions(
    VariationalObjective,
    fetch_function=lambda t: ([t.tensor], lambda val: val[0]),
    feed_function=lambda t, v: [(t.tensor, v)],
    feed_function_for_partial_run=lambda t: [t.tensor])
Exemplo n.º 18
0
        # register the variable, which is used to detect dependencies
        contextmanager.randvar_registry.register_parameter(self)
        contextmanager.randvar_registry.update_graph()


def _tensor_conversion_function(p, dtype=None, name=None, as_ref=False):
    """
        Function that converts the inferpy variable into a Tensor.
        This will enable the use of enable tf.convert_to_tensor(rv)

        If the variable needs to be broadcast_to, do it right now
    """
    return tf.convert_to_tensor(p.var)


# register the conversion function into a tensor
tf.register_tensor_conversion_function(  # enable tf.convert_to_tensor
    Parameter, _tensor_conversion_function)


def _session_run_conversion_fetch_function(p):
    """
        This will enable run and operations with other tensors
    """
    return ([tf.convert_to_tensor(p)], lambda val: val[0])


tf_session.register_session_run_conversion_functions(  # enable sess.run, eval
    Parameter, _session_run_conversion_fetch_function)
Exemplo n.º 19
0
    def __getitem__(self, key):
        return self.as_tensor()[key]

    def as_tensor(self, dtype=None, name=None, as_ref=False):
        with tf.name_scope(name, "PartitionedTensor.as_tensor", self.tensors):
            assert not as_ref
            assert dtype in [None, self.dtype]
            result = tf.concat(self.tensors, axis=0)

            # Cache 'result' if we haven't already cached a value for this device.
            if result.device not in self._concats:
                self._concats[result.device] = result
            return self._concats[result.device]

    @property
    def device(self):
        # PartitionedTensors in general do not live on a single device.  If the
        # device cannot be determined unambiguously this property will return None.
        device = self.tensors[0].device
        if all(tensor.device == device for tensor in self.tensors):
            return device
        return None


tf.register_tensor_conversion_function(
    PartitionedTensor,
    lambda val, dtype, name, as_ref: val.as_tensor(dtype, name, as_ref))

# TODO(b/69623235): Add a function for finding tensors that share gradients
# to eliminate redundant fisher factor computations.
Exemplo n.º 20
0
    @property
    def dtype(self):
        return self.value.dtype


def _to_tensor(value, dtype=None, name=None, as_ref=False):
    if dtype and not dtype.is_compatible_with(value.dtype):
        raise ValueError('Incompatible type conversion requested to type '
                         '%s for variable of type %s' %
                         (dtype.name, value.dtype.name))
    if as_ref:
        raise ValueError('%r: Ref type not supported.' % value)
    return value.value


tf.register_tensor_conversion_function(_SimpleTensor, _to_tensor)


class ArithMixinTestCase(tf.test.TestCase):

    def test_prerequisite(self):
        if six.PY2:
            self.assertAlmostEqual(regular_div(3, 2), 1)
            self.assertAlmostEqual(regular_div(3.3, 1.6), 2.0625)
        else:
            self.assertAlmostEqual(regular_div(3, 2), 1.5)
            self.assertAlmostEqual(regular_div(3.3, 1.6), 2.0625)
        self.assertAlmostEqual(true_div(3, 2), 1.5)
        self.assertAlmostEqual(true_div(3.3, 1.6), 2.0625)
        self.assertAlmostEqual(floor_div(3, 2), 1)
        self.assertAlmostEqual(floor_div(3.3, 1.6), 2.0)
Exemplo n.º 21
0
# -*- coding:utf8 -*-
# File   : __init__.py
# Author : Jiayuan Mao
# Email  : [email protected]
# Date   : 12/29/16
#
# This file is part of TensorArtist.

from .tfutils import TArtGraphKeys
from .graph import *
from . import opr, optimizer, summary, train

import tensorflow as tf


def varnode_to_tftensor(varnode, *args, **kwargs):
    v = varnode.impl
    if isinstance(v, tf.Variable):
        return tf.convert_to_tensor(v)
    return v


tf.register_tensor_conversion_function(VarNode, varnode_to_tftensor)
Exemplo n.º 22
0
from tensorflow.python.framework import test_util
from tensorflow_estimator.python.estimator.export import export


class LabeledTensorMock(object):
  """Mock class emulating LabeledTensor."""

  def __init__(self):
    self.tensor = tf.constant([1])


def _convert_labeled_tensor_mock_to_tensor(value, *args, **kwargs):
  return ops.internal_convert_to_tensor(value.tensor, *args, **kwargs)


tf.register_tensor_conversion_function(LabeledTensorMock,
                                       _convert_labeled_tensor_mock_to_tensor)


class ServingInputReceiverTest(tf.test.TestCase):

  def test_serving_input_receiver_constructor(self):
    """Tests that no errors are raised when input is expected."""
    features = {
        "feature0": tf.constant([0]),
        u"feature1": tf.constant([1]),
        "feature2": tf.sparse.SparseTensor(
            indices=[[0, 0]], values=[1], dense_shape=[1, 1]),
        # ints are allowed only in the `features` dict
        42: tf.constant([3]),
    }
    receiver_tensors = {
Exemplo n.º 23
0
    def __init__(self, value):
        self._value = value

    def to_tensor(self, dtype=None, name=None, as_ref=False):
        return tf.convert_to_tensor(self._value, dtype=dtype, name=name)

    @property
    def value(self):
        return self._value

    @value.setter
    def value(self, value):
        self._value = value


tf.register_tensor_conversion_function(TensorRef,
                                       conversion_func=TensorRef.to_tensor)


class AdaptStepResults(
        collections.namedtuple(
            "AdaptStepResults",
            "inner_results, step_size, num_leapfrog_steps, cur_step")):
    __slots__ = ()


class AdaptStep(tfp.mcmc.TransitionKernel):
    def __init__(self, inner_kernel, step_size, num_leapfrog_steps,
                 trajectory_length, target_accept_rate, gain, num_adapt_steps):
        self.inner_kernel = inner_kernel
        self.step_size = step_size
        self.num_leapfrog_steps = num_leapfrog_steps
Exemplo n.º 24
0
    def test_enables_nontensor_plumbing(self):
        if tf.executing_eagerly():
            self.skipTest('`compile` functionality changed.')
        # Setup.

        class Foo(object):
            def __init__(self, input_):
                self._input = input_
                self.value = tf.convert_to_tensor([[42.]])

            @property
            def dtype(self):
                return self.value.dtype

        tf.register_tensor_conversion_function(
            Foo, lambda x, *args, **kwargs: x.value)
        tf_utils.register_symbolic_tensor_type(Foo)

        class PlumbingLayer(keras.layers.Lambda):
            def __init__(self, fn, **kwargs):
                def _fn(*fargs, **fkwargs):
                    d = fn(*fargs, **fkwargs)
                    x = tf.convert_to_tensor(d)
                    d.shape = x.shape
                    d.get_shape = x.get_shape
                    return d, x

                super(PlumbingLayer, self).__init__(_fn, **kwargs)
                self._enter_dunder_call = False

            def __call__(self, inputs, *args, **kwargs):
                self._enter_dunder_call = True
                d, _ = super(PlumbingLayer,
                             self).__call__(inputs, *args, **kwargs)
                self._enter_dunder_call = False
                return d

            def call(self, inputs, *args, **kwargs):
                d, v = super(PlumbingLayer, self).call(inputs, *args, **kwargs)
                if self._enter_dunder_call:
                    return d, v
                return d

        # User-land.
        model = keras.Sequential([
            keras.layers.InputLayer((1, )),
            PlumbingLayer(Foo),  # Makes a `Foo` object.
        ])
        # Let's ensure Keras graph history is preserved by composing the models.
        model = keras.Model(model.inputs, model(model.outputs))
        # Now we instantiate the model and verify we have a `Foo` object, not a
        # `Tensor`.
        y = model(tf.convert_to_tensor([[7.]]))
        self.assertIsInstance(y, Foo)
        # Confirm that (custom) loss sees `Foo` instance, not Tensor.
        obtained_prediction_box = [None]

        def custom_loss(y_obs, y_pred):
            del y_obs
            obtained_prediction_box[0] = y_pred
            return y_pred

        # Apparently `compile` calls the loss function enough to trigger the
        # side-effect.
        model.compile('SGD', loss=custom_loss)
        self.assertIsInstance(obtained_prediction_box[0], Foo)
Exemplo n.º 25
0
    """
        if in_layers is None:
            in_layers = self.in_layers
        if not isinstance(in_layers, Sequence):
            in_layers = [in_layers]
        tensors = []
        for input in in_layers:
            tensors.append(tf.convert_to_tensor(input))
        return tensors


def _convert_layer_to_tensor(value, dtype=None, name=None, as_ref=False):
    return tf.convert_to_tensor(value.out_tensor, dtype=dtype, name=name)


tf.register_tensor_conversion_function(Layer, _convert_layer_to_tensor)


class Dense(Layer):
    def __init__(
            self,
            out_channels,
            activation_fn=None,
            biases_initializer=tf.zeros_initializer,
            weights_initializer=tf.contrib.layers.variance_scaling_initializer,
            **kwargs):
        """Create a dense layer.

    The weight and bias initializers are specified by callable objects that construct
    and return a Tensorflow initializer when invoked with no arguments.  This will typically
    be either the initializer class itself (if the constructor does not require arguments),
Exemplo n.º 26
0
"""PyMC4."""
from ._model import *
from .random_variables import *

import tensorflow as _tf
from ._template_contexts import get_context as _get_context
from .random_variables import RandomVariable as _RandomVariable

__version__ = "0.0.1"


def _convert_rv_to_backend(d, dtype=None, name=None, as_ref=False):
    if isinstance(d, _RandomVariable):
        return d.as_tensor()
    return d


_tf.register_tensor_conversion_function(
    _RandomVariable, conversion_func=_convert_rv_to_backend, priority=0
)
Exemplo n.º 27
0
            raise ValueError(error_msg.format(x.shape))

    @staticmethod
    def validate_type(x):
        """Raise a type error if the dtype of x is not float."""
        if not x.dtype.is_floating:
            raise TypeError("Quaternion: dtype must be one of float16/32/64.")

    @scope_wrapper
    def norm(self, keepdims=True):
        """Return the norm of the quaternion."""
        return tf.reduce_sum(tf.square(self._q), axis=-1)

    @scope_wrapper
    def abs(self, keepdims=True):
        """Return the square root of the norm of the quaternion."""
        return tf.sqrt(self.norm(keepdims))


# ____________________________________________________________________________
#                          quaternion to tensor conversion
def quaternion_to_tensor(x, dtype=None, name=None, as_ref=None):
    """Convert a Quaternion to a `tf.Tensor`."""
    # Todo(phil): handle as_ref correctly
    return tf.convert_to_tensor(x.value(), dtype, name)


tf.register_tensor_conversion_function(Quaternion,
                                       quaternion_to_tensor,
                                       priority=100)
Exemplo n.º 28
0
    @_handle_name.setter
    def _handle_name(self, handle_name):
        self.latent_variable._handle_name = handle_name

    @property
    def _initializer_op(self):
        return self.latent_variable._initializer_op

    @_initializer_op.setter
    def _initializer_op(self, initializer_op):
        self.latent_variable._initializer_op = initializer_op

    def _as_graph_element(self):
        if self.quantizer and context.should_quantize():
            return self.quantizer(self.latent_variable)
        graph_element = self.latent_variable._as_graph_element()
        if graph_element is None:
            return self._op
        return graph_element


QuantizedVariable._OverloadAllOperators()
tf.register_tensor_conversion_function(
    QuantizedVariable, QuantizedVariable._dense_var_to_tensor
)
try:
    ops.register_dense_tensor_like_type(QuantizedVariable)
except AttributeError:
    pass
Exemplo n.º 29
0
""" Neural ordinary differential equations module. """
from types import FunctionType
import tensorflow as tf

tf.register_tensor_conversion_function(
    FunctionType, lambda value, *args, **kwargs: tf.zeros(0))
tf.register_tensor_conversion_function(
    tf.keras.Model, lambda value, *args, **kwargs: tf.zeros(0))


def _flatten(tensors):
    """ Flattens given tensor or a collection of tensors. """
    if not isinstance(tensors, (list, tuple)):  # given single tensor
        return tf.reshape(tensors, (-1, ))
    return (tf.concat([tf.reshape(t, (-1, )) for t in tensors], 0)
            if tensors else tf.convert_to_tensor([]))


def _unflatten(tensor, shapes):
    """ Splits given tensor and reshapes results using given shapes. """
    split = tf.split(tensor, [tf.reduce_prod(s) for s in shapes])
    return [tf.reshape(t, s) for t, s in zip(split, shapes)]


def odeint_grad(grad_output,
                func,
                yt,
                t,
                variables=None,
                rtol=1e-6,
                atol=1e-12):
Exemplo n.º 30
0
            return self.read_value().__matmul__(o)
        except AttributeError:
            # See https://docs.python.org/3/library/constants.html#NotImplemented
            return NotImplemented

    def __rmatmul__(self, o):
        try:
            return self.read_value().__rmatmul__(o)
        except AttributeError:
            # See https://docs.python.org/3/library/constants.html#NotImplemented
            return NotImplemented

    # pylint: enable=multiple-statements


tf.register_tensor_conversion_function(AutoCastVariable,
                                       AutoCastVariable._dense_var_to_tensor)  # pylint:disable=protected-access


def create_autocast_variable(variable, op=None):
    """Creates an AutoCastVariable that wraps another variable.

  This typically just returns `AutoCastVariable(variable)`. But, if the variable
  is a DistributedVariable or one of its subclasses, we instead dynamically
  create a class that subclasses from both AutoCastVariable and
  variable.__class__. This is so the returned variable will still pass
  `isinstance(variable, variable.__class__)`, which is required for
  DistributedVariables and its subclasses to work properly.

  Args:
    variable: A floating-point resource variable to wrap.
    op: Optional operation of this variable.
Exemplo n.º 31
0
    def set_weights(self, weights):
        if len(weights) != len(self.variables):
            raise ValueError(
                f"set_weights() expects a list of {len(self.variables)} arrays, "
                f"received {len(weights)}.")
        tf.keras.backend.batch_set_value(zip(self.variables, weights))


def _parameter_conversion_func(value, dtype=None, name=None, as_ref=False):
    if as_ref:
        raise ValueError("as_ref=True is not supported.")
    return tf.convert_to_tensor(value(), dtype=dtype, name=name)


tf.register_tensor_conversion_function(
    Parameter,
    _parameter_conversion_func,
)


@tf.keras.utils.register_keras_serializable(package="tensorflow_compression")
class RDFTParameter(Parameter):
    """RDFT reparameterization of a convolution kernel.

  This uses the real-input discrete Fourier transform (RDFT) of a kernel as
  its parameterization. The inverse RDFT is applied to the variable to produce
  the kernel.

  (see https://en.wikipedia.org/wiki/Discrete_Fourier_transform)

  Attributes:
    dc: Boolean. The `dc` parameter provided on initialization.
Exemplo n.º 32
0
    return ([tensor.value()], lambda val: val[0])

  @staticmethod
  def _session_run_conversion_feed_function(feed, feed_val):
    return [(feed.value(), feed_val)]

  @staticmethod
  def _session_run_conversion_feed_function_for_partial_run(feed):
    return [feed.value()]

  @staticmethod
  def _tensor_conversion_function(v, dtype=None, name=None, as_ref=False):
    _ = name
    if dtype and not dtype.is_compatible_with(v.dtype):
      raise ValueError(
          "Incompatible type conversion requested to type '%s' for variable "
          "of type '%s'" % (dtype.name, v.dtype.name))
    if as_ref:
      raise ValueError("%s: Ref type is not supported." % v)
    return v.value()


register_session_run_conversion_functions(
    RandomVariable,
    RandomVariable._session_run_conversion_fetch_function,
    RandomVariable._session_run_conversion_feed_function,
    RandomVariable._session_run_conversion_feed_function_for_partial_run)

tf.register_tensor_conversion_function(
    RandomVariable, RandomVariable._tensor_conversion_function)
Exemplo n.º 33
0
    return ([tensor.value()], lambda val: val[0])

  @staticmethod
  def _session_run_conversion_feed_function(feed, feed_val):
    return [(feed.value(), feed_val)]

  @staticmethod
  def _session_run_conversion_feed_function_for_partial_run(feed):
    return [feed.value()]

  @staticmethod
  def _tensor_conversion_function(v, dtype=None, name=None, as_ref=False):
    _ = name, as_ref
    if dtype and not dtype.is_compatible_with(v.dtype):
      raise ValueError(
          "Incompatible type conversion requested to type '%s' for variable "
          "of type '%s'" % (dtype.name, v.dtype.name))
    return v.value()


RandomVariable._overload_all_operators()

register_session_run_conversion_functions(
    RandomVariable,
    RandomVariable._session_run_conversion_fetch_function,
    RandomVariable._session_run_conversion_feed_function,
    RandomVariable._session_run_conversion_feed_function_for_partial_run)

tf.register_tensor_conversion_function(
    RandomVariable, RandomVariable._tensor_conversion_function)