Beispiel #1
0
import warnings
import numpy as np
import orcanet.misc as misc

# for loading via toml
lmods, register = misc.get_register()


class ColumnLabels:
    """
    Label of each model output is column with the same name in the h5 file.
    This is the default label modifier.

    Example
    -------
    Model has output "energy" --> label is column "energy" from the label
    dataset in the h5 file.

    Parameters
    ----------
    model : ks.Model
            A keras model.

    """
    def __init__(self, model):
        self.output_names = model.output_names

    def __call__(self, info_blob):
        ys = {name: info_blob["y_values"][name] for name in self.output_names}
        return ys
Beispiel #2
0
"""
Some basic sample modifiers to use with orcanet.
Use them by setting .cfg.sample_modifier of the orcanet.core.Organizer.

"""
from abc import abstractmethod
import warnings
import numpy as np
from orcanet.misc import get_register
import tensorflow as tf

# for loading via toml
smods, register = get_register()


class PerInputModifier:
    """
    For modifiers that do the same operation on each input.
    Apply modify on x_value of each input, and output as dict.

    """
    def __call__(self, info_blob):
        x_values = info_blob["x_values"]
        xs = dict()
        for key, x_value in x_values.items():
            xs[key] = self.modify(x_value)
        return xs

    @abstractmethod
    def modify(self, x_value):
        """ x_value is a batch of input data as a numpy array. """
Beispiel #3
0
 def test_register(self):
     saved, register = misc.get_register()
     register(_func)
     register(_Cls)
     self.assertDictEqual(self.register, saved)
Beispiel #4
0
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras as ks
import tensorflow.keras.layers as layers
import medgeconv
from orcanet.misc import get_register

# for loading via toml and orcanet custom objects
blocks, register = get_register()
# edge conv blocks
register(medgeconv.DisjointEdgeConvBlock)


@register
class ConvBlock:
    """
    1D/2D/3D Convolutional block followed by BatchNorm, Activation,
    MaxPooling and/or Dropout.

    Parameters
    ----------
    conv_dim : int
        Specifies the dimension of the convolutional block, 1D/2D/3D.
    filters : int
        Number of filters used for the convolutional layer.
    strides : int or tuple
        The stride length of the convolution.
    padding : str or int or list
        If str: Padding of the conv block.
        If int or list: Padding argument of a ZeroPaddingND layer that
        gets added before the convolution.
Beispiel #5
0
"""
OrcaNet custom loss functions.
"""
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
from orcanet.misc import get_register

# fuzz factor for numerical stability
EPS = tf.constant(1e-7, dtype="float32")
# for loading via toml and orcanet custom objects
loss_functions, _register = get_register()


@_register
def lkl_normal_tfp(y_true, y_pred):
    """ Normal distribution using tfp. See lkl_normal. """
    mu_true = y_true[:, 0]
    mu_pred, sigma_pred = y_pred[:, 0], y_pred[:, 1]

    return -1 * tfp.distributions.Normal(
        loc=mu_pred,
        scale=tf.math.maximum(sigma_pred, EPS),
    ).log_prob(mu_true)


@_register
def lkl_normal(y_true, y_pred):
    """
    Negative normal log-likelihood function for n regression output neurons
    with clipping for increased stability.