Exemplo n.º 1
0
def test_power_arg0():
    # the +1.'s here are to avoid regimes where numerical diffs fail
    make_fun = lambda y: lambda x: np.power(x, y)
    fun = make_fun(npr.randn()**2 + 1.)
    check_grads(fun)(npr.rand()**2 + 1.)

    # test y == 0. as a special case, c.f. #116
    fun = make_fun(0.)
    assert grad(fun)(0.) == 0.
    assert grad(grad(fun))(0.) == 0.
    def train_loss(wb_vect, unflattener, i):
        wb_struct = unflattener(wb_vect)
        preds = predict(wb_struct, graph_arr, nodes_nbrs_compressed,
                        graph_idxs, layers)
        graph_scores = get_actual(graph_idxs, df, preds)
        mse = np.mean(np.power(preds - graph_scores, 2))

        train_losses.append(mse)
        preds_iter.append(preds)
        actual_iter.append(graph_scores)
        gc.collect()
        return mse
Exemplo n.º 3
0
def train_loss(wb_vect, unflattener, cv=False, batch=True, batch_size=10,
               debug=False):
    """
    Training loss is MSE.

    We pass in a flattened parameter vector and its unflattener.
    """
    wb_struct = unflattener(wb_vect)

    if batch:
        batch_size = batch_size
    else:
        batch_size = len(graphs)

    if cv:
        samp_graphs, samp_inputs = batch_sample(test_graphs,
                                                input_shape,
                                                batch_size=batch_size)
    else:
        samp_graphs, samp_inputs = batch_sample(graphs,
                                                input_shape,
                                                batch_size=batch_size)

    print('batch size: {0}'.format(len(samp_graphs)))
    preds = predict(wb_struct, samp_inputs, samp_graphs)
    graph_ids = [g.graph['seqid'] for g in samp_graphs]
    graph_scores = drug_data.set_index('seqid').ix[graph_ids]['FPV'].values.\
        reshape(preds.shape)

    assert preds.shape == graph_scores.shape

    mse = np.mean(np.power(preds - graph_scores, 2))

    if debug:
        print(graph_ids)
        print('Predictions:')
        print(preds)
        print('Mean: {0}'.format(np.mean(preds)))
        print('')
        print('Actual')
        print(graph_scores)
        print('Mean: {0}'.format(np.mean(graph_scores)))
        print('')
        print('Difference')
        print(preds - graph_scores)
        print('Mean Squared Error: {0}'.format(mse))
        print('')

    return mse
Exemplo n.º 4
0
def integrate_rkdp5(
    rhs,
    x_eval,
    x_initial,
    y_initial,
    atol=1e-12,
    rtol=0.,
    step_safety_factor=0.9,
    step_update_factor_max=10,
    step_update_factor_min=2e-1,
):
    """
    Integrate using the RKDP5(4) method. For quick intuition, consult [2] and [3].
    See table 5.2 on pp. 178 of [1] or [3] for the Butcher tableau. See pp. 167-169 of [1]
    for automatic step size control and starting step size. Scipy's RK45 implementation
    in python [4] was used as a reference for this implementation.

    References:
    [1] E. Hairer, S.P. Norsett and G. Wanner, Solving Ordinary Differential Equations
    i. Nonstiff Problems. 2nd edition. Springer Series in Computational Mathematics,
    Springer-Verlag (1993)
    [2] https://en.wikipedia.org/wiki/Runge–Kutta_methods
    [3] https://en.wikipedia.org/wiki/Dormand–Prince_method
    [4] https://github.com/scipy/scipy/blob/master/scipy/integrate/_ivp/rk.py
    [5] https://math.stackexchange.com/questions/2947231/how-can-i-derive-the-dense-output-of-ode45/2948244
    
    Arguments:
    atol :: float or array(N) - the absolute tolerance of the component-wise
        local error, i.e. "Atoli" in e.q. 4.10 on pp. 167 of [1]
    rhs :: (x :: float, y :: array(N)) -> dy_dx :: array(N)
        - the right-hand side of the equation dy_dx = rhs(x, y)
        that defines the first order differential equation
    rtol :: float or array(N) - the relative tolerance of the component-wise
        local error, i.e. "Rtoli" in e.q. 4.10 on pp. 167 of [1]
    step_safety_factor :: float - the safety multiplication factor used in the
        step update rule, i.e. "fac" in e.q. 4.13 on pp. 168 of [1]
    step_update_factor_max :: float - the maximum step multiplication factor used in the
        step update rule, i.e. "facmax" in e.q. 4.13 on pp. 168 of [1]
    step_update_factor_min :: float - the minimum step multiplication factor used in the
        step update rule, i.e. "facmin"in e.q.e 4.13 on pp. 168 of [1]
    x_eval :: ndarray (eval_count) - an array of points `x` whose
        corresponding `y` value should be evaluated. It is assumed
        that this list does not contain duplicates, that
        the values are sorted in increasing order, and that
        all values are greater than `x_initial`.
    x_final :: float - the final value of x (inclusive) that concludes the integration interval
    x_initial :: float - the initial value of x (inclusive) that begins the integration interval
    y_initial :: array(N) - the initial value of y

    Returns:
    y_evald :: ndarray (eval_count x N) - an array of points `y` whose
        corresponding `x` value is specified in x_eval
    """
    # Determine how far to integrate to.
    if len(x_eval) == 0:
        raise ValueError("No output was specified.")
    else:
        x_final = x_eval[-1]

    # Compute initial step size per pp. 169 of [1].
    f0 = rhs(x_initial, y_initial)
    d0 = rms_norm(y_initial)
    d1 = rms_norm(f0)
    if d0 < 1e-5 or d1 < 1e-5:
        h0 = 1e-6
    else:
        h0 = 0.01 * d0 / d1
    y1 = y_initial + h0 * f0
    f1 = rhs(x_initial + h0, y1)
    d2 = rms_norm(f1 - f0) / h0
    if anp.maximum(d1, d2) <= 1e-15:
        h1 = anp.maximum(1e-6, h0 * 1e-3)
    else:
        h1 = anp.power(0.01 / anp.maximum(d1, d2), 1 / (P + 1))
    step_current = anp.minimum(100 * h0, h1)

    # Integrate.
    y_eval_list = list()
    x_current = x_initial
    y_current = y_initial
    k1 = f0
    while x_current <= x_final:
        step_rejected = False
        step_accepted = False
        # Repeatedly attempt to move to the next position in the mesh
        # until the step size is adapted such that the local step error
        # is within an acceptable tolerance.
        while not step_accepted:
            # Attempt to step by `step_current`.
            ks, y1, y1h = integrate_rkdp5_step(step_current,
                                               rhs,
                                               x_current,
                                               y_current,
                                               k1=k1)
            # Before the step size is updated for the next step, note where
            # the current attempted step size places us in the mesh.
            x_new = x_current + step_current
            # Compute the local error associated with the attempted step.
            scale = atol + anp.maximum(anp.abs(y1), anp.abs(y1h)) * rtol
            error_norm = rms_norm((y1 - y1h) / scale)

            # If the step is accepted, increase the step size,
            # and move to the next step.
            if error_norm < 1:
                step_accepted = True
                # Avoid division by zero in update.
                if error_norm == 0:
                    step_update_factor = step_update_factor_max
                else:
                    step_update_factor = anp.minimum(
                        step_update_factor_max,
                        step_safety_factor * anp.power(error_norm, ERROR_EXP))
                # Avoid an extraneous update in next step.
                if step_rejected:
                    step_update_factor = anp.minimum(1, step_update_factor)
                step_current = step_current * step_update_factor
            # If the step was rejected, decrease the step size,
            # and reattempt the step.
            else:
                step_rejected = True
                step_update_factor = anp.maximum(
                    step_update_factor_min,
                    step_safety_factor * anp.power(error_norm, ERROR_EXP))
                step_current = step_current * step_update_factor
        #ENDWHILE
        # Interpolate any output points that ocurred in the step.
        x_eval_step_indices = anp.nonzero(
            anp.logical_and(x_current <= x_eval, x_eval <= x_new))[0]
        x_eval_step = x_eval[x_eval_step_indices]
        if len(x_eval_step) != 0:
            y_eval_step = rkdp5_dense(ks, x_current, x_new, x_eval_step,
                                      y_current, y1)
            for y_eval_ in y_eval_step:
                y_eval_list.append(y_eval_)

        # Update the position in the mesh.
        x_current = x_new
        y_current = y1
        k1 = ks[6]  # k[6] = k7
    #ENDWHILE

    return anp.stack(y_eval_list)
Exemplo n.º 5
0
def hillPlus(A, k, n):
    return np.power(A, n) / (np.power(A, n) + np.power(k, n))
Exemplo n.º 6
0
from __future__ import absolute_import

import autograd.numpy as np
import scipy.stats
from autograd.extend import primitive, defvjp
from autograd.numpy.numpy_vjps import unbroadcast_f
from autograd.scipy.special import gamma, psi

cdf = primitive(scipy.stats.gamma.cdf)
logpdf = primitive(scipy.stats.gamma.logpdf)
pdf = primitive(scipy.stats.gamma.pdf)

def grad_gamma_logpdf_arg0(x, a):
    return (a - x - 1) / x

def grad_gamma_logpdf_arg1(x, a):
    return np.log(x) - psi(a)

defvjp(cdf, lambda ans, x, a: unbroadcast_f(x, lambda g: g * np.exp(-x) * np.power(x, a-1) / gamma(a)), argnums=[0])
defvjp(logpdf,
       lambda ans, x, a: unbroadcast_f(x, lambda g: g * grad_gamma_logpdf_arg0(x, a)),
       lambda ans, x, a: unbroadcast_f(a, lambda g: g * grad_gamma_logpdf_arg1(x, a)))
defvjp(pdf,
       lambda ans, x, a: unbroadcast_f(x, lambda g: g * ans * grad_gamma_logpdf_arg0(x, a)),
       lambda ans, x, a: unbroadcast_f(a, lambda g: g * ans * grad_gamma_logpdf_arg1(x, a)))
Exemplo n.º 7
0
def rank_2_B_update(B,y,s,c):
    normalizer = np.dot(s,c)
    temp = np.outer(y - np.matmul(B, s), np.transpose(c))
    symmetric_term = (temp + np.transpose(temp)) /normalizer
    residual_term = (np.dot(np.transpose(s), y-np.matmul(B,s) ) * np.outer(c,np.transpose(c)))/np.power(normalizer, 2)
    return B + symmetric_term - residual_term
Exemplo n.º 8
0
 def _cfun(self, d):
     return np.power(d,2) / (2 * (d + 1))
Exemplo n.º 9
0
from __future__ import absolute_import
import scipy.special
import autograd.numpy as np
from autograd.extend import primitive, defvjp
from autograd.numpy.numpy_vjps import unbroadcast_f

### Beta function ###
beta    = primitive(scipy.special.beta)
betainc = primitive(scipy.special.betainc)
betaln  = primitive(scipy.special.betaln)

defvjp(beta,
       lambda ans, a, b: unbroadcast_f(a, lambda g: g * ans * (psi(a) - psi(a + b))),
       lambda ans, a, b: unbroadcast_f(b, lambda g: g * ans * (psi(b) - psi(a + b))))
defvjp(betainc,
       lambda ans, a, b, x: unbroadcast_f(x, lambda g: g * np.power(x, a - 1) * np.power(1 - x, b - 1) / beta(a, b)),
       argnums=[2])
defvjp(betaln,
       lambda ans, a, b: unbroadcast_f(a, lambda g: g * (psi(a) - psi(a + b))),
       lambda ans, a, b: unbroadcast_f(b, lambda g: g * (psi(b) - psi(a + b))))

### Gamma functions ###
polygamma    = primitive(scipy.special.polygamma)
psi          = primitive(scipy.special.psi)        # psi(x) is just polygamma(0, x)
digamma      = primitive(scipy.special.digamma)    # digamma is another name for psi.
gamma        = primitive(scipy.special.gamma)
gammaln      = primitive(scipy.special.gammaln)
gammainc     = primitive(scipy.special.gammainc)
gammaincc    = primitive(scipy.special.gammaincc)
gammasgn     = primitive(scipy.special.gammasgn)
rgamma       = primitive(scipy.special.rgamma)
Exemplo n.º 10
0
def test_power_arg1_zero():
    fun = lambda y: np.power(0., y)
    check_grads(fun)(npr.rand()**2)
Exemplo n.º 11
0
def test_power_arg1_zero():
    fun = lambda y : np.power(0., y)
    d_fun = grad(fun)
    check_grads(fun, npr.rand()**2)
    check_grads(d_fun, npr.rand()**2)
Exemplo n.º 12
0
def norm2(x):
    # if np.ndim(x) > 1:
    #     raise ValueError("Expected dimension greater than 2, but received")
    # else:
    return np.sqrt(np.sum(np.power(x, 2)))
Exemplo n.º 13
0
def test_power_arg1():
    x = npr.randn()**2
    fun = lambda y: np.power(x, y)
    check_grads(fun)(npr.rand()**2)
Exemplo n.º 14
0
    def populate_coordinates(self):
        # Populates a variable called self.coordinates with the coordinates of the airfoil.
        name = self.name.lower().strip()

        # If it's a NACA 4-series airfoil, try to generate it
        if "naca" in name:
            nacanumber = name.split("naca")[1]
            if nacanumber.isdigit():
                if len(nacanumber) == 4:

                    # Parse
                    max_camber = int(nacanumber[0]) * 0.01
                    camber_loc = int(nacanumber[1]) * 0.1
                    thickness = int(nacanumber[2:]) * 0.01

                    # Set number of points per side
                    n_points_per_side = 100

                    # Referencing https://en.wikipedia.org/wiki/NACA_airfoil#Equation_for_a_cambered_4-digit_NACA_airfoil
                    # from here on out

                    # Make uncambered coordinates
                    x_t = cosspace(n_points=n_points_per_side
                                   )  # Generate some cosine-spaced points
                    y_t = 5 * thickness * (
                        +0.2969 * np.power(x_t, 0.5) - 0.1260 * x_t -
                        0.3516 * np.power(x_t, 2) + 0.2843 * np.power(x_t, 3) -
                        0.1015 * np.power(
                            x_t, 4)  # 0.1015 is original, #0.1036 for sharp TE
                    )

                    if camber_loc == 0:
                        camber_loc = 0.5  # prevents divide by zero errors for things like naca0012's.

                    # Get camber
                    y_c_piece1 = max_camber / camber_loc**2 * (
                        2 * camber_loc * x_t[x_t <= camber_loc] -
                        x_t[x_t <= camber_loc]**2)
                    y_c_piece2 = max_camber / (1 - camber_loc)**2 * (
                        (1 - 2 * camber_loc) + 2 * camber_loc *
                        x_t[x_t > camber_loc] - x_t[x_t > camber_loc]**2)
                    y_c = np.hstack((y_c_piece1, y_c_piece2))

                    # Get camber slope
                    dycdx_piece1 = 2 * max_camber / camber_loc**2 * (
                        camber_loc - x_t[x_t <= camber_loc])
                    dycdx_piece2 = 2 * max_camber / (1 - camber_loc)**2 * (
                        camber_loc - x_t[x_t > camber_loc])
                    dycdx = np.hstack((dycdx_piece1, dycdx_piece2))
                    theta = np.arctan(dycdx)

                    # Combine everything
                    x_U = x_t - y_t * np.sin(theta)
                    x_L = x_t + y_t * np.sin(theta)
                    y_U = y_c + y_t * np.cos(theta)
                    y_L = y_c - y_t * np.cos(theta)

                    # Flip upper surface so it's back to front
                    x_U, y_U = np.flipud(x_U), np.flipud(y_U)

                    # Trim 1 point from lower surface so there's no overlap
                    x_L, y_L = x_L[1:], y_L[1:]

                    x = np.hstack((x_U, x_L))
                    y = np.hstack((y_U, y_L))

                    coordinates = np.column_stack((x, y))

                    self.coordinates = coordinates
                    return
                else:
                    print(
                        "Unfortunately, only 4-series NACA airfoils can be generated at this time."
                    )

        # Try to read from airfoil database
        try:
            import importlib.resources
            from . import airfoils
            raw_text = importlib.resources.read_text(airfoils, name + '.dat')
            trimmed_text = raw_text[raw_text.find('\n'):]

            coordinates1D = np.fromstring(
                trimmed_text,
                sep='\n')  # returns the coordinates in a 1D array
            assert len(
                coordinates1D
            ) % 2 == 0, 'File was found in airfoil database, but it could not be read correctly!'  # Should be even

            coordinates = np.reshape(coordinates1D, (-1, 2))
            self.coordinates = coordinates
            return

        except FileNotFoundError:
            print("File was not found in airfoil database!")
Exemplo n.º 15
0
 def gammainc_vjp_arg1(ans, a, x):
     coeffs = sign * np.exp(-x) * np.power(x, a - 1) / gamma(a)
     return unbroadcast_f(x, lambda g: g * coeffs)
Exemplo n.º 16
0
from autograd.numpy.numpy_vjps import unbroadcast_f

### Beta function ###
beta = primitive(scipy.special.beta)
betainc = primitive(scipy.special.betainc)
betaln = primitive(scipy.special.betaln)

defvjp(
    beta,
    lambda ans, a, b: unbroadcast_f(a, lambda g: g * ans *
                                    (psi(a) - psi(a + b))),
    lambda ans, a, b: unbroadcast_f(b, lambda g: g * ans *
                                    (psi(b) - psi(a + b))))
defvjp(betainc,
       lambda ans, a, b, x: unbroadcast_f(
           x, lambda g: g * np.power(x, a - 1) * np.power(1 - x, b - 1) / beta(
               a, b)),
       argnums=[2])
defvjp(betaln,
       lambda ans, a, b: unbroadcast_f(a, lambda g: g * (psi(a) - psi(a + b))),
       lambda ans, a, b: unbroadcast_f(b, lambda g: g * (psi(b) - psi(a + b))))

### Gamma functions ###
polygamma = primitive(scipy.special.polygamma)
psi = primitive(scipy.special.psi)  # psi(x) is just polygamma(0, x)
digamma = primitive(scipy.special.digamma)  # digamma is another name for psi.
gamma = primitive(scipy.special.gamma)
gammaln = primitive(scipy.special.gammaln)
gammainc = primitive(scipy.special.gammainc)
gammaincc = primitive(scipy.special.gammaincc)
gammasgn = primitive(scipy.special.gammasgn)
Exemplo n.º 17
0
def get_learning_rate_exp_decay(global_step, base_lr=0.01, decay_rate=0.9, decay_steps=2, staircase=True):
    if staircase:
        exponent = global_step / decay_steps # integer division
    else:
        exponent = global_step / np.float(decay_steps) 
    return base_lr * np.power(decay_rate, exponent)
Exemplo n.º 18
0
 def _calc_pareto_front(self, n_pareto_points=100):
     x = anp.linspace(0, 1, n_pareto_points)
     return anp.array([x, 1 - anp.power(x, 2)]).T
Exemplo n.º 19
0
def ll(x, num_peds, ess, robot_mu_x, robot_mu_y, ped_mu_x, ped_mu_y, \
       cov_robot_x, cov_robot_y, inv_cov_robot_x, inv_cov_robot_y, \
       cov_ped_x, cov_ped_y, inv_cov_ped_x, inv_cov_ped_y, \
       one_over_cov_sum_x, one_over_cov_sum_y, \
       one_over_cov_sumij_x, one_over_cov_sumij_y, normalize, T):

  quad_mu_x = np.dot((x[:T]-robot_mu_x).T, np.dot(inv_cov_robot_x, \
                                                              x[:T]-robot_mu_x))
  quad_mu_y = np.dot((x[T:2*T]-robot_mu_y).T, np.dot(inv_cov_robot_y, \
                                                           x[T:2*T]-robot_mu_y))
  llambda = -0.5*quad_mu_x - 0.5*quad_mu_y

  i = 2
  for ped in range(ess):
    quad_mu_x = np.dot((x[i*T:(i+1)*T]-ped_mu_x[ped]).T, np.dot(\
                            inv_cov_ped_x[ped], x[i*T:(i+1)*T]-ped_mu_x[ped]))
    quad_mu_y = np.dot((x[(i+1)*T:(i+2)*T]-ped_mu_y[ped]).T, np.dot(\
                        inv_cov_ped_y[ped], x[(i+1)*T:(i+2)*T]-ped_mu_y[ped]))
    llambda = llambda -0.5*quad_mu_x - 0.5*quad_mu_y
    i = i + 2

  i = 2#robot-agent CCA
  for ped in range(ess):
    vel_x = np.tile(x[:T],(T,1)).T - np.tile(x[i*T:(i+1)*T], (T,1))
    vel_y = np.tile(x[T:2*T],(T,1)).T - np.tile(x[(i+1)*T:(i+2)*T],(T,1))
    vel_x = np.diag(vel_x)
    vel_y = np.diag(vel_y)
    i = i + 2

    vel_x_2 = np.power(vel_x, 2)
    vel_y_2 = np.power(vel_y, 2)

    quad_x = np.multiply(vel_x_2, one_over_cov_sum_x[ped])
    quad_y = np.multiply(vel_y_2, one_over_cov_sum_y[ped])

    Z_x = np.exp(-0.5*quad_x)
    Z_y = np.exp(-0.5*quad_y)
    Z = np.multiply(Z_x, Z_y)

    log_znot_norm = np.sum(np.log1p(-Z))

    llambda = llambda + log_znot_norm
  # agent-agent CCA
  i = 2
  j = 4
  for ped_i in range(ess):
    ped_j = int(j/2) - 1
    while j<=2*ess:
      vel_x = np.tile(\
                 x[i*T:(i+1)*T],(T,1)).T - np.tile(x[(j)*T:(j+1)*T], (T,1))
      vel_y = np.tile(\
                 x[(i+1)*T:(i+2)*T],(T,1)).T - np.tile(x[(j+1)*T:(j+2)*T],(T,1))
      vel_x = np.diag(vel_x)
      vel_y = np.diag(vel_y)
      vel_x_2 = np.power(vel_x, 2)
      vel_y_2 = np.power(vel_y, 2)

      quad_x = np.multiply(vel_x_2, one_over_cov_sumij_x[ped_i][ped_j])
      quad_y = np.multiply(vel_y_2, one_over_cov_sumij_y[ped_i][ped_j])

      Z_x = np.exp(-0.5*quad_x)
      Z_y = np.exp(-0.5*quad_y)
      Z = np.multiply(Z_x, Z_y)
      log_znot_norm = np.sum(np.log1p(-Z))

      llambda = llambda + log_znot_norm
      j = j + 2
    i = i + 2
    j = i + 2
  return -1.*llambda
Exemplo n.º 20
0
def dd_ll(x, num_peds, ess, robot_mu_x, robot_mu_y, ped_mu_x, ped_mu_y, \
          cov_robot_x, cov_robot_y, inv_cov_robot_x, inv_cov_robot_y, \
          cov_ped_x, cov_ped_y, inv_cov_ped_x, inv_cov_ped_y, \
          one_over_cov_sum_x, one_over_cov_sum_y, normalize):
    T = np.size(robot_mu_x)

    H = np.zeros((2 * T * np.int(ess + 1), 2 * T * np.int(ess + 1)), float)
    sum_d_alpha = [0. for _ in range(2 * T * np.int(np.round(ess + 1)))]

    n = 2
    for ped in range(ess):
        # if normalize == True:
        #   # normalize_x = np.multiply(np.power(2*np.pi,-0.5), \
        # one_over_std_sum_x[ped])
        #   # normalize_y = np.multiply(np.power(2*np.pi,-0.5), \
        # one_over_std_sum_y[ped])
        # else:
        normalize_x = 1.
        normalize_y = 1.

        vel_robot_x = np.tile(x[:T],
                              (T, 1)).T - np.tile(x[n * T:(n + 1) * T], (T, 1))
        vel_robot_y = np.tile(x[T:2 * T],
                              (T, 1)).T - np.tile(x[(n + 1) * T:(n + 2) * T],
                                                  (T, 1))
        vel_robot_x_2 = np.power(vel_robot_x, 2)
        vel_robot_y_2 = np.power(vel_robot_y, 2)
        vel_robot_x_y = np.multiply(vel_robot_x, vel_robot_y)

        one_over_cov_x_y = np.multiply(one_over_cov_sum_x[ped], \
                                                            one_over_cov_sum_y[ped])
        quad_robot_x = np.multiply(one_over_cov_sum_x[ped], vel_robot_x_2)
        quad_robot_y = np.multiply(one_over_cov_sum_y[ped], vel_robot_y_2)

        Z_x = np.multiply(normalize_x, np.exp(-0.5 * quad_robot_x))
        Z_y = np.multiply(normalize_y, np.exp(-0.5 * quad_robot_y))

        Z = np.multiply(Z_x, Z_y)
        X = np.divide(Z, 1. - Z)
        X_2 = np.power(X, 2)

        X_plus_X2 = np.add(X, X_2)

        d_alpha_x = np.multiply(X, one_over_cov_sum_x[ped])
        d_alpha_x = np.add(d_alpha_x, -np.multiply(X_plus_X2, np.power(\
                             np.multiply(vel_robot_x, one_over_cov_sum_x[ped]), 2)))
        d_alpha_y = np.multiply(X, one_over_cov_sum_y[ped])
        d_alpha_y = np.add(d_alpha_y, -np.multiply(X_plus_X2, np.power(\
                             np.multiply(vel_robot_y, one_over_cov_sum_y[ped]), 2)))

        sum_d_alpha[:T] = np.add(sum_d_alpha[:T], np.sum(d_alpha_x, axis=1))
        sum_d_alpha[T:2 * T] = np.add(sum_d_alpha[T:2 * T],
                                      np.sum(d_alpha_y, axis=1))

        d_off_alpha = -np.multiply(X_plus_X2, np.multiply(vel_robot_x_y, \
                                                                  one_over_cov_x_y))
        #   OFF DIAGONALS
        H[:T, T:2 * T] = np.add(H[:T, T:2 * T],
                                np.diag(np.sum(d_off_alpha, axis=1)))

        H[:T, n * T:(n + 1) * T] = -1. * d_alpha_x
        H[n * T:(n + 1) * T, :T] = H[:T, n * T:(n + 1) * T].T

        H[T:2 * T, (n + 1) * T:(n + 2) * T] = -1. * d_alpha_y
        H[(n + 1) * T:(n + 2) * T, T:2 * T] = H[T:2 * T,
                                                (n + 1) * T:(n + 2) * T].T

        H[T:2*T,n*T:(n+1)*T] = np.multiply(X_plus_X2, np.multiply(vel_robot_x_y, \
                                                                  one_over_cov_x_y))
        H[n * T:(n + 1) * T, T:2 * T] = H[T:2 * T, n * T:(n + 1) * T].T

        H[:T,(n+1)*T:(n+2)*T] = np.multiply(X_plus_X2, np.multiply(vel_robot_x_y, \
                                                                  one_over_cov_x_y))
        H[(n + 1) * T:(n + 2) * T, :T] = H[:T, (n + 1) * T:(n + 2) * T].T

        n = n + 2

    H[:T, :T] = np.add(np.diag(sum_d_alpha[:T]), -1. * inv_cov_robot_x)
    H[T:2 * T, T:2 * T] = np.add(np.diag(sum_d_alpha[T:2 * T]),
                                 -1. * inv_cov_robot_y)

    H[T:2 * T, :T] = H[:T, T:2 * T].T
    #      PED DIAGONALS
    n = 2
    for ped in range(ess):
        # if normalize == True:
        #   # normalize_x = np.multiply(np.power(2*np.pi,-0.5), \
        # one_over_std_sum_x[ped])
        #   # normalize_y = np.multiply(np.power(2*np.pi,-0.5), \
        # one_over_std_sum_y[ped])
        # else:
        normalize_x = 1.
        normalize_y = 1.

        vel_ped_x = np.tile(x[:T], (T, 1)) - np.tile(x[n * T:(n + 1) * T],
                                                     (T, 1)).T
        vel_ped_y = np.tile(x[T:2 * T],
                            (T, 1)) - np.tile(x[(n + 1) * T:(n + 2) * T],
                                              (T, 1)).T
        vel_ped_x_2 = np.power(vel_ped_x, 2)
        vel_ped_y_2 = np.power(vel_ped_y, 2)
        vel_ped_x_y = np.multiply(vel_ped_x, vel_ped_y)

        one_over_cov_x_y = np.multiply(one_over_cov_sum_x[ped], \
                                                            one_over_cov_sum_y[ped])
        quad_ped_x = np.multiply(one_over_cov_sum_x[ped], vel_ped_x_2)
        quad_ped_y = np.multiply(one_over_cov_sum_y[ped], vel_ped_y_2)

        Z_x = np.multiply(normalize_x, np.exp(-0.5 * quad_ped_x))
        Z_y = np.multiply(normalize_y, np.exp(-0.5 * quad_ped_y))

        Z = np.multiply(Z_x, Z_y)
        X = np.divide(Z, 1. - Z)
        X_2 = np.power(X, 2)

        X_plus_X2 = np.add(X, X_2)

        d_alpha_x = np.multiply(X, one_over_cov_sum_x[ped])
        d_alpha_x = np.add(d_alpha_x, -np.multiply(X_plus_X2, np.power(\
                               np.multiply(vel_ped_x, one_over_cov_sum_x[ped]), 2)))
        d_alpha_y = np.multiply(X, one_over_cov_sum_y[ped])
        d_alpha_y = np.add(d_alpha_y, -np.multiply(X_plus_X2, np.power(\
                               np.multiply(vel_ped_y, one_over_cov_sum_y[ped]), 2)))

        H[n*T:(n+1)*T,n*T:(n+1)*T] = np.diag(np.sum(d_alpha_x, axis=1)) - \
                                                                  inv_cov_ped_x[ped]
        H[(n+1)*T:(n+2)*T,(n+1)*T:(n+2)*T] = np.diag(np.sum(d_alpha_y, axis=1)) - \
                                                                  inv_cov_ped_y[ped]
        H[n*T:(n+1)*T,(n+1)*T:(n+2)*T] = -np.diag(np.sum(np.multiply(X_plus_X2, \
                               np.multiply(vel_ped_x_y, one_over_cov_x_y)), axis=1))

        H[(n + 1) * T:(n + 2) * T,
          n * T:(n + 1) * T] = H[n * T:(n + 1) * T, (n + 1) * T:(n + 2) * T].T

        n = n + 2
    return -1. * H
Exemplo n.º 21
0
from __future__ import absolute_import, division

import autograd.numpy as np
import scipy.stats
from autograd.extend import primitive, defvjp
from autograd.numpy.numpy_vjps import unbroadcast_f
from autograd.scipy.special import gamma

cdf = primitive(scipy.stats.chi2.cdf)
logpdf = primitive(scipy.stats.chi2.logpdf)
pdf = primitive(scipy.stats.chi2.pdf)

def grad_chi2_logpdf(x, df):
    return np.where(df % 1 == 0, (df - x - 2) / (2 * x), 0)

defvjp(cdf, lambda ans, x, df: unbroadcast_f(x, lambda g: g * np.power(2., -df/2) * np.exp(-x/2) * np.power(x, df/2 - 1) / gamma(df/2)), argnums=[0])
defvjp(logpdf, lambda ans, x, df: unbroadcast_f(x, lambda g: g * grad_chi2_logpdf(x, df)), argnums=[0])
defvjp(pdf, lambda ans, x, df: unbroadcast_f(x, lambda g: g * ans * grad_chi2_logpdf(x, df)), argnums=[0])
Exemplo n.º 22
0
def d_ll(x, num_peds, ess, robot_mu_x, robot_mu_y, ped_mu_x, ped_mu_y, \
         cov_robot_x, cov_robot_y, inv_cov_robot_x, inv_cov_robot_y, \
         cov_ped_x, cov_ped_y, inv_cov_ped_x, inv_cov_ped_y, \
         one_over_cov_sum_x, one_over_cov_sum_y, normalize):
    T = np.size(robot_mu_x)

    d_alpha = [0. for _ in range(2 * T * np.int(np.round(ess + 1)))]
    d_beta = [0. for _ in range(2 * T * np.int(np.round(ess + 1)))]
    d_llambda = np.asarray(
        [0. for _ in range(2 * T * np.int(np.round(ess + 1)))])

    n = 2
    for ped in range(ess):
        # if normalize == True:
        #   # normalize_x = np.multiply(np.power(2*np.pi,-0.5), \
        # one_over_std_sum_x[ped])
        #   # normalize_y = np.multiply(np.power(2*np.pi,-0.5), \
        # one_over_std_sum_y[ped])
        # else:
        normalize_x = 1.
        normalize_y = 1.

        vel_robot_x = np.tile(x[:T],
                              (T, 1)).T - np.tile(x[n * T:(n + 1) * T], (T, 1))
        vel_robot_y = np.tile(x[T:2 * T],
                              (T, 1)).T - np.tile(x[(n + 1) * T:(n + 2) * T],
                                                  (T, 1))
        n = n + 2

        vel_robot_x_2 = np.power(vel_robot_x, 2)
        vel_robot_y_2 = np.power(vel_robot_y, 2)

        quad_robot_x = np.multiply(one_over_cov_sum_x[ped], vel_robot_x_2)
        quad_robot_y = np.multiply(one_over_cov_sum_y[ped], vel_robot_y_2)

        Z_x = np.multiply(normalize_x, np.exp(-0.5 * quad_robot_x))
        Z_y = np.multiply(normalize_y, np.exp(-0.5 * quad_robot_y))

        Z = np.multiply(Z_x, Z_y)
        X = np.divide(Z, 1. - Z)

        alpha_x = np.multiply(
            X, np.multiply(vel_robot_x, one_over_cov_sum_x[ped]))
        alpha_y = np.multiply(
            X, np.multiply(vel_robot_y, one_over_cov_sum_y[ped]))
        #        X and Y COMPONENT OF R DERIVATIVE
        d_alpha[:T] = np.add(d_alpha[:T], np.sum(alpha_x, axis=1))
        d_alpha[T:2 * T] = np.add(d_alpha[T:2 * T], np.sum(alpha_y, axis=1))

    d_beta[:T] = -np.dot(x[:T] - robot_mu_x, inv_cov_robot_x)
    d_beta[T:2 * T] = -np.dot(x[T:2 * T] - robot_mu_y, inv_cov_robot_y)

    d_llambda[0:2 * T] = np.add(d_alpha[0:2 * T], d_beta[0:2 * T])
    #        X AND Y COMPONENT OF PED DERIVATIVE
    n = 2
    for ped in range(ess):
        # if normalize == True:
        #   # normalize_x = np.multiply(np.power(2*np.pi,-0.5), \
        # one_over_std_sum_x[ped])
        #   # normalize_y = np.multiply(np.power(2*np.pi,-0.5), \
        # one_over_std_sum_y[ped])
        # else:
        normalize_x = 1.
        normalize_y = 1.

        vel_ped_x = np.tile(x[:T], (T, 1)) - np.tile(x[n * T:(n + 1) * T],
                                                     (T, 1)).T
        vel_ped_y = np.tile(x[T:2 * T],
                            (T, 1)) - np.tile(x[(n + 1) * T:(n + 2) * T],
                                              (T, 1)).T
        vel_ped_x_2 = np.power(vel_ped_x, 2)
        vel_ped_y_2 = np.power(vel_ped_y, 2)

        quad_ped_x = np.multiply(one_over_cov_sum_x[ped], vel_ped_x_2)
        quad_ped_y = np.multiply(one_over_cov_sum_y[ped], vel_ped_y_2)

        Z_x = np.multiply(normalize_x, np.exp(-0.5 * quad_ped_x))
        Z_y = np.multiply(normalize_y, np.exp(-0.5 * quad_ped_y))

        Z = np.multiply(Z_x, Z_y)
        X = np.divide(Z, 1. - Z)

        alpha_x = np.multiply(X, np.multiply(vel_ped_x,
                                             one_over_cov_sum_x[ped]))
        alpha_y = np.multiply(X, np.multiply(vel_ped_y,
                                             one_over_cov_sum_y[ped]))

        d_alpha[n * T:(n + 1) * T] = -np.sum(alpha_x, axis=1)
        d_alpha[(n + 1) * T:(n + 2) * T] = -np.sum(alpha_y, axis=1)

        d_beta[n*T:(n+1)*T] = -np.dot(x[n*T:(n+1)*T]-ped_mu_x[ped], \
                                                                 inv_cov_ped_x[ped])
        d_beta[(n+1)*T:(n+2)*T] = -np.dot(x[(n+1)*T:(n+2)*T]-ped_mu_y[ped], \
                                                                 inv_cov_ped_y[ped])
        n = n + 2

    d_llambda[2 * T:] = np.add(d_alpha[2 * T:], d_beta[2 * T:])
    return -1. * d_llambda
Exemplo n.º 23
0
 def _cfun(self, d, pd_la = 1):
     return (np.power(d + 1, pd_la + 1) - (d +1))/(pd_la * (pd_la + 1)) - d / (pd_la + 1)
Exemplo n.º 24
0
def get_learning_rate_inv_scaling(t, base_lr=0.01, power_t=0.25):
    return base_lr / np.power(t + 1, power_t)
Exemplo n.º 25
0
def test_power_arg0():
    y = npr.randn()**2 + 1.0
    fun = lambda x : np.power(x, y)
    d_fun = grad(fun)
    check_grads(fun, npr.rand()**2)
    check_grads(d_fun, npr.rand()**2)
def RKF45_Integrator(t_start, t_stop, h0, x0, A):
    # An integrator using a 4(5) RKF method
    T_0 = time.time()
    """
	x0 = initial conditions
	t_start = start time
	t_stop = end time
	n_step = number of steps
	A = A(t) matrix function
	"""
    Ndim = x0.size
    x_ = np.zeros((1, Ndim))  # set up the array of x values
    t_ = np.zeros(1)  # set up the array of t values
    t_[0] = t_start
    x_[0, :] = x0
    h = h0
    h_min = h0 * (10**(-3))
    h_max = 3 * h0
    n = 0
    t = t_start
    #
    S = 0.99  # safety factor
    #
    while n < n_step:
        x_n = x_[n, :].reshape(Ndim, 1)
        Err_small = False
        h_new = h
        while Err_small == False:
            # compute the predictions using 4th and 5th order RK methods
            k1 = h * A(t) @ x_n
            k2 = h * A(t + 0.25 * h) @ (x_n + 0.25 * k1)
            k3 = h * A(t + (3 / 8) * h) @ (x_n + (3 / 32) * k1 + (9 / 32) * k2)
            k4 = h * A(t + (12 / 13) * h) @ (x_n + (1932 / 2197) * k1 -
                                             (7200 / 2197) * k2 +
                                             (7296 / 2197) * k3)
            k5 = h * A(t + h) @ (x_n + (439 / 216) * k1 - 8 * k2 +
                                 (3680 / 513) * k3 - (845 / 4104) * k4)
            k6 = h * A(t + 0.5 * h) @ (x_n - (8 / 27) * k1 + 2 * k2 -
                                       (3544 / 2565) * k3 +
                                       (1859 / 4104) * k4 - (11 / 40) * k5)
            y_np1 = x_n + (25 / 216) * k1 + (1408 / 2565) * k3 + (
                2197 / 4101) * k4 - (11 / 40) * k5
            z_np1 = x_n + (16 / 135) * k1 + (6656 / 12825) * k3 + (
                28561 / 56430) * k4 - (9 / 50) * k5 + (2 / 55) * k6
            #
            Err = ferr(y_np1, z_np1)
            Err_max = epsilon_RK * np.abs(z_np1[0, 0] + epsilon_RK)
            Err_ratio = np.abs(Err / Err_max)
            #
            if Err_ratio <= 1:
                h_new = h * S * np.power(Err_ratio, -1.0 / (5))
                """
				or h_new = h*(epsilon*h/max(np.abs(y_np1 - z_np1)))**(1/4)
				"""
                if h_new > h_max:  # limit the maximum step size
                    h_new = h_max
                Err_small = True  # break loop
            elif Err_ratio > 1:
                h_new = h * S * np.power(Err_ratio, -1.0 / (4))
                #print(" h_new = ", h_new)
                if h_new < h_min:
                    #print("h < h_min")
                    h_new = h_min
                    Err_small = True  # break loop
                elif h_new >= h_min:
                    h = h_new
        t = t + h
        x_ = np.vstack(
            (x_, x_np1_l.reshape(1,
                                 Ndim)))  # add x_n+1 to the array of x values
        t_ = np.append(t_, t)  # add t_n+1 to the array of t values
        n = n + 1
        h = h_new
        if True:  #np.round(((t-t_start)/(t_stop-t_start))*100000) % 1000 == 0:
            print("\r" + "integrated {:.1%}".format(
                (t - t_start) / (t_stop - t_start)),
                  end='')
    T = time.time() - T_0
    print(" done in {:.5g}s".format(T))
    return (t_, x_, T)
Exemplo n.º 27
0
def test_pow_f64():
  grad_test(lambda x: 0.4 ** x, lambda x: np.power(0.4, x), default_fp=ti.f64)
  grad_test(lambda y: y ** 0.4, lambda y: np.power(y, 0.4), default_fp=ti.f64)
def Magnus_Integrator(t_start, t_stop, h0, x0, A, alpha, order):
    T_0 = time.time()
    """
	x0 = initial conditions
	t_start = start time
	t_stop = end time
	h0 = initial step size
	A = A(t) matrix function
	alpha = alpha generating function
	order = 4 or 6
	"""
    Ndim = x0.size
    x_ = np.zeros((1, Ndim))  # set up the array of x values
    t_ = np.zeros(1)  # set up the array of t values
    t_[0] = t_start
    x_[0, :] = x0
    h = h0
    h_min = h0 * (10**(-3))
    h_max = 3 * h0
    n = 0
    t = t_start
    #
    S = 0.99  # safety factor

    #
    def M(time, hstep):
        M_ = linalg.expm(Omega(time, time + hstep, A, alpha, order))
        return M_

    #
    while t <= t_stop:
        x_n = x_[n, :].reshape(Ndim, 1)
        Err_small = False
        h_new = h
        while Err_small == False:
            # compute the predictions using one step of h & two steps of h/2
            #print("\r" + "trying step " + str(n) + " h=" + str(h) + " ...", end='')
            x_np1_0 = M(t, h) @ x_n
            x_np1_l = M(t + 0.5 * h, 0.5 * h) @ (M(t, 0.5 * h) @ x_n)
            # compute error
            Err = ferr(x_np1_0, x_np1_l)
            Err_max = epsilon * np.abs(
                x_np1_l[0, 0] +
                epsilon)  #h*(A(t) @ x_n)[0,0]) # maximum error allowed
            #Err_ratio = Err / Err_max
            #
            if Err <= Err_max:
                h_new = h * 1.5  #h*S*np.power(np.abs(Err/Err_max), 1.0/(order + 1))
                if h_new > h_max:  # limit the maximum step size
                    h_new = h_max
                Err_small = True  # break loop
            elif Err > Err_max:
                h_new = h * S * np.power(Err_max / Err, 1.0 / (order))
                #print(" h_new = ", h_new)
                if h_new < h_min:
                    #print("h < h_min")
                    h_new = h_min
                    Err_small = True  # break loop
                elif h_new >= h_min:
                    h = h_new
        t = t + h
        x_ = np.vstack(
            (x_, x_np1_l.reshape(1,
                                 Ndim)))  # add x_n+1 to the array of x values
        t_ = np.append(t_, t)  # add t_n+1 to the array of t values
        n = n + 1
        h = h_new
        if True:  #np.round(((t-t_start)/(t_stop-t_start))*100000) % 1000 == 0:
            print("\r" + "integrated {:.1%}".format(
                (t - t_start) / (t_stop - t_start)),
                  end='')
    T = time.time() - T_0
    print(" done in {:.5g}s".format(T))
    return (t_, x_, T)
Exemplo n.º 29
0
def train_model(args, mdl, mdl_test, results):
    coeff = []
    ang_sb = []
    ang_np = []
    p_angles = []
    all_w = []
    results['args'] = args
    loss = []

    init_loss = mdl.loss(mdl.params_flat)
    init_grad_norm = np.linalg.norm(mdl.gradient(mdl.params_flat))
    print(
        "\n===============================================================================================\n"
    )

    print('Initial loss: {}, norm grad: {}\n'.format(init_loss,
                                                     init_grad_norm))
    results['init_full_loss'] = init_loss
    results['init_full_grad_norm'] = init_grad_norm

    results['history1'] = []
    results['history1_columns'] = [
        'iter_no', 'batch_loss', 'batch_grad_norm', 'batch_param_norm'
    ]
    results['history2'] = []
    results['history2_columns'] = ['full_hessian', 'full_hessian_evals']

    for iter_no in tqdm(range(args.max_iterations),
                        desc="Training Progress",
                        dynamic_ncols=True):
        inputs, targets = get_batch_samples(iter_no, args, mdl)
        batch_loss = mdl.loss(mdl.params_flat, inputs, targets)
        batch_grad = mdl.gradient(mdl.params_flat, inputs, targets)
        batch_grad_norm = np.linalg.norm(batch_grad)
        batch_param_norm = np.linalg.norm(mdl.params_flat)

        if iter_no % args.freq == 0:

            # calculating hessian
            hess = mdl.hessian(mdl.params_flat)  # Calculating Hessian
            hess = torch.tensor(
                hess).float()  # Converting the Hessian to Tensor
            eigenvalues, eigenvec = torch.symeig(
                hess, eigenvectors=True
            )  # Extracting the eigenvalues and Eigen Vectors from the Calculated Hessian

            if iter_no == 0:
                prev_hess = hess
                prev_eigval = eigenvalues
                prev_eigvec = eigenvec

            top = args.top  # This decides how many top eigenvectors are considered
            dom = eigenvec[:,
                           -top:]  # |The reason for negative top :: torch.symeig outputs eigen vectors in the increasing order and as a result |
            # |                               the top (maximum) eigenvectors will be atlast.                             |
            dom = dom.float()
            alpha = torch.rand(
                top
            )  # A random vector which is of the dim of variable "top" is being initialized

            # Finding the top vector
            vec = (alpha * dom.float()).sum(
                1)  # Representing alpha onto dominant eigen vector
            vec = vec / torch.sqrt(
                (vec * vec).sum())  # Normalization of top vector
            #            vec = vec*5

            # Finding gradient at top vec using Dummy network.
            mdl_test.params_flat = np.array(vec)

            batch_grad_mdl_test = mdl_test.gradient(mdl_test.params_flat,
                                                    inputs, targets)
            mdl_test.params_flat -= batch_grad_mdl_test * args.learning_rate

            # Find coeff and append. But why do we need to find the coeffs ?
            c = torch.mv(hess.transpose(0, 1),
                         torch.tensor(mdl_test.params_flat).float())
            if np.size(coeff) == 0:
                coeff = c.detach().cpu().numpy()
                coeff = np.expand_dims(coeff, axis=0)
            else:
                coeff = np.concatenate(
                    (coeff, np.expand_dims(c.detach().cpu().numpy(), axis=0)),
                    0)

#	Statistics of subspaces, (1) Angle between top subpaces
            eigenvalues_prev, eigenvec_prev = torch.symeig(prev_hess,
                                                           eigenvectors=True)
            dom_prev = eigenvec_prev[:,
                                     -top:]  # Is it not the same as the variable "dom" that was calculated earlier ?
            # calculation 1 norm, which is nothing but angle between subspaces
            ang = np.linalg.norm(
                torch.mm(dom_prev, dom.transpose(0, 1)).numpy(), 1)
            ang_sb.append(ang)
            ang = np.rad2deg(subspace_angles(dom_prev, dom))
            ang_np.append(ang)
            #    Calculating principal angles
            u, s, v = torch.svd(torch.mm(dom.transpose(0, 1), dom_prev))
            #    Output in radians
            s = torch.acos(torch.clamp(s, min=-1, max=1))
            s = s * 180 / math.pi
            #    Attach 's' to p_angles
            if np.size(p_angles) == 0:
                p_angles = s.detach().cpu().numpy()
                p_angles = np.expand_dims(p_angles, axis=0)
            else:
                p_angles = np.concatenate(
                    (p_angles, np.expand_dims(s.detach().cpu().numpy(),
                                              axis=0)), 0)
            prev_hess = hess
            prev_eigval = eigenvalues
            prev_eigvec = eigenvec


#    saving weights in all iterations
        if batch_grad_norm <= args.stopping_grad_norm:
            break
        mdl.params_flat -= batch_grad * args.learning_rate
        #print(mdl.params_flat)
        all_w.append(np.power(math.e, mdl.params_flat))
        # print('{:06d} {} loss: {:.8f}, norm grad: {:.8f}'.format(iter_no, datetime.now(), batch_loss, batch_grad_norm))
        loss.append(batch_loss)
    final_loss = mdl.loss(mdl.params_flat)
    final_grad_norm = np.linalg.norm(mdl.gradient(mdl.params_flat))
    print('\nFinal loss: {}, norm grad: {}'.format(final_loss,
                                                   final_grad_norm))
    args.suffix = args.results_folder + '/coeff.npy'
    np.save(args.suffix, coeff)
    args.suffix = args.results_folder + '/ang_sb.npy'
    np.save(args.suffix, ang_sb)
    args.suffix = args.results_folder + '/ang_np.npy'
    np.save(args.suffix, ang_np)
    args.suffix = args.results_folder + '/p_angles.npy'
    np.save(args.suffix, p_angles)
    args.suffix = args.results_folder + '/all_weights.npy'
    np.save(args.suffix, np.array(all_w))

    #    Saving png plots
    coeff = torch.tensor(coeff)
    for i in range(coeff.shape[0]):
        a = torch.zeros(coeff[i].shape[0]).long()
        b = torch.arange(0, coeff[i].shape[0])
        c = torch.where(((coeff[i] > -0.1) & (coeff[i] < 0.1)), b, a)
        z = torch.zeros(coeff[i].shape[0]).fill_(0)
        z[torch.nonzero(c)] = coeff[i][torch.nonzero(c)]
        z = np.array(z)
        plt.plot(z)
    plt.xlabel('Dimension', fontsize=14)
    plt.ylabel('Coefficient', fontsize=14)
    pnpy = args.results_folder + '/plot1'
    plt.savefig(pnpy, format='png', pad_inches=5)

    return args.results_folder
Exemplo n.º 30
0
    return np.sum(np.power(predictions - train_Y, 2)) / N


# Function that returns gradients of loss function
gradient_fun = elementwise_grad(loss)

# Optimizable parameters with random initialization
weight = rng.randn()
bias = rng.randn()

for epoch in range(training_epochs):
    gradients = gradient_fun((weight, bias))
    weight -= gradients[0] * learning_rate
    bias -= gradients[1] * learning_rate

# Train error
print('Train error={}'.format(loss((weight, bias))))

# Test error
predictions = (test_X * weight) + bias
print('Test error={}'.format(np.sum(np.power(predictions - test_Y, 2)) / N))

# Optimization results
print('Weight={} Bias={}'.format(weight, bias))

# Graphic display
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.plot(train_X, weight * train_X + bias, label='Fitted line')
plt.legend()
plt.show()
Exemplo n.º 31
0
def test_power_arg1():
    x = npr.randn()**2
    fun = lambda y : np.power(x, y)
    d_fun = grad(fun)
    check_grads(fun, npr.rand()**2)
    check_grads(d_fun, npr.rand()**2)
Exemplo n.º 32
0
def loss((weight, bias)):
    """ Loss function: Mean Squared Error """
    predictions = (train_X * weight) + bias
    return np.sum(np.power(predictions - train_Y, 2)) / N
Exemplo n.º 33
0
def f_test(x, y):
    return np.power(x, 2) + np.power(y - 1, 2)
Exemplo n.º 34
0
def alpha(omega, q, chi2l, chi2):
    return (
        (-0.18229166666666666 - (5 * (1 + q) * (1 / (1 + q) - q / (1 + q))) /
         (64. * q)) / omega +
        ((-35 * chi2l * q) / 128. -
         (15 * chi2l * (1 + q) *
          (1 / (1 + q) - q /
           (1 + q))) / 128.) / np.power(omega, 0.6666666666666666) +
        (-1.7952473958333333 -
         (35 * np.power(chi2, 2) * np.power(q, 2)) / 128. - (515 * q) /
         (384. * np.power(1 + q, 2)) - (175 * (1 / (1 + q) - q /
                                               (1 + q))) / (256. * (1 + q)) -
         (4555 * (1 + q) * (1 / (1 + q) - q /
                            (1 + q))) / (7168. * q) -
         (15 * np.power(chi2, 2) * q * (1 + q) *
          (1 / (1 + q) - q /
           (1 + q))) / 128. - (15 * np.power(1 / (1 + q) - q /
                                             (1 + q), 2)) /
         (256. * q)) / np.power(omega, 0.3333333333333333) +
        (4.318908476114694 - (35 * chi2l * np.pi * q) / 16. +
         (475 * np.power(chi2, 2) * np.power(q, 2)) / 6144. -
         (35 * np.power(chi2, 4) * np.power(q, 4)) / 512. +
         (35 * np.power(chi2, 2) * np.power(chi2l, 2) * np.power(q, 4)) / 128.
         + (955 * np.power(q, 2)) / (576. * np.power(1 + q, 4)) + (39695 * q) /
         (86016. * np.power(1 + q, 2)) +
         (575 * np.power(chi2, 2) * np.power(q, 3)) /
         (1536. * np.power(1 + q, 2)) +
         (1645 * np.power(chi2l, 2) * np.power(q, 3)) /
         (192. * np.power(1 + q, 2)) + (2725 * q * (1 / (1 + q) - q /
                                                    (1 + q))) /
         (3072. * np.power(1 + q, 3)) - (265 * (1 / (1 + q) - q /
                                                (1 + q))) /
         (14336. * (1 + q)) + (145 * np.power(chi2, 2) * np.power(q, 2) *
                               (1 / (1 + q) - q /
                                (1 + q))) / (512. * (1 + q)) +
         (1815 * np.power(chi2l, 2) * np.power(q, 2) * (1 / (1 + q) - q /
                                                        (1 + q))) /
         (256. * (1 + q)) - (15 * chi2l * np.pi * (1 + q) *
                             (1 / (1 + q) - q /
                              (1 + q))) / 16. +
         (27895885 * (1 + q) * (1 / (1 + q) - q /
                                (1 + q))) / (2.1676032e7 * q) -
         (485 * np.power(chi2, 2) * q * (1 + q) *
          (1 / (1 + q) - q /
           (1 + q))) / 14336. - (15 * np.power(chi2, 4) * np.power(q, 3) *
                                 (1 + q) * (1 / (1 + q) - q /
                                            (1 + q))) / 512. +
         (15 * np.power(chi2, 2) * np.power(chi2l, 2) * np.power(q, 3) *
          (1 + q) *
          (1 / (1 + q) - q /
           (1 + q))) / 128. + (1615 * np.power(1 / (1 + q) - q /
                                               (1 + q), 2)) / (28672. * q) +
         (15 * np.power(chi2, 2) * q * np.power(1 / (1 + q) - q /
                                                (1 + q), 2)) / 256. +
         (375 * np.power(chi2l, 2) * q * np.power(1 / (1 + q) - q /
                                                  (1 + q), 2)) / 256. +
         (35 * np.power(1 / (1 + q) - q /
                        (1 + q), 2)) / (256. * np.power(1 + q, 2)) +
         (15 * np.power(1 / (1 + q) - q / (1 + q), 3)) /
         (1024. * q * (1 + q))) * np.power(omega, 0.3333333333333333) -
        (35 * np.pi * np.log(omega)) / 48. +
        (2995 * chi2l * q * np.log(omega)) / 9216. -
        (35 * np.power(chi2, 2) * chi2l * np.power(q, 3) * np.log(omega)) /
        384. + (2545 * chi2l * np.power(q, 2) * np.log(omega)) /
        (1152. * np.power(1 + q, 2)) +
        (5 * chi2l * q * (1 / (1 + q) - q / (1 + q)) * np.log(omega)) /
        (3. * (1 + q)) + (2035 * chi2l * (1 + q) *
                          (1 / (1 + q) - q /
                           (1 + q)) * np.log(omega)) / 21504. -
        (5 * np.pi * (1 + q) * (1 / (1 + q) - q / (1 + q)) * np.log(omega)) /
        (16. * q) - (5 * np.power(chi2, 2) * chi2l * np.power(q, 2) * (1 + q) *
                     (1 / (1 + q) - q / (1 + q)) * np.log(omega)) / 128. +
        (5 * chi2l * np.power(1 / (1 + q) - q /
                              (1 + q), 2) * np.log(omega)) / 16.)
Exemplo n.º 35
0
def get_learning_rate_inv_scaling(t, base_lr=0.01, power_t=0.25):
   return base_lr / np.power(t+1, power_t)
Exemplo n.º 36
0
def test_power_arg1():
    x = npr.randn()**2
    fun = lambda y : np.power(x, y)
    check_grads(fun)(npr.rand()**2)
Exemplo n.º 37
0
def lms_loss(params,inputs,targets,l2_reg) :
    reg_prior = l2_reg * l2_norm(params)
    lms = auto_np.sum(auto_np.power(neural_net_predict(params,inputs) - targets,2))
    return lms + reg_prior
Exemplo n.º 38
0
def weibull(t, k=1.5, l=1, w=7):
    x = t/float(w)*2.5  # transform to [0, 3]
    return (k/l) * np.power((x/l), k-1) * np.exp(-1 * np.power((x/l), k)) if x > 0 else 0
Exemplo n.º 39
0
 def calc_linear_loss(W):
     y_pred = np.dot(XMat, W)
     return np.sqrt((np.power(yMat - y_pred, 2))).mean()
Exemplo n.º 40
0
def d_ll(x, num_peds, ess, robot_mu_x, robot_mu_y, ped_mu_x, ped_mu_y, \
         cov_robot_x, cov_robot_y, inv_cov_robot_x, inv_cov_robot_y, \
         cov_ped_x, cov_ped_y, inv_cov_ped_x, inv_cov_ped_y, \
         one_over_cov_sum_x, one_over_cov_sum_y, \
         one_over_cov_sumij_x, one_over_cov_sumij_y, normalize, T):
  d_beta = [0. for _ in range(2*T*np.int(np.round(ess+1)))]
  d_llambda = np.asarray([0. for _ in range(2*T*np.int(np.round(ess+1)))])
# DERIVATIVE WRT ROBOT
  i = 2
  for ped in range(ess):
    vel_x = np.tile(x[:T],(T,1)).T - np.tile(x[i*T:(i+1)*T],(T,1))
    vel_y = np.tile(x[T:2*T],(T,1)).T - np.tile(x[(i+1)*T:(i+2)*T],(T,1))
    vel_x = np.diag(vel_x)
    vel_y = np.diag(vel_y)

    vel_x_2 = np.power(vel_x, 2)
    vel_y_2 = np.power(vel_y, 2)

    quad_x = np.multiply(one_over_cov_sum_x[ped], vel_x_2)
    quad_y = np.multiply(one_over_cov_sum_y[ped], vel_y_2)

    Z_x = np.exp(-0.5*quad_x)
    Z_y = np.exp(-0.5*quad_y)
    Z = np.multiply(Z_x, Z_y)
    X = np.divide(Z, 1.-Z)

    alpha_x = np.multiply(X, np.multiply(vel_x, one_over_cov_sum_x[ped]))
    alpha_y = np.multiply(X, np.multiply(vel_y, one_over_cov_sum_y[ped]))

    d_llambda[:T] = np.add(d_llambda[:T], np.sum(alpha_x, axis=1))
    d_llambda[T:2*T] = np.add(d_llambda[T:2*T], np.sum(alpha_y, axis=1))
    i = i + 2

  d_beta[:T] = -np.dot(x[:T]-robot_mu_x, inv_cov_robot_x)
  d_beta[T:2*T] = -np.dot(x[T:2*T]-robot_mu_y, inv_cov_robot_y)
  d_llambda[0:2*T] = np.add(d_llambda[0:2*T], d_beta[0:2*T])
# DERIVATIVE WRT PED: ROBOT-PED DERIVATIVE, FIRST TERM
  i = 2
  for ped in range(ess):
    vel_x = np.tile(x[:T],(T,1)) - np.tile(x[i*T:(i+1)*T],(T,1)).T
    vel_y = np.tile(x[T:2*T],(T,1)) - np.tile(x[(i+1)*T:(i+2)*T],(T,1)).T
    vel_x = np.diag(vel_x)
    vel_y = np.diag(vel_y)

    vel_x_2 = np.power(vel_x, 2)
    vel_y_2 = np.power(vel_y, 2)

    quad_x = np.multiply(vel_x_2, one_over_cov_sum_x[ped])
    quad_y = np.multiply(vel_y_2, one_over_cov_sum_y[ped])

    Z_x = np.exp(-0.5*quad_x)
    Z_y = np.exp(-0.5*quad_y)

    Z = np.multiply(Z_x, Z_y)
    X = np.divide(Z, 1.-Z)

    alpha_x = np.multiply(X, np.multiply(vel_x, one_over_cov_sum_x[ped]))
    alpha_y = np.multiply(X, np.multiply(vel_y, one_over_cov_sum_y[ped]))

    d_llambda[i*T:(i+1)*T] = -np.sum(alpha_x, axis=1)
    d_llambda[(i+1)*T:(i+2)*T] = -np.sum(alpha_y, axis=1)
    i = i + 2
# DERIVATIVE WRT PED: PED-PED DERIVATIVE, SECOND TERM
  i = 2
  j = 2
  for ped_i in range(ess):
    for ped_j in range(ess):
      if i != j:
        vel_x = np.tile(x[i*T:(i+1)*T],(T,1)).T - np.tile(x[j*T:(j+1)*T], (T,1))
        vel_y = np.tile(\
                 x[(i+1)*T:(i+2)*T],(T,1)).T - np.tile(x[(j+1)*T:(j+2)*T],(T,1))
        vel_x = np.diag(vel_x)
        vel_y = np.diag(vel_y)
        vel_x_2 = np.power(vel_x, 2)
        vel_y_2 = np.power(vel_y, 2)

        quad_x = np.multiply(vel_x_2, one_over_cov_sumij_x[ped_i][ped_j])
        quad_y = np.multiply(vel_y_2, one_over_cov_sumij_y[ped_i][ped_j])

        Z_x = np.exp(-0.5*quad_x)
        Z_y = np.exp(-0.5*quad_y)
        Z = np.multiply(Z_x, Z_y)
        X = np.divide(Z, 1.-Z)

        alpha_x = np.multiply(X, np.multiply(vel_x, \
                                            one_over_cov_sumij_x[ped_i][ped_j]))
        alpha_y = np.multiply(X, np.multiply(vel_y, \
                                            one_over_cov_sumij_y[ped_i][ped_j]))

        d_llambda[i*T:(i+1)*T] = np.add(d_llambda[i*T:(i+1)*T], \
                                                        np.sum(alpha_x, axis=1))
        d_llambda[(i+1)*T:(i+2)*T] = np.add(d_llambda[(i+1)*T:(i+2)*T], \
                                                        np.sum(alpha_y, axis=1))
      j = j + 2
    i = i + 2
    j = 2
# DERIVATIVE WRT PED: PED-PED DERIVATIVE, THIRD TERM
  i = 2
  for ped in range(ess):
    d_beta[i*T:(i+1)*T] = -np.dot(x[i*T:(i+1)*T]-ped_mu_x[ped], \
                                                           inv_cov_ped_x[ped])
    d_beta[(i+1)*T:(i+2)*T] = -np.dot(x[(i+1)*T:(i+2)*T]-ped_mu_y[ped], \
                                                           inv_cov_ped_y[ped])
    i = i + 2
  d_llambda[2*T:] = np.add(d_llambda[2*T:], d_beta[2*T:])
  return -1.*d_llambda
Exemplo n.º 41
0
def test_power_arg1_zero():
    fun = lambda y : np.power(0., y)
    check_grads(fun)(npr.rand()**2)
Exemplo n.º 42
0
 def calc_linear_loss(W):
     y_pred = np.dot(XMat, W)
     return np.sqrt((np.power(yMat - y_pred, 2))).mean() \
                 + np.sum(self.alpha * W[0:-1] * W[0:-1])
Exemplo n.º 43
0
 def _cfun(self, d):
     return 2 * np.power( np.power( d + 1 , .5) - 1, 2)
Exemplo n.º 44
0
 def incremental_ll(theta, mu, y, C, V, eta_k, lmd, d, t):
     return 0.5 * d * np.log(normnon(theta, mu, V, t) + eta_k) + (
         (d + lmd) / 2.0
     ) * np.log(1 + np.power(
         np.linalg.norm(y - C @ self.nonlinearity(theta, mu, t)), 2) /
                (lmd * (eta_k + normnon(theta, mu, V, t))))
Exemplo n.º 45
0
 def _cfun(self, d):
     return np.power(d,2) / (d + 2)
Exemplo n.º 46
0
def GammaCorrect(im):
    return 255.0 * np.power(im.astype(np.float32) * (1.0 / 255.0), 2.2)
Exemplo n.º 47
0
 def mags2nanomaggies(self, mags):
     return np.power(10., (mags - 22.5)/-2.5)
Exemplo n.º 48
0
#    
#    m_hat = m/(1-np.power(b1,i))
#    v_hat = v/(1-np.power(b2,i))
#    
#    param = param - (eta*m_hat)/(np.sqrt(v_hat) + e)   
    

for i in range(1,it+1):
    gradw1 = gw1(w1,w2,w3,b1,b2,b3)
    gradw2 = gw2(w1,w2,w3,b1,b2,b3)
    gradw3 = gw3(w1,w2,w3,b1,b2,b3)
    gradb1 = gb1(w1,w2,w3,b1,b2,b3)
    gradb2 = gb2(w1,w2,w3,b1,b2,b3)
    gradb3 = gb3(w1,w2,w3,b1,b2,b3)
    
    gradw1_2 = np.power(gradw1,2)
    gradw2_2 = np.power(gradw2,2)
    gradw3_2 = np.power(gradw3,2)
    gradb1_2 = np.power(gradb1,2)
    gradb2_2 = np.power(gradb2,2)
    gradb3_2 = np.power(gradb3,2)
    
    mw1 = b1*mw1 + (1-b1)*gradw1
    vw1 = b2*vw1 + (1-b2)*gradw1_2
    m_hatw1 = mw1/(1-np.power(b1,i))
    v_hatw1 = vw1/(1-np.power(b2,i))
    
    mw2 = b1*mw2 + (1-b1)*gradw2
    vw2 = b2*vw2 + (1-b2)*gradw2_2
    m_hatw2 = mw2/(1-np.power(b1,i))
    v_hatw2 = vw2/(1-np.power(b2,i))
Exemplo n.º 49
0
 def gammainc_vjp_arg1(ans, a, x):
     coeffs = sign * np.exp(-x) * np.power(x, a - 1) / gamma(a)
     return unbroadcast_f(x, lambda g: g * coeffs)
Exemplo n.º 50
0
pix_1d = np.linspace(0., 1., n_grid) # pixel gridding
fdensity_true = float(Ndata)/float(n_grid**2); #number density of obj in 1d

sig_psf = 0.01 # psf width
sig_noise = 0.01 # noise level

mid = int(n_grid/2);
x,y = np.meshgrid(pix_1d,pix_1d);
psf = np.exp(-((y-pix_1d[mid])**2 + (x - pix_1d[mid])**2)/2/sig_psf**2); #keep in mind difference between x and y position and indices! Here, you are given indices, but meshgrid is in x-y coords

#these are values for the power law function for sampling intensities
w_interval = (1,2);
w_lin = np.linspace(1,2,100);
alpha_true = 2;
w_norm = (50**(alpha_true+1) - w_interval[0]**(alpha_true+1))/(alpha_true+1);
w_func = np.power(w_lin,alpha_true)/w_norm;
w_true = w_norm*np.random.choice(w_func,Ndata);


def psi(pos): 
    ''' measurement model, which in our case is just a 1d gaussian of width 
    sigma (PSF) written out to a meshgrid created by pix1d 
    '''
    x,y = np.meshgrid(pix_1d,pix_1d);
    return np.exp(-((y-pix_1d[pos[0]])**2 + (x - pix_1d[pos[1]])**2)/2/sig_psf**2); #keep in mind difference between x and y position and indices! Here, you are given indices, but meshgrid is in x-y coords

def gaussian(x, loc=None, scale=None): 
    '''
    scipy's gaussian pdf didn't work idk
    '''
    y = (x - loc)/scale
Exemplo n.º 51
0
def test_power_arg1():
    x = npr.randn()**2
    fun = lambda y : np.power(x, y)
    d_fun = grad(fun)
    check_grads(fun, npr.rand()**2)
    check_grads(d_fun, npr.rand()**2)
Exemplo n.º 52
0
def test_pow():
  grad_test(lambda x: 0.4 ** x, lambda x: np.power(0.4, x))
  grad_test(lambda y: y ** 0.4, lambda y: np.power(y, 0.4))