Exemplo n.º 1
0
def sqr_vonMises23D_angle(y_reco, y_true, re=False):

    #energy
    loss_energy = reduce_mean(
        tf.math.squared_difference(y_reco[:, 0], y_true[:, 0]))  #mae again

    polar_k = abs(y_reco[:, 3]) + eps
    zenth_k = abs(y_reco[:, 4]) + eps

    cos_azi = cos(subtract(y_true[:, 2], y_reco[:, 2]))

    cos_zenth = cos(subtract(y_true[:, 1], y_reco[:, 1]))

    lnI0_azi = polar_k + tf.math.log(
        1 + tf.math.exp(-2 * polar_k)
    ) - 0.25 * tf.math.log(1 + 0.25 * tf.square(polar_k)) + tf.math.log(
        1 + 0.24273 * tf.square(polar_k)) - tf.math.log(1 + 0.43023 *
                                                        tf.square(polar_k))
    lnI0_zenth = zenth_k + tf.math.log(
        1 + tf.math.exp(-2 * zenth_k)
    ) - 0.25 * tf.math.log(1 + 0.25 * tf.square(zenth_k)) + tf.math.log(
        1 + 0.24273 * tf.square(zenth_k)) - tf.math.log(1 + 0.43023 *
                                                        tf.square(zenth_k))

    llh_azi = polar_k * cos_azi - lnI0_azi
    llh_zenith = zenth_k * cos_zenth - lnI0_zenth

    loss_azi = reduce_mean(-llh_azi)
    loss_zenith = reduce_mean(-llh_zenith)

    kappa = tf.math.abs(y_reco[:, 5]) + eps
    cos_alpha = cos_angle(y_reco, y_true)
    # tf.debugging.assert_less_equal(tf.math.abs(cos_alpha), 1, message='cos_alpha problem', summarize=None, name=None)
    tf.debugging.assert_all_finite(tf.math.abs(cos_alpha),
                                   message='cos_alpha problem infinite/nan',
                                   name=None)
    nlogC = -tf.math.log(kappa) + kappa + tf.math.log(1 -
                                                      tf.math.exp(-2 * kappa))
    tf.debugging.assert_all_finite(nlogC, 'log kappa problem', name=None)

    loss_angle = tf.reduce_mean(-kappa * cos_alpha + nlogC)

    if not re:
        return loss_azi + loss_zenith + loss_energy + loss_angle
    if re:
        return float(loss_azi + loss_zenith + loss_energy + loss_angle), [
            float(loss_energy),
            float(loss_zenith),
            float(loss_azi),
            float(loss_angle)
        ]
Exemplo n.º 2
0
    def trainModel(self, infos):

        with self.graph.as_default():
            states = tf.constant([i["state"].tolist() for i in infos],
                                 shape=[infos.size, 24])
            rewards = tf.constant([i["reward"] for i in infos],
                                  dtype=tf.float32,
                                  shape=[infos.size, 1])
            # next_states = tf.constant([i["next_state"].tolist() for i in infos], shape=[infos.size, 24])
            actions = tf.constant([i["action"] for i in infos],
                                  dtype=tf.float32,
                                  shape=[infos.size, 12])

            # Qtargets = tf.constant(self.model.predict(next_states, steps=1), shape=[infos.size, 12])
            # Recupere l'etat actuel
            targets = tf.constant(self.model.predict(states, steps=1),
                                  shape=[infos.size, 12])
            # Calcure le mask negatif
            mask = tf.ones([infos.size, 12], dtype=tf.float32)
            mask = tfm.subtract(mask, actions)
            # Applique le mask négatif
            targets = tfm.multiply(targets, mask)

            # Calcure le mask positif
            mask = tfm.multiply(rewards, actions)
            # Applique le mask positif
            targets = tfm.add(targets, mask)

            self.model.fit(states, targets, steps_per_epoch=200)  # 1000
Exemplo n.º 3
0
 def loss(y_pred, y_true):
     #y_true = print_tensor(y_true, message='y_true = ')
     #y_pred = print_tensor(y_pred, message='y_pred = ')
     absPred = y_pred[:, :1]
     absTrue = y_true[:, :1]
     #absPred = print_tensor(absPred, message='absPred = ')
     #absTrue = print_tensor(absTrue, message='absTrue = ')
     #prvPred = y_pred[1:]
     prvTrue = y_true[:, 1:]
     #prvTrue = print_tensor(prvTrue, message='prvTrue = ')
     anglePred = tanh(subtract(absPred, prvTrue))
     angleTrue = tanh(subtract(absTrue, prvTrue))
     grd = gradientMass * (mse(anglePred, angleTrue))
     abs = absoluteMass * (mse(absPred, absTrue))
     #return mse(anglePred,angleTrue)
     return grd + abs
Exemplo n.º 4
0
def abs_negcos_angle(y_reco, y_true, re=False):
    # Energy loss
    loss_energy = reduce_mean(abs(subtract(y_reco[:,0], y_true[:,0]))) #this works well but could maybe be improved
    # Angle loss
    loss_angle = reduce_mean(1-cos_angle(y_reco, y_true))
    if not re:
        return loss_energy+loss_angle
    else:   
        return float(loss_energy+loss_angle), [float(loss_energy), float(loss_angle)]
Exemplo n.º 5
0
def abs_vM2D_KDE_weak(y_reco, y_true, kdet, re=False):
    #energy
    loss_energy = reduce_mean(abs(subtract(y_reco[:, 0],
                                           y_true[:, 0])))  #mae again

    polar_k = abs(y_reco[:, 3]) + eps
    zenth_k = abs(y_reco[:, 4]) + eps

    cos_azi = cos(subtract(y_true[:, 2], y_reco[:, 2]))

    cos_zenth = cos(subtract(y_true[:, 1], y_reco[:, 1]))

    lnI0_azi = polar_k + tf.math.log(
        1 + tf.math.exp(-2 * polar_k)
    ) - 0.25 * tf.math.log(1 + 0.25 * tf.square(polar_k)) + tf.math.log(
        1 + 0.24273 * tf.square(polar_k)) - tf.math.log(1 + 0.43023 *
                                                        tf.square(polar_k))
    lnI0_zenth = zenth_k + tf.math.log(
        1 + tf.math.exp(-2 * zenth_k)
    ) - 0.25 * tf.math.log(1 + 0.25 * tf.square(zenth_k)) + tf.math.log(
        1 + 0.24273 * tf.square(zenth_k)) - tf.math.log(1 + 0.43023 *
                                                        tf.square(zenth_k))

    llh_azi = polar_k * cos_azi - lnI0_azi
    llh_zenith = zenth_k * cos_zenth - lnI0_zenth

    loss_azi = reduce_mean(-llh_azi)
    loss_zenith = reduce_mean(-llh_zenith)
    kder = kde(tf.cast(y_reco[:, 1], tf.float32))
    kdeloss = tf.reduce_mean(
        tf.math.abs(kdet.log_prob(xkde) - kder.log_prob(xkde))) / 10

    if not re:
        return loss_azi + loss_zenith + loss_energy + tf.cast(
            kdeloss, tf.float32)
    if re:
        return float(loss_azi + loss_zenith + loss_energy + kdeloss), [
            float(loss_energy),
            float(loss_zenith),
            float(loss_azi),
            float(kdeloss)
        ]
Exemplo n.º 6
0
def compute_covariance(x):
    """ Compute the covariance cov(x) = E[x*x^T] - E[x]E[x]^T. Based on Locatello et al. implementation
    (https://github.com/google-research/disentanglement_lib)

    :param x: a matrix of size N*M
    :return: the covariance of x, a matrix of size M*M
    """
    e_x = tfm.reduce_mean(x, axis=0)
    e_x_e_xt = tf.expand_dims(e_x, 1) * tf.expand_dims(e_x, 0)
    e_xxt = tfm.reduce_mean(tf.expand_dims(x, 2) * tf.expand_dims(x, 1), axis=0)
    return tfm.subtract(e_xxt, e_x_e_xt)
Exemplo n.º 7
0
    def call(self, inputs):
        mean = reduce_mean(inputs, axis=0)
        std = reduce_std(inputs, axis=0) + 1e-6

        InputBatchNormalization.temp += 1
        InputBatchNormalization.mean += mean
        InputBatchNormalization.std += std

        inputs = divide(subtract(inputs, mean), std)

        return inputs.squeeze(0)
Exemplo n.º 8
0
def sqr_vonMises2D_angle(y_reco, y_true, re=False):
    #energy
    loss_energy = reduce_mean(
        tf.math.squared_difference(y_reco[:, 0], y_true[:, 0]))  #mae again

    polar_k = abs(y_reco[:, 3]) + eps
    zenth_k = abs(y_reco[:, 4]) + eps

    cos_azi = cos(subtract(y_true[:, 2], y_reco[:, 2]))

    cos_zenth = cos(subtract(y_true[:, 1], y_reco[:, 1]))

    lnI0_azi = polar_k + tf.math.log(
        1 + tf.math.exp(-2 * polar_k)
    ) - 0.25 * tf.math.log(1 + 0.25 * tf.square(polar_k)) + tf.math.log(
        1 + 0.24273 * tf.square(polar_k)) - tf.math.log(1 + 0.43023 *
                                                        tf.square(polar_k))
    lnI0_zenth = zenth_k + tf.math.log(
        1 + tf.math.exp(-2 * zenth_k)
    ) - 0.25 * tf.math.log(1 + 0.25 * tf.square(zenth_k)) + tf.math.log(
        1 + 0.24273 * tf.square(zenth_k)) - tf.math.log(1 + 0.43023 *
                                                        tf.square(zenth_k))

    llh_azi = polar_k * cos_azi - lnI0_azi
    llh_zenith = zenth_k * cos_zenth - lnI0_zenth

    loss_azi = reduce_mean(-llh_azi)
    loss_zenith = reduce_mean(-llh_zenith)
    if not re:
        return loss_azi + loss_zenith + loss_energy
    if re:
        return float(loss_azi + loss_zenith + loss_energy), [
            float(loss_energy),
            float(loss_zenith),
            float(loss_azi)
        ]
Exemplo n.º 9
0
def abs_linear_unit(y_reco, y_true, re=False):
    ''
    from tensorflow.math import sin, cos, acos, abs, reduce_mean, subtract
    
    #energy loss

    loss_energy = reduce_mean(abs(subtract(y_reco[:,0], y_true[:,0])))
    
    #angle loss
    
    cos_alpha = cos_unit(y_reco,y_true)
    loss_angle = reduce_mean(tf.math.acos(cos_alpha))
    if not re:
        return loss_energy+loss_angle
    else:   
        return float(loss_energy+loss_angle), [float(loss_energy), float(loss_angle)]
Exemplo n.º 10
0
def loss_funcxpos2(y_reco, y_true, re=False):
    from tensorflow.math import sin, cos, acos, abs, reduce_mean, subtract, square
    # Energy loss
    loss_energy = reduce_mean(abs(subtract(y_reco[:,0], y_true[:,0]))) #this works well but could maybe be improved

    zeni = [cos(y_true[:,1]) - y_reco[:,1] , 
            sin(y_true[:,1]) - y_reco[:,2]]

    azi  = [cos(y_true[:,2]) - y_reco[:,3] , 
            sin(y_true[:,2]) - y_reco[:,4]]

    loss_angle = reduce_mean(square(azi[0]))+reduce_mean(square(azi[1]))+reduce_mean(square(zeni[0]))+reduce_mean(square(zeni[1]))
    if not re:
        return loss_energy+loss_angle
    else:   
        return float(loss_energy+loss_angle), [float(loss_energy), float(loss_angle)]
Exemplo n.º 11
0
def psnr(y_label, y_pred):
    """
    PSNR is Peek Signal to Noise Ratio, which is similar to mean squared error.

    It can be calculated as
    PSNR = 20 * log10(MAXp) - 10 * log10(MSE)

    When providing an unscaled input, MAXp = 255. Therefore 20 * log10(255)== 48.1308036087.
    However, since we are scaling our input, MAXp = 1. Therefore 20 * log10(1) = 0.
    Thus we remove that component completely and only compute the remaining MSE component.
    """
    _result = subtract(y_label, y_pred)
    _result = square(_result)
    _result = tf_mean(_result)
    _result = multiply(-10., log(_result, 10.))
    return _result
Exemplo n.º 12
0
 def update_state(self, y_true, y_pred, sample_weight=None):
     y_true = tf.cast(y_true, tf.float32)
     y_pred = tf.cast(y_pred, tf.float32)
     error = 100 * math.abs(math.subtract(y_true, y_pred)) / y_true
     return super(MAPE, self).update_state(error,
                                           sample_weight=sample_weight)
Exemplo n.º 13
0
 def f2():
     val1 = tfm.divide(
         tfm.multiply(tfm.subtract(k, k_1), tfm.subtract(k, k_1)), 2)
     val2 = tfm.multiply(tfm.subtract(k, k_1), tfm.subtract(x, k))
     val = tfm.add(val1, val2)
     return val
Exemplo n.º 14
0
 def f1():
     return tfm.add(
         tfm.subtract(tfm.divide(tfm.multiply(x, x), 2),
                      tfm.multiply(k_1, x)),
         tfm.divide(tfm.multiply(k_1, k_1), 2))
Exemplo n.º 15
0
def DSSIM(y_true, y_pred):
    return tfmath.divide(
        tfmath.subtract(1.0, tfimage.ssim(y_true, y_pred, max_val=1.0)), 2.0)
Exemplo n.º 16
0
def tf_ssim(x, y, is_normalized=False):
    """
    k1 = 0.01
    k2 = 0.03
    L = 1.0 if is_normalized else 255.0
    c1 = np.power(k1 * L, 2)
    c2 = np.power(k2 * L, 2)
    c3 = c2 / 2
    """
    k1 = 0.01
    k2 = 0.03
    L = 1.0 if is_normalized else 255.0
    c1 = tf_pow(multiply(k1, L), 2.0)
    c2 = tf_pow(multiply(k2, L), 2.0)
    c3 = divide(c2, 2.0)

    # if type(x) is np.ndarray:
    #      x = tf.convert_to_tensor(x, dtype=tf.float32)
    # if type(y) is np.ndarray:
    #      y = tf.convert_to_tensor(y, dtype=tf.float32)
    """
    ux = x.mean()
    uy = y.mean()
    """
    ux = tf_mean(x)
    uy = tf_mean(y)
    """
    std_x = x.std()
    std_y = y.std()
    """
    std_x = tf_std(x)
    std_y = tf_std(y)
    """
    xy = (x - ux) * (y - uy)
    std_xy = xy.mean()
    """
    xy = multiply(subtract(x, ux), subtract(y, uy))
    std_xy = tf_mean(xy)
    """
    l_xy = (2 * ux * uy + c1) / (np.power(ux, 2) + np.power(uy, 2) + c1)
    """
    l_son = add(multiOperation(multiply, 2.0, ux, uy), c1)
    l_mom = multiOperation(add, tf_pow(ux, 2.0), tf_pow(uy, 2.0), c1)
    l_xy = divide(l_son, l_mom)
    """
    c_xy = (2 * std_x * std_y + c2) / (np.power(std_x, 2) + np.power(std_y, 2) + c2)
    """
    c_son = add(multiOperation(multiply, 2.0, std_x, std_y), c2)
    c_mom = multiOperation(add, tf_pow(std_x, 2.0), tf_pow(std_y, 2.0), c2)
    c_xy = divide(c_son, c_mom)
    """
    s_xy = (std_xy + c3) / (std_x * std_y + c3)
    """
    s_son = add(std_xy, c3)
    s_mom = add(multiply(std_x, std_y), c3)
    s_xy = divide(s_son, s_mom)

    one = tf.constant(1.0)
    _ssim = multiOperation(multiply, l_xy, c_xy, s_xy)
    _result = tf.cond(greater(_ssim, one), lambda: one, lambda: _ssim)

    return _result