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
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
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))
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)
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))
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
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))
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))
def func2_pure(self, x): x = ztf.unstack_x(x) return param2 * x + param3
def func1_pure(self, x): x = ztf.unstack_x(x) return param1 * x
def func1_5deps(x): a, b, c, d, e = ztf.unstack_x(x) return a + b * c**2 + d**2 * e**3
def func3_2deps(x): a, b = ztf.unstack_x(x) return a**2 + b**2