Exemplo n.º 1
0
def gradient_penalty_loss(y_true, y_pred, averaged_samples,
                          gradient_penalty_weight):
    """Calculates the gradient penalty loss for a batch of "averaged" samples.
  In Improved WGANs, the 1-Lipschitz constraint is enforced by adding a term to the
  loss function that penalizes the network if the gradient norm moves away from 1.
  However, it is impossible to evaluate this function at all points in the input
  space. The compromise used in the paper is to choose random points on the lines
  between real and generated samples, and check the gradients at these points. Note
  that it is the gradient w.r.t. the input averaged samples, not the weights of the
  discriminator, that we're penalizing!
  In order to evaluate the gradients, we must first run samples through the generator
  and evaluate the loss. Then we get the gradients of the discriminator w.r.t. the
  input averaged samples. The l2 norm and penalty can then be calculated for this
  gradient.
  Note that this loss function requires the original averaged samples as input, but
  Keras only supports passing y_true and y_pred to loss functions. To get around this,
  we make a partial() of the function with the averaged_samples argument, and use that
  for model training."""
    # first get the gradients:
    #   assuming: - that y_pred has dimensions (batch_size, 1)
    #             - averaged_samples has dimensions (batch_size, nbr_features)
    # gradients afterwards has dimension (batch_size, nbr_features), basically
    # a list of nbr_features-dimensional gradient vectors
    gradients = K.gradients(y_pred, averaged_samples)[0]
    # compute the euclidean norm by squaring ...
    gradients_sqr = K.square(gradients)
    #   ... summing over the rows ...
    gradients_sqr_sum = K.sum(gradients_sqr,
                              axis=np.arange(1, len(gradients_sqr.shape)))
    #   ... and sqrt
    gradient_l2_norm = K.sqrt(gradients_sqr_sum)
    # compute lambda * (1 - ||grad||)^2 still for each single sample
    gradient_penalty = gradient_penalty_weight * K.square(1 - gradient_l2_norm)
    # return the mean as loss over all the batch samples
    return K.mean(gradient_penalty)
Exemplo n.º 2
0
def euclidean_distance_angles_biwi(y_true, y_pred):

    diff = y_pred - y_true
    weights = theano.shared(
        np.expand_dims(3 * np.array([0.2, 0.35, 0.45]), axis=0))
    weights = T.patternbroadcast(weights, (True, False))
    diff = diff * weights

    return K.sqrt(K.sum(K.square(diff), axis=-1, keepdims=True))
Exemplo n.º 3
0
def invmass_from_8cartesian_nopy2(x):
    invmass = K.sqrt((x[:, 0:1] + x[:, 4:5])**2 - (x[:, 1:2] + x[:, 5:6])**2 -
                     (x[:, 2:3])**2 - (x[:, 3:4] + x[:, 6:7])**2)
    return invmass
Exemplo n.º 4
0
def root_mean_squared_error(y_true, y_pred):
    return KTF.sqrt(KTF.mean(KTF.square(y_pred - y_true)))
Exemplo n.º 5
0
 def normalize(self, x):
     # utility function to normalize a tensor by its L2 norm
     return x / (KTF.sqrt(KTF.mean(KTF.square(x)) + 1e-5))
Exemplo n.º 6
0
def add_invmass_from_8cartesian(x):
    invmass = K.sqrt((x[:, 0:1] + x[:, 4:5])**2 - (x[:, 1:2] + x[:, 5:6])**2 -
                     (x[:, 2:3] + x[:, 6:7])**2 - (x[:, 3:4] + x[:, 7:8])**2)
    return K.concatenate([x, invmass])
Exemplo n.º 7
0
    def get_updates(self, params, loss):
        grads = self.get_gradients(loss, params)
        self.updates = [K.update_add(self.iterations, 1)]

        lr = self.lr
        if self.initial_decay > 0:
            lr *= (1. /
                   (1. +
                    self.decay * K.cast(self.iterations, K.dtype(self.decay))))

        t = K.cast(self.iterations, K.floatx()) + 1
        lr_t = lr * K.sqrt(1. - K.pow(self.beta_2, t)) / (
            1. - K.pow(self.beta_1, t))

        shapes = [K.get_variable_shape(p) for p in params]
        ms = [K.zeros(shape) for shape in shapes]
        vs = [K.zeros(shape) for shape in shapes]
        self.weights = [self.iterations] + ms + vs

        for p, g, m, v in zip(params, grads, ms, vs):

            # if a weight tensor (len > 1) use weight normalized parameterization
            # this is the only part changed w.r.t. keras.optimizers.Adam
            ps = K.get_variable_shape(p)
            if len(ps) > 1:

                # get weight normalization parameters
                V, V_norm, V_scaler, g_param, grad_g, grad_V = get_weightnorm_params_and_grads(
                    p, g)

                # Adam containers for the 'g' parameter
                V_scaler_shape = K.get_variable_shape(V_scaler)
                m_g = K.zeros(V_scaler_shape)
                v_g = K.zeros(V_scaler_shape)

                # update g parameters
                m_g_t = (self.beta_1 * m_g) + (1. - self.beta_1) * grad_g
                v_g_t = (self.beta_2 *
                         v_g) + (1. - self.beta_2) * K.square(grad_g)
                new_g_param = g_param - lr_t * m_g_t / (K.sqrt(v_g_t) +
                                                        self.epsilon)
                self.updates.append(K.update(m_g, m_g_t))
                self.updates.append(K.update(v_g, v_g_t))

                # update V parameters
                m_t = (self.beta_1 * m) + (1. - self.beta_1) * grad_V
                v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(grad_V)
                new_V_param = V - lr_t * m_t / (K.sqrt(v_t) + self.epsilon)
                self.updates.append(K.update(m, m_t))
                self.updates.append(K.update(v, v_t))

                # if there are constraints we apply them to V, not W
                # if p in constraints:
                #     c = constraints[p]
                #     new_V_param = c(new_V_param)

                # wn param updates --> W updates
                add_weightnorm_param_updates(self.updates, new_V_param,
                                             new_g_param, p, V_scaler)

            else:  # do optimization normally
                m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
                v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g)
                p_t = p - lr_t * m_t / (K.sqrt(v_t) + self.epsilon)

                self.updates.append(K.update(m, m_t))
                self.updates.append(K.update(v, v_t))

                new_p = p_t
                # apply constraints
                # if p in constraints:
                #     c = constraints[p]
                #     new_p = c(new_p)
                self.updates.append(K.update(p, new_p))
        return self.updates
Exemplo n.º 8
0
 def loss_func(y_true, y_pred):
     return ktf.sqrt(ktf.mean(ktf.square(y_pred - y_true)))