lbn.py 29.4 KB
Newer Older
Marcel Rieger's avatar
Marcel Rieger committed
1 2 3
# coding: utf-8

"""
Marcel Rieger's avatar
Marcel Rieger committed
4
TensorFlow implementation of the Lorentz Boost Network (LBN).
Marcel Rieger's avatar
Marcel Rieger committed
5
For more information, see https://arxiv.org/abs/1812.09722.
Marcel Rieger's avatar
Marcel Rieger committed
6 7 8 9 10 11 12 13 14
"""


__author__ = "Marcel Rieger"
__copyright__ = "Copyright 2018-2019, Marcel Rieger"
__license__ = "BSD"
__credits__ = ["Martin Erdmann", "Erik Geiser", "Yannik Rath", "Marcel Rieger"]
__contact__ = "https://git.rwth-aachen.de/3pia/lbn"
__email__ = "marcel.rieger@cern.ch"
Marcel Rieger's avatar
Marcel Rieger committed
15
__version__ = "1.0.0"
Marcel Rieger's avatar
Marcel Rieger committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

__all__ = ["LBN", "FeatureFactoryBase", "FeatureFactory"]


import functools

import numpy as np
import tensorflow as tf


class LBN(object):
    """
    Lorentz Boost Network (LBN) class.

    Usage example:

    .. code-block:: python

        # initialize the LBN
        lbn = LBN(10, boost_mode=LBN.PAIRS)

        # create a feature tensor based on input four-vectors
        features = lbn(four_vectors)

        # use the features as input for a subsequent, application-specific network
        ...

    *n_particles* and *n_restframes* are the number of particle and rest-frame combinations to
    build. Their interpretation depends on the *boost_mode*. *n_restframes* is only used for the
    *PRODUCT* mode. It is inferred from *n_particles* for *PAIRS* and *COMBINATIONS*.

    *is_training* can an external scalar boolean tensor denoting the training or testing phase. If
    *None*, in internal flag is created instead and available under the same name. *epsilon* is
    supposed to be a small number that is used in various places for numerical stability. *name* is
    the main namespace of the LBN and defaults to the class name.

    *feature_factory* must be a subclass of :py:class:`FeatureFactoryBase` and provides the
    available, generic mappings from boosted particles to output features of the LBN. If *None*, the
    default :py:class:`FeatureFactory` is used.

    *particle_weights* and *restframe_weights* can refer to externally defined variables with custom
    initialized weights. If set, their shape must match the number of combinations and inputs. For
    simple initialization tests, *weight_init* can be a tuple containing the Gaussian mean and
    standard deviation that is passed to ``tf.random_normal``. When *None*, and the weight tensors
    are created internally, mean and standard deviation default to *0* and *1 / combinations*. When
    *abs_particle_weights* (*abs_restframe_weights*) is *True*, ``tf.abs`` is applied to the
    particle (rest frame) weights. When *clip_particle_weights* (*clip_restframe_weights*) is
    *True*, particle (rest frame) weights are clipped at *epsilon*, or at the passed value if it is
    not a boolean. Note that the abs operation is applied before clipping.

    *batch_norm* defines whether feature scaling via batch normalization with floating averages is
    applied to the output features. It can be a single value or a tuple of two values that are
    passed to ``tf.layers.batch_normalization``. Make sure to set the proper value for the
    *is_training* flag in the feed dict.

    Instances of this class store most of the intermediate tensors (such as inputs, combinations
    weights, boosted particles, boost matrices, raw features, etc) for later inspection. Note that
    most of these tensors are set after :py:meth:`build` (or the :py:meth:`__call__` shorthand as
    shown above) are invoked.
    """

    # available boost modes
    PAIRS = "pairs"
    PRODUCT = "product"
    COMBINATIONS = "combinations"

    def __init__(self, n_particles, n_restframes=None, boost_mode=PAIRS, feature_factory=None,
            particle_weights=None, abs_particle_weights=True, clip_particle_weights=False,
            restframe_weights=None, abs_restframe_weights=True, clip_restframe_weights=False,
            weight_init=None, batch_norm=True, is_training=None, epsilon=1e-5, name=None):
        super(LBN, self).__init__()

        # determine the number of output particles, which depends on the boost mode
        # PAIRS:
        #   n_restframes set to n_particles, boost pairwise, n_out = n_particles
        # PRODUCT:
        #   boost n_particles into n_restframes, n_out = n_partiles * n_restframes
        # COMBINATIONS:
        #   build only particles, boost them into each other, except for boosts of particles into
        #   themselves, n_out = n**2 - n
        if boost_mode == self.PAIRS:
            n_restframes = n_particles
            self.n_out = n_particles
        elif boost_mode == self.PRODUCT:
            self.n_out = n_particles * n_restframes
        elif boost_mode == self.COMBINATIONS:
            n_restframes = n_particles
            self.n_out = n_particles**2 - n_particles
        else:
            raise ValueError("unknown boost_mode '{}'".format(boost_mode))

        # store boost mode and number of particles and restframes to build
        self.boost_mode = boost_mode
        self.n_particles = n_particles
        self.n_restframes = n_restframes

        # output batch normalization
        if isinstance(batch_norm, bool):
            self.batch_norm_center = batch_norm
            self.batch_norm_scale = batch_norm
        elif isinstance(batch_norm, (list, tuple)) and len(batch_norm) == 2:
            self.batch_norm_center, self.batch_norm_scale = batch_norm
        else:
            raise ValueError("invalid batch_norm, should be bool and list/tuple of two bools")

        # particle weights and settings
        self.particle_weights = particle_weights
        self.abs_particle_weights = abs_particle_weights
        self.clip_particle_weights = clip_particle_weights

        # rest frame weigths and settings
        self.restframe_weights = restframe_weights
        self.abs_restframe_weights = abs_restframe_weights
        self.clip_restframe_weights = clip_restframe_weights

        # custom weight init parameters in a tuple (mean, stddev)
        self.weight_init = weight_init

        # training flag
        self.is_training = is_training

        # epsilon for numerical stability
        self.epsilon = epsilon

        # internal name
        self.name = name or self.__class__.__name__

        # sizes that are set during build
        self.n_in = None  # number of input particles
        self.n_dim = None  # size per input vector, must be four

        # constants
        self.I = None  # the I matrix
        self.U = None  # the U matrix

        # tensor of input vectors
        self.inputs = None

        # split input tensors
        self.inputs_E = None  # energy column of inputs
        self.inputs_px = None  # px column of inputs
        self.inputs_py = None  # py column of inputs
        self.inputs_pz = None  # pz column of inputs

        # tensors of particle combinations
        self.particles_E = None  # energy column of combined particles
        self.particles_px = None  # px column of combined particles
        self.particles_py = None  # py column of combined particles
        self.particles_pz = None  # pz column of combined particles
        self.particles_pvec = None  # p vectors of combined particles
        self.particles = None  # stacked 4-vectors of combined particles

        # tensors of rest frame combinations
        self.restframes_E = None  # energy column of combined restframes
        self.restframes_px = None  # px column of combined restframes
        self.restframes_py = None  # py column of combined restframes
        self.restframes_pz = None  # pz column of combined restframes
        self.restframes_pvec = None  # p vectors of combined restframes
        self.restframes = None  # stacked 4-vectors of combined restframes

        # Lorentz boost matrix with shape (batch, n_out, 4, 4)
        self.Lambda = None

        # boosted particles with shape (batch, n_out, 4)
        self.boosted_particles = None

        # intermediate features
        self._raw_features = None  # raw features before batch normalization, etc
        self._norm_features = None  # features after batch normalization, if used

        # final output features
        self.features = None

        # initialize the feature factory
        if feature_factory is None:
            feature_factory = FeatureFactory
        elif not issubclass(feature_factory, FeatureFactoryBase):
            raise TypeError("feature_factory '{}' is not a subclass of FeatureFactoryBase".format(
                feature_factory))
        self.feature_factory = feature_factory(self)

    @property
    def available_features(self):
        """
        Shorthand to access the list of available features in the :py:attr:`feature_factory`.
        """
        return list(self.feature_factory._feature_funcs.keys())

    @property
    def n_features(self):
        """
        Returns the number of created output features which depends on the number of boosted
        particles and the feature set.
        """
        if self.features is None:
            return None

        return self.features.shape[-1].value

    def register_feature(self, func=None, **kwargs):
        """
        Shorthand to register a new feautre to the current :py:attr:`feature_factory` instance. Can
        be used as a (configurable) decorator. The decorated function receives the feature factory
        instance as the only argument. All *kwargs* are forwarded to
        :py:meth:`FeatureFactoryBase._wrap_feature`. Example:

        .. code-block:: python

            lbn = LBN(10, boost_mode=LBN.PAIRS)

            @lbn.register_feature
            def px_plus_py(ff):
                return ff.px() + ff.py()

            print("px_plus_py" in lbn.available_features)  # -> True

            # or register with a different name
            @lbn.register_feature(name="pxy")
            def px_plus_py(ff):
                return ff.px() + ff.py()

            print("pxy" in lbn.available_features)  # -> True
        """
        def decorator(func):
            return self.feature_factory._wrap_feature(func, **kwargs)

        return decorator(func) if func else decorator

    def __call__(self, *args, **kwargs):
        """
        Shorthand for :py:meth:`build`.
        """
        return self.build(*args, **kwargs)

    def build(self, inputs, **kwargs):
        """
        Builds the LBN structure layer by layer within dedicated variable scopes. *input* must be a
        tensor of the input four-vectors. All *kwargs* are forwarded to :py:meth:`build_features`.
        """
        with tf.variable_scope(self.name):
            with tf.variable_scope("placeholders"):
                self.build_placeholders()

            with tf.variable_scope("inputs"):
                self.handle_input(inputs)

            with tf.variable_scope("constants"):
                self.build_constants()

            with tf.variable_scope("particles"):
                self.build_combinations("particle", self.n_particles)

Marcel Rieger's avatar
Marcel Rieger committed
268
            # rest frames are not built for COMBINATIONS boost mode
Marcel Rieger's avatar
Marcel Rieger committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
            if self.boost_mode != self.COMBINATIONS:
                with tf.variable_scope("restframes"):
                    self.build_combinations("restframe", self.n_restframes)

            with tf.variable_scope("boost"):
                self.build_boost()

            with tf.variable_scope("features"):
                self.build_features(**kwargs)
            self.features = self._raw_features

            if self.batch_norm_center or self.batch_norm_scale:
                with tf.variable_scope("norm"):
                    self.build_norm()
                self.features = self._norm_features

        return self.features

    def build_placeholders(self):
        """
        Builds the internal placeholders.
        """
        # the train phase placeholder
        if self.is_training is None:
            self.is_training = tf.placeholder(tf.bool, name="is_training")

    def handle_input(self, inputs):
        """
        Takes the passed four-vector *inputs* and infers dimensions and some internal tensors.
        """
        # store the input vectors
        self.inputs = inputs

        # infer sizes
        self.n_in = self.inputs.shape[1].value
        self.n_dim = self.inputs.shape[2].value
        if self.n_dim != 4:
            raise Exception("input dimension must be 4 to represent 4-vectors")

        # split 4-vector components
        names = ["E", "px", "py", "pz"]
        split = [1, 1, 1, 1]
        for t, name in zip(tf.split(self.inputs, split, axis=-1), names):
            setattr(self, "inputs_" + name, tf.squeeze(t, -1))

    def build_constants(self):
        """
        Builds the internal constants for the boost matrix.
        """
        # 4x4 identity
        self.I = tf.constant(np.identity(4), tf.float32)

        # U matrix
        self.U = tf.constant([[-1, 0, 0, 0]] + 3 * [[0, -1, -1, -1]], tf.float32)

    def build_combinations(self, name, m):
        """
        Builds the combination layers which are quite similiar for particles and rest frames. Hence,
        *name* must be either ``"particle"`` or ``"restframe"``, and *m* is the corresponding number
        of combinations.
        """
        if name not in ("particle", "restframe"):
            raise ValueError("unknown combination name '{}'".format(name))

        # determine the weight tensor shape
        weight_shape = (self.n_in, m)

        # name helper
        name_ = lambda tmpl: tmpl.format(name)

        # build the weight matrix, or check it if already set
        W = getattr(self, name_("{}_weights"))
        if W is None:
            # build a new weight matrix
            if isinstance(self.weight_init, tuple):
                mean, stddev = self.weight_init
            else:
                mean, stddev = 0., 1. / m

            W = tf.Variable(tf.random_normal(weight_shape, mean, stddev, dtype=tf.float32))

        else:
            # W is set externally, check the shape, consider batching
            shape = tuple(W.shape.as_list())
            if shape != weight_shape:
                raise ValueError("external {}_weights shape {} does not match {}".format(
                    name, shape, weight_shape))

        # store as raw weights before applying abs or clipping
        W = tf.identity(W, name=name_("raw_{}_weights"))

        # apply abs
        if getattr(self, name_("abs_{}_weights")):
            W = tf.abs(W, name=name_("abs_{}_weights"))

        # apply clipping
        clip = getattr(self, name_("clip_{}_weights"))
        if clip is True:
            clip = self.epsilon
        if clip is not False:
            W = tf.maximum(W, clip, name=name_("clipped_{}_weights"))

        # assign a name to the final weights
        W = tf.identity(W, name=name_("{}_weights"))

        # create four-vectors of combinations
        E = tf.matmul(self.inputs_E, W, name=name_("{}s_E"))
        px = tf.matmul(self.inputs_px, W, name=name_("{}s_px"))
        py = tf.matmul(self.inputs_py, W, name=name_("{}s_py"))
        pz = tf.matmul(self.inputs_pz, W, name=name_("{}s_pz"))

        # create the full 3- and 4-vector stacks again
        p = tf.stack([px, py, pz], axis=-1, name=name_("{}s_pvec"))
        q = tf.stack([E, px, py, pz], axis=-1, name=name_("{}s"))

        # save all tensors for later inspection
        setattr(self, name_("{}_weights"), W)
        setattr(self, name_("{}s_E"), E)
        setattr(self, name_("{}s_px"), px)
        setattr(self, name_("{}s_py"), py)
        setattr(self, name_("{}s_pz"), pz)
        setattr(self, name_("{}s_pvec"), p)
        setattr(self, name_("{}s"), q)

    def build_boost(self):
        """
        Builds the boosted particles depending on the requested boost mode. For infos on the boost
        matrix, see `this link <https://en.wikipedia.org/wiki/Lorentz_transformation>`__. The
        vectorized implementation is as follows:

        I = identity(4x4)

        U = -1(1x1)  0(1x3)
             0(3x1) -1(3x3)

        e = (1, -beta_vec/beta(1x3))^T

        Lambda = I + (U + gamma) x ((U + 1) x beta - U) x e . e^T
        """
        # n_particles and n_restframes must be identical for PAIRS and COMBINATIONS boosting
        if self.boost_mode in (self.PAIRS, self.COMBINATIONS):
            if self.n_restframes != self.n_particles:
                raise ValueError("n_restframes ({}) must be identical to n_particles ({}) in boost"
                    " mode '{}'".format(self.n_restframes, self.n_particles, self.boost_mode))

        # get the objects that are used to infer beta and gamma for the build the boost matrix,
        if self.boost_mode == self.COMBINATIONS:
            restframes_E = self.particles_E
            restframes_pvec = self.particles_pvec
        else:
            restframes_E = self.restframes_E
            restframes_pvec = self.restframes_pvec

        # to build the boost parameters, reshape E and p tensors so that batch and particle axes
        # are merged, and once the Lambda matrix is built, this reshape is reverted again
        # note: there might be more performant operations in future TF releases
        E = tf.reshape(restframes_E, [-1, 1])
        pvec = tf.reshape(restframes_pvec, [-1, 3])

        # determine the beta vectors
        betavec = tf.div(pvec, E)

        # determine the scalar beta and gamma values
        beta = tf.div(tf.sqrt(tf.reduce_sum(tf.square(pvec), axis=1)), tf.squeeze(E, axis=-1))
        gamma = tf.div(1., tf.sqrt(1. - tf.square(beta) + self.epsilon))

        # the e vector, (1, -betavec / beta)^T
        beta = tf.expand_dims(beta, axis=-1)
        e = tf.expand_dims(tf.concat([tf.ones_like(E), -tf.div(betavec, beta)], axis=-1), axis=-1)
        e_T = tf.transpose(e, perm=[0, 2, 1])

        # finally, the boost matrix
        beta = tf.expand_dims(beta, axis=-1)
        gamma = tf.reshape(gamma, [-1, 1, 1])
        Lambda = self.I + (self.U + gamma) * ((self.U + 1) * beta - self.U) * tf.matmul(e, e_T)

        # revert the merging of batch and particle axes
        Lambda = tf.reshape(Lambda, [-1, self.n_restframes, 4, 4])

        # prepare particles for matmul
        particles = tf.reshape(self.particles, [-1, self.n_particles, 4, 1])

        # Lambda and particles need to be updated for PRODUCT and COMBINATIONS boosting
        if self.boost_mode in (self.PRODUCT, self.COMBINATIONS):
            # two approaches are possible
            # a) tile Lambda while repeating particles
            # b) batched gather using tiled and repeated indices
            # go with b) for the moment since diagonal entries can be removed before the matmul
            l_indices = np.tile(np.arange(self.n_restframes), self.n_particles)
            p_indices = np.repeat(np.arange(self.n_particles), self.n_restframes)

            # remove indices that would lead to diagonal entries for COMBINATIONS boosting
            if self.boost_mode == self.COMBINATIONS:
                no_diag = np.hstack((triu_range(self.n_particles), tril_range(self.n_particles)))
                l_indices = l_indices[no_diag]
                p_indices = p_indices[no_diag]

            # update Lambda and particles
            Lambda = tf.gather(Lambda, l_indices, axis=1)
            particles = tf.gather(particles, p_indices, axis=1)

        # store the final boost matrix
        self.Lambda = Lambda

        # actual boosting
        boosted_particles = tf.matmul(self.Lambda, particles)

        # remove the last dimension resulting from multiplication and save
        self.boosted_particles = tf.squeeze(boosted_particles, axis=-1, name="boosted_particles")

    def build_features(self, features=None, external_features=None):
        """
        Builds the output features. *features* should be a list of feature names as registered to
        the :py:attr:`feature_factory` instance. When *None*, the default features
Marcel Rieger's avatar
Marcel Rieger committed
483 484
        ``["E", "px", "py", "pz"]`` are built. *external_features* can be a list of tensors of
        externally produced features, that are concatenated to the built features and are, e.g.,
Marcel Rieger's avatar
Marcel Rieger committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
        subject to the internal batch normalization.
        """
        # default to reshaped 4-vector elements
        if features is None:
            features = ["E", "px", "py", "pz"]

        # create the list of feature ops to concat
        concat = []
        for name in features:
            func = getattr(self.feature_factory, name)
            if func is None:
                raise ValueError("unknown feature '{}'".format(name))
            concat.append(func())

        # add external features
        if external_features is not None:
            if isinstance(external_features, (list, tuple)):
                concat.extend(list(external_features))
            else:
                concat.append(external_features)

        # save raw features
        self._raw_features = tf.concat(concat, axis=-1)

    def build_norm(self):
        """
        Applies simple batch normalization with floating averages to the output features using
        ``tf.layers.batch_normalization``. Make sure to also run the operation returned by
        ``tf.get_collection(tf.GraphKeys.UPDATE_OPS)`` during each train step.
        """
        self._norm_features = tf.layers.batch_normalization(
            self.features,
            axis=1,
            training=self.is_training,
            center=self.batch_norm_center,
            scale=self.batch_norm_scale,
        )


class FeatureFactoryBase(object):
    """
Marcel Rieger's avatar
Marcel Rieger committed
526 527
    Base class of the feature factory. It does not implement actual features but rather the
    feature wrapping and tensor caching functionality. So-called hidden features are also subject to
Marcel Rieger's avatar
Marcel Rieger committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
    caching but are not supposed to be accessed by the LBN. They rather provide intermediate results
    that are used in multiple places and retained for performance purposes.
    """

    excluded_attributes = ["_wrap_feature", "_wrap_features", "lbn"]

    def __init__(self, lbn):
        super(FeatureFactoryBase, self).__init__()

        # cached tensors stored by name
        # contains also hidden features
        self._tensor_cache = {}

        # dict of registered, bound feature functions
        # does not contain hidden features
        self._feature_funcs = {}

        # wrap all features defined in this class
        self._wrap_features()

        # reference to the lbn instance
        self.lbn = lbn

        # some shorthands
        self.n = lbn.n_out
        self.epsilon = lbn.epsilon

    def _wrap_feature(self, func, name=None, hidden=None):
        """
        Wraps and registers a feature function. It ensures that the stored function is bound to this
        instance. *name* defaults to the actual function name. When *hidden* is *None*, the decision
Marcel Rieger's avatar
Marcel Rieger committed
559
        is inferred from whether *name* starts with an underscore.
Marcel Rieger's avatar
Marcel Rieger committed
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
        """
        if not name:
            name = func.__name__
        if hidden is None:
            hidden = name.startswith("_")

        # bind it to self if not bound yet
        if getattr(func, "__self__", None) is None:
            func = func.__get__(self)

        @functools.wraps(func)
        def wrapper(ff, *args, **kwargs):
            if kwargs.pop("no_cache", False):
                return func(*args, **kwargs)
            else:
                if name not in self._tensor_cache:
                    self._tensor_cache[name] = tf.identity(func(*args, **kwargs), name=name)
                return self._tensor_cache[name]

        # register the bound, caching-aware wrapper to this instance
        setattr(self, name, wrapper.__get__(self))

        # store in known feature func if not hidden
        if not hidden:
            self._feature_funcs[name] = wrapper

        return wrapper

    def _wrap_features(self):
        """
        Interprets all non-excluded instance methods as feature functions and replaces them by
Marcel Rieger's avatar
Marcel Rieger committed
591
        caching-aware wrappers using :py:meth:`_wrap_feature`.
Marcel Rieger's avatar
Marcel Rieger committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
        """
        # loop through attributes
        for name in dir(self):
            # magic method or excluded?
            if name.startswith("__") or name in self.excluded_attributes:
                continue

            # callable?
            func = getattr(self, name)
            if not callable(func):
                continue

            # wrap it
            self._wrap_feature(func, name)


class FeatureFactory(FeatureFactoryBase):
    """
Marcel Rieger's avatar
Marcel Rieger committed
610
    Default feature factory implementing various generic feature mappings.
Marcel Rieger's avatar
Marcel Rieger committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    """

    def __init__(self, lbn):
        super(FeatureFactory, self).__init__(lbn)

        # pairwise features are computed by multiplying row and column vectors to obtain a
        # matrix from which we want to extract the values of the upper triangle w/o diagonal,
        # so store these upper triangle indices for later use in tf.gather
        self.triu_indices = triu_range(self.n)

    def E(self):
        """
        Energy.
        """
        return self.lbn.boosted_particles[..., 0]

    def px(self):
        """
        Momentum component x.
        """
        return self.lbn.boosted_particles[..., 1]

    def py(self):
        """
        Momentum component y.
        """
        return self.lbn.boosted_particles[..., 2]

    def pz(self):
        """
        Momentum component z.
        """
        return self.lbn.boosted_particles[..., 3]

    def _pvec(self):
        """
        Momentum vector. Hidden.
        """
        return self.lbn.boosted_particles[..., 1:]

    def _p2(self):
        """
        Squared absolute momentum. Hidden.
        """
        return tf.maximum(tf.reduce_sum(self._pvec()**2, axis=-1), self.epsilon)

    def p(self):
        """
        Absolute momentum.
        """
        return self._p2()**0.5

    def pt(self):
        """
        Scalar, transverse momentum.
        """
        return tf.maximum(self._p2() - self.pz()**2, self.epsilon)**0.5

    def eta(self):
        """
        Pseudorapidity.
        """
        return tf.atanh(tf.clip_by_value(self.pz() / self.p(), self.epsilon - 1, 1 - self.epsilon))

    def phi(self):
        """
        Azimuth.
        """
        return tf.atan2(tf_non_zero(self.py(), self.epsilon), self.px())

    def m(self):
        """
        Mass.
        """
        return tf.maximum(self.E()**2 - self._p2(), self.epsilon)**0.5

    def beta(self):
        """
        Relativistic speed, v/c or p/E.
        """
        return self.p() / tf.maximum(self.E(), self.epsilon)

    def gamma(self):
        """
        Relativistic gamma factor, 1 / sqrt(1-beta**2) or E / m.
        """
        return self.E() / tf.maximum(self.m(), self.epsilon)

    def pair_dr(self):
        """
        Distance between all pairs of particles in the eta-phi plane.
        """
        # eta difference on lower triangle elements
        d_eta = tf.reshape(self.eta(), (-1, self.n, 1)) - tf.reshape(self.eta(), (-1, 1, self.n))
        d_eta = tf.gather(tf.reshape(d_eta, (-1, self.n**2)), self.triu_indices, axis=1)

        # phi difference on lower triangle elements, handle boundaries
        d_phi = tf.reshape(self.phi(), (-1, self.n, 1)) - tf.reshape(self.phi(), (-1, 1, self.n))
        d_phi = tf.gather(tf.reshape(d_phi, (-1, self.n**2)), self.triu_indices, axis=1)
        d_phi = tf.abs(d_phi)
        d_phi = tf.minimum(d_phi, 2. * np.math.pi - d_phi)

        return (d_eta**2 + d_phi**2)**0.5

    def _pvec_norm(self):
        """
        Normalized momentum vector. Hidden.
        """
        return self._pvec() / tf.expand_dims(self.p(), axis=-1)

    def _pvec_norm_T(self):
        """
        Normalized, transposed momentum vector. Hidden.
        """
        return tf.transpose(self._pvec_norm(), perm=[0, 2, 1])

    def pair_cos(self):
        """
        Cosine of the angle between all pairs of particles.
        """
        # cos = (p1 x p2) / (|p1| x |p2|) = (p1 / |p1|) x (p2 / |p2|)
        all_pair_cos = tf.matmul(self._pvec_norm(), self._pvec_norm_T())

        # return only upper triangle without diagonal
        return tf.gather(tf.reshape(all_pair_cos, [-1, self.n**2]), self.triu_indices, axis=1)

    def pair_ds(self):
        """
        Sign-conserving Minkowski space distance between all pairs of particles.
        """
        # (dE**2 - dpx**2 - dpy**2 - dpz**2)**0.5
        # first, determine all 4-vector differences
        pvm = tf.expand_dims(self.lbn.boosted_particles, axis=-2)
        pvm_T = tf.transpose(pvm, perm=[0, 2, 1, 3])
        all_diffs = pvm - pvm_T

        # extract elements of the upper triangle w/o diagonal and calculate their norm
        diffs = tf.gather(tf.reshape(all_diffs, [-1, self.n**2, 4]), self.triu_indices, axis=1)
        diffs_E = diffs[..., 0]
        diffs_p2 = tf.reduce_sum(diffs[..., 1:]**2, axis=-1)

        ds = diffs_E**2 - diffs_p2
        return tf.sign(ds) * tf.abs(ds)**0.5

    def pair_dy(self):
        """
        Rapidity difference between all pairs of particles.
        """
        # dy = y1 - y2 = atanh(beta1) - atanh(beta2)
        beta = tf.clip_by_value(self.beta(), self.epsilon, 1 - self.epsilon)
        dy = tf.atanh(tf.expand_dims(beta, axis=-1)) - tf.atanh(tf.expand_dims(beta, axis=-2))

        # return only upper triangle without diagonal
        return tf.gather(tf.reshape(dy, [-1, self.n**2]), self.triu_indices, axis=1)


def tf_non_zero(t, epsilon):
    """
    Ensures that all zeros in a tensor are replaced with epsilon.
    """
    # use combination of abs and sign instead of a where op
    return t + (1 - tf.abs(tf.sign(t))) * epsilon


def tril_range(n, k=-1):
    """
    Returns a 1D numpy array containing all lower triangle indices of a square matrix with size *n*.
    *k* is the offset from the diagonal.
    """
    tril_indices = np.tril_indices(n, k)
    return np.arange(n**2).reshape(n, n)[tril_indices]


def triu_range(n, k=1):
    """
    Returns a 1D numpy array containing all upper triangle indices of a square matrix with size *n*.
    *k* is the offset from the diagonal.
    """
    triu_indices = np.triu_indices(n, k)
    return np.arange(n**2).reshape(n, n)[triu_indices]