Exemplo n.º 1
0
    def _value(self, obs: Tuple[str]):
        obs = convert_to_container(value=obs, container=tuple)
        values = self.get_iteration()
        # TODO(Mayou36): add conversion to right dimension? (n_events, n_obs)? # check if 1-D?
        if len(values.shape.as_list()) == 0:
            values = tf.expand_dims(values, -1)
        if len(values.shape.as_list()) == 1:
            values = tf.expand_dims(values, -1)
        perm_indices = self.space.axes if self.space.axes != tuple(
            range(values.shape[-1])) else False

        # permutate = perm_indices is not None
        if obs:
            if not frozenset(obs) <= frozenset(self.obs):
                raise ValueError(
                    "The observable(s) {} are not contained in the dataset. "
                    "Only the following are: {}".format(
                        frozenset(obs) - frozenset(self.obs), self.obs))
            perm_indices = self.space.get_axes(obs=obs)
            # values = list(values[self.obs.index(o)] for o in obs if o in self.obs)
        if perm_indices:
            values = ztf.unstack_x(values)
            values = list(values[i] for i in perm_indices)
            values = ztf.stack_x(values)

        # cast data to right type
        if not values.dtype == self.dtype:
            values = tf.cast(values, dtype=self.dtype)
        return values
Exemplo n.º 2
0
def func4_3deps(x):
    if isinstance(x, np.ndarray):
        a, b, c = x
    else:
        a, b, c = ztf.unstack_x(x)

    return a**2 + b**3 + 0.5 * c
Exemplo n.º 3
0
    def unstack_x(self, obs: ztyping.ObsTypeInput = None):
        """Return the unstacked data: a list of tensors.

        Args:
            obs (): which observables to return

        Returns:
            List(tf.Tensor)
        """
        return ztf.unstack_x(self._value_internal(obs=obs))
Exemplo n.º 4
0
    def gradients(self, x: ztyping.XType, norm_range: ztyping.LimitsType, params: ztyping.ParamsTypeOpt = None):
        warnings.warn("Taking the gradient *this way* in TensorFlow is inefficient! Consider taking it with"
                      "respect to the loss function.")
        if params is not None:
            params = convert_to_container(params)
        if params is None or isinstance(params[0], str):
            params = self.get_params(only_floating=False, names=params)

        probs = self.pdf(x, norm_range=norm_range)
        gradients = [tf.gradients(prob, params) for prob in ztf.unstack_x(probs)]
        return tf.stack(gradients)
Exemplo n.º 5
0
    def unstack_x(self,
                  obs: ztyping.ObsTypeInput = None,
                  always_list: bool = False):
        """Return the unstacked data: a list of tensors or a single Tensor.

        Args:
            obs (): which observables to return
            always_list (bool): If True, always return a list (also if length 1)

        Returns:
            List(tf.Tensor)
        """
        return ztf.unstack_x(self._value_internal(obs=obs))
Exemplo n.º 6
0
    def _sort_value(self, value, obs: Tuple[str]):
        obs = convert_to_container(value=obs, container=tuple)
        perm_indices = self.space.axes if self.space.axes != tuple(
            range(value.shape[-1])) else False

        # permutate = perm_indices is not None
        if obs:
            if not frozenset(obs) <= frozenset(self.obs):
                raise ValueError(
                    "The observable(s) {} are not contained in the dataset. "
                    "Only the following are: {}".format(
                        frozenset(obs) - frozenset(self.obs), self.obs))
            perm_indices = self.space.get_axes(obs=obs)
            # values = list(values[self.obs.index(o)] for o in obs if o in self.obs)
        if perm_indices:
            value = ztf.unstack_x(value)
            value = list(value[i] for i in perm_indices)
            value = ztf.stack_x(value)

        return value
Exemplo n.º 7
0
 def _func(self, x):
     mu = self.params['mu']
     sigma = self.params['sigma']
     x = ztf.unstack_x(x)
     return ztf.exp(-ztf.square((x - mu) / sigma))
Exemplo n.º 8
0
 def _unnormalized_pdf(self, x):
     mu = self.params['mu']
     sigma = self.params['sigma']
     x = ztf.unstack_x(x)
     return ztf.exp(-ztf.square((x - mu) / sigma))
Exemplo n.º 9
0
 def func2_pure(self, x):
     x = ztf.unstack_x(x)
     return param2 * x + param3
Exemplo n.º 10
0
 def func1_pure(self, x):
     x = ztf.unstack_x(x)
     return param1 * x
Exemplo n.º 11
0
def func1_5deps(x):
    a, b, c, d, e = ztf.unstack_x(x)
    return a + b * c**2 + d**2 * e**3
Exemplo n.º 12
0
def func3_2deps(x):
    a, b = ztf.unstack_x(x)
    return a**2 + b**2