Ejemplo n.º 1
0
    def get_updates(self, params, constraints, 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 * self.iterations))

        t = self.iterations + 1
        lr_t = lr / (1. - K.pow(self.beta_1, t))

        shapes = [K.int_shape(p) for p in params]
        # zero init of 1st moment
        ms = [K.zeros(shape) for shape in shapes]
        # zero init of exponentially weighted infinity norm
        us = [K.zeros(shape) for shape in shapes]
        self.weights = [self.iterations] + ms + us

        for p, g, m, u in zip(params, grads, ms, us):

            m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
            u_t = K.maximum(self.beta_2 * u, K.abs(g))
            p_t = p - lr_t * m_t / (u_t + self.epsilon)

            self.updates.append(K.update(m, m_t))
            self.updates.append(K.update(u, u_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
Ejemplo n.º 2
0
  def get_updates(self, loss, params):
    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 / (1. - K.pow(self.beta_1, t))

    shapes = [K.int_shape(p) for p in params]
    # zero init of 1st moment
    ms = [K.zeros(shape) for shape in shapes]
    # zero init of exponentially weighted infinity norm
    us = [K.zeros(shape) for shape in shapes]
    self.weights = [self.iterations] + ms + us

    for p, g, m, u in zip(params, grads, ms, us):

      m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
      u_t = K.maximum(self.beta_2 * u, K.abs(g))
      p_t = p - lr_t * m_t / (u_t + self.epsilon)

      self.updates.append(K.update(m, m_t))
      self.updates.append(K.update(u, u_t))
      new_p = p_t

      # Apply constraints.
      if getattr(p, 'constraint', None) is not None:
        new_p = p.constraint(new_p)

      self.updates.append(K.update(p, new_p))
    return self.updates
Ejemplo n.º 3
0
def hinge(y_true, y_pred):
  return K.mean(K.maximum(1. - y_true * y_pred, 0.), axis=-1)
Ejemplo n.º 4
0
def balanced_squared_hinge(y_true, y_pred):
  postive = K.cumsum(y_true-0.)
  negtive = K.cumsum(1.-y_true)
  posrate = postive/(postive+negtive)
  negrate = negtive/(postive+negtive)
  return K.mean(K.square(K.maximum((1. - y_pred) * y_true, 0.)), axis=-1)*negrate + K.mean(K.square(K.maximum((1. - y_true) * y_pred, 0.)), axis=-1)*posrate
Ejemplo n.º 5
0
def squared_hinge(y_true, y_pred):
  return K.mean(K.square(K.maximum(1. - y_true * y_pred, 0.)), axis=-1)
 def _merge_function(self, inputs):
   output = inputs[0]
   for i in range(1, len(inputs)):
     output = K.maximum(output, inputs[i])
   return output
Ejemplo n.º 7
0
def iou_accuracy(y_true, y_pred):
  i = K.cast(K.cumsum(K.maximum(y_true*K.round(y_pred), 0.)), K.floatx())
  u = K.cast(K.cumsum(K.maximum(y_true+K.round(y_pred), 0.)), K.floatx())
  return i/u
Ejemplo n.º 8
0
def categorical_hinge(y_true, y_pred):
    pos = K.sum(y_true * y_pred, axis=-1)
    neg = K.max((1. - y_true) * y_pred, axis=-1)
    return K.maximum(neg - pos + 1., 0.)
Ejemplo n.º 9
0
def categorical_hinge(y_true, y_pred):
  pos = K.sum(y_true * y_pred, axis=-1)
  neg = K.max((1. - y_true) * y_pred, axis=-1)
  return K.maximum(neg - pos + 1., 0.)
Ejemplo n.º 10
0
def hinge(y_true, y_pred):
  return K.mean(K.maximum(1. - y_true * y_pred, 0.), axis=-1)
Ejemplo n.º 11
0
def squared_hinge(y_true, y_pred):
  return K.mean(K.square(K.maximum(1. - y_true * y_pred, 0.)), axis=-1)