Пример #1
0
def brick(CC=1.4,buckling=0.0):
   """
    Returns a list containing all the elements of the basic brick for graphene
       d2
         \ 
          \_____d3
          /
         /
       d1
   """
   # NNN vectors
   d1=np.array([-1./(2.*np.sqrt(3.)),-1./2.,0.])
   d2=np.array([-1./(2.*np.sqrt(3.)), 1./2.,0.])
   d3=np.array([1./np.sqrt(3.),0.,0.])
   # ====================================
   #    Normalize the distance vectors
   # ====================================
   d1 = ut.norm(d1,CC)
   d2 = ut.norm(d2,CC)
   d3 = ut.norm(d3,CC)
 
   alpha1 = 2*d3-d2-d1
   alpha2 = d1-d2
   # Position of the atoms in the cell (brick)
   vec_buc = np.array([0,0,buckling])
   position_atoms=[np.array([0.,0.,0.]),-d1+vec_buc,d3-d1,d3-d1-d2+vec_buc]
   name_atoms=['C','C','C','C']
   a_cell= [alpha1,alpha2]
   return name_atoms, position_atoms, a_cell
Пример #2
0
def dimer(CC=1.4,buckling=0.0):
   """
     Returns the base of the graphene and its lattice vectors
                  d2
                   \ 
                    \____d3
                    /
                   /
                  d1
   """
   d1 = np.array([-1./(2.*np.sqrt(3.)),-1./2.,0.])
   d2 = np.array([-1./(2.*np.sqrt(3.)), 1./2.,0.])
   d3 = np.array([1./np.sqrt(3.),0.,0.])

   d1 = ut.norm(d1,CC)
   d2 = ut.norm(d2,CC)
   d3 = ut.norm(d3,CC)

   vec_buc = np.array([0,0,buckling])
   cell = [np.array([0.,0.,0.]),d3+vec_buc]
   At = ['C','C']

   a1 = d3-d1
   a2 = d3-d2
   latt_vec = [a1,a2] ## vectors along which repeat the basic dimer
   return At,cell,latt_vec
Пример #3
0
    def collision_detection(self, obj):
        if isinstance(obj, Object.Bullet):
            pot = [
                self.position[0], self.position[1] - self.earth,
                self.position[2]
            ]
            after_bullet = obj.position
            before_bullet = obj.back()

            before_to_after = util.Vec(after_bullet) - util.Vec(before_bullet)
            pot_to_before = util.Vec(before_bullet) - util.Vec(pot)
            pot_to_after = util.Vec(after_bullet) - util.Vec(pot)

            if util.dot(before_to_after, -1 * pot_to_before) < 0:
                if util.norm(pot_to_before) < self.radius:
                    return True
                else:
                    return False
            elif util.dot(before_to_after, pot_to_after) < 0:
                if util.norm(pot_to_after) < self.radius:
                    return True
                else:
                    return False
            else:
                if util.norm(util.cross3d(before_to_after, pot_to_before)
                             ) / util.norm(before_to_after) < self.radius:
                    return True
                else:
                    return False
    def build_q_gradients(self):
        cfg = self.config
        q_gradients = self.q_neg_elbo_grad
        constraint_grad = [0.] * len(self.q_params)
        magnitude = self.magnitude
        if cfg['c/decay'] == 'linear':
            magnitude = tf.maximum(magnitude, 0.)
        for name, distance in self.distance.items():
            distance_grad = tf.gradients(distance, self.q_params)
            for i in range(len(self.q_params)):
                if distance_grad[i] is not None:
                    param_name = util.tensor_name(self.q_params[i])
                    update = magnitude * distance_grad[i]
                    constraint_grad[i] += update
                    q_gradients[i] += update
                    update_norm = util.norm(update)
                    fraction = tf.reduce_mean(
                        update_norm /
                        util.norm(self.q_neg_elbo_grad[i] + update))

                    fraction = tf.Print(fraction, [fraction], 'fraction: ')
                    tf.summary.scalar(
                        '_'.join(['c/fraction_grad_d', name, param_name]),
                        fraction)
                    tf.summary.scalar(
                        '_'.join(['c/norm_grad_constraint', name, param_name]),
                        update_norm)
                    tf.summary.scalar(
                        '_'.join([
                            'c/ratio_grad_constraint_grad_neg_elbo', name,
                            param_name
                        ]), update_norm / self.q_neg_elbo_grad_norm[i])
        self.q_gradients = q_gradients
        self.q_constraint_grad = constraint_grad
Пример #5
0
def armchair(CC,buckling):
   """
    Returns a list containing all the elements of the basis for an
    armchair ribbon
           H(0)      H(1)            |
            \       /                |      d2
          (2)C-----C(3)              |       \ 
                    \                |        \_____d3
                  (4)C-----C(5)      |        /
                    /       \        |       /
                   H(6)      H(7)    |      d1
   """
   # NNN vectors
   d1=np.array([-1./(2.*np.sqrt(3.)),-1./2.,0.])
   d2=np.array([-1./(2.*np.sqrt(3.)), 1./2.,0.])
   d3=np.array([1./np.sqrt(3.),0.,0.])
   # ====================================
   #    Normalize the distance vectors
   # ====================================
   d1 = ut.norm(d1,CC)
   d2 = ut.norm(d2,CC)
   d3 = ut.norm(d3,CC)
 
   # Position of the atoms in the cell (brick)
   vec_buc = np.array([0,0,buckling])
   position_atoms=[np.array([0.,0.,0.]),d3+vec_buc,d3-d2,d3-d2+d3+vec_buc]
   name_atoms=['C','C','C','C']
   latt = d3 - d2 + d3 - d1
   #a_cell= [d1 - d2, latt] # repetition vector, lattice vector
   a_cell= [latt, d1 - d2] # repetition vector, lattice vector
   return name_atoms, position_atoms, a_cell
Пример #6
0
def make_full_ellipse(P0, T0, P2, T2, P):
    ''' Construct a full ellipse in three-dimensional space based on
    information given in the form of Group 2: start and end points,
    together with their tangent directions and an additional point.

    Source: The NURBS Book (2nd Ed.), Pg. 318-320.

    '''

    P0, T0 = [np.asfarray(V) for V in P0, T0]
    P2, T2 = [np.asfarray(V) for V in P2, T2]
    P = np.asfarray(P)
    P1, w1 = make_one_arc(P0, T0, P2, T2, P)
    S, T = P0 - P1, P2 - P1
    k = 1.0 / w1**2
    e = k / (k - 1.0) / 2
    a = util.norm(S)**2
    b = np.dot(S, T)
    g = util.norm(T)**2
    d = a * g - b**2
    E = a + g - 2 * b
    ls = np.roots((2 * d, -(k * E + 4 * b), 2 * (k - 1)))
    l1, l2 = np.sort(ls)
    r1, r2 = np.sqrt(e / l1), np.sqrt(e / l2)
    if abs(k / 2 - g * l1) > abs(k / 2 - a * l1):
        xb = k / 2 - g * l1
        yb = b * l1 - k / 2 + 1
    else:
        xb = b * l1 - k / 2 + 1
        yb = k / 2 - a * l1
    r = a * xb**2 + 2 * b * xb * yb + g * yb**2
    x0, y0 = xb / r, yb / r
    Q1 = P1 + (e + r1 * x0) * S + (e + r1 * y0) * T
    Q2 = P1 + (e - r1 * x0) * S + (e - r1 * y0) * T
    U = util.normalize(Q2 - Q1)
    V = np.cross(U, util.normalize(np.cross(S, T)))
    Pw, Uq = np.zeros((8 + 1, 4)), np.zeros(8 + 4)
    C = P1 + e * (S + T)
    Pw[0] = Pw[8] = np.hstack((C + r1 * U, 1.0))
    Pw[1] = np.hstack((w1 * (C + r1 * U + r2 * V), w1))
    Pw[2] = np.hstack((C + r2 * V, 1.0))
    Pw[3] = np.hstack((w1 * (C + r2 * V - r1 * U), w1))
    Pw[4] = np.hstack((C - r1 * U, 1.0))
    Pw[5] = np.hstack((w1 * (C - r1 * U - r2 * V), w1))
    Pw[6] = np.hstack((C - r2 * V, 1.0))
    Pw[7] = np.hstack((w1 * (C - r2 * V + r1 * U), w1))
    for i in xrange(3):
        Uq[i] = 0.0
        Uq[i + 9] = 1.0
    for i in xrange(2):
        Uq[i + 3] = 0.25
        Uq[i + 5] = 0.5
        Uq[i + 7] = 0.75
    cpol = curve.ControlPolygon(Pw=Pw)
    return curve.Curve(cpol, (2, ), (Uq, ))
Пример #7
0
def lanczos_bidiagonalization(A, p0, k):
    """
    Lanczos bidiagonalization as presented as (algorithm 1)
    in the Kokiopoulou paper. 

    inputs:

    Linear Operator : as defined in util
    p0 : start vector 
    k : number of iters

    output: 
    B_k :  bidiagonal real matrix, size (k+1) x k
    U_kp1  : Orthogonal bases U_{k+1} \in C, m x (k+1) 
    V_k  : Orthogonal bases V_{k} \in C, n x (k) 
    
    Note that we infer the correct dtype based on p0

    FIXME: Don't return dense B_k

    """
    
    dtype = p0.dtype

    betas = np.zeros(k+1)
    alphas = np.zeros(k)
    m, n = A.dims()
    U = np.zeros((m, k+1), dtype=dtype)
    V = np.zeros((n, k+1), dtype=dtype)

    # init
    betas[0] = norm(p0)
    U[:, 0] = p0/betas[0]
    V[:, 0] = 0 
    for i in range(k):
        r = A.compute_ATx(U[:, i]) 
        if i > 0: 
            r -= np.dot(betas[0], V[:, i]) 
        alphas[i] = norm(r)
        V[:, i+1]  = r/alphas[i]
        p = A.compute_Ax(V[:, i+1]) - alphas[i]*U[:, i]
        betas[i+1] = norm(p)
        U[:, i+1] = p / betas[i+1]

    B = np.zeros((k+1, k), dtype = dtype)

    for i in range(k):
        B[i, i] = alphas[i]
        B[i+1, i] = betas[i+1]

    return B, U, V[:, 1:]
Пример #8
0
def partial_lanczos_bidiagonalization(A, p1, m):
    """
    Are we implementing the same algo as before? 
    Does this do the same reortho? 

    from BR05 algo 2.1
    FIXME : Add recent docs
    """
    
    l, n = A.dims()
    assert l >= n
    assert len(p1) == n 
    
    P = np.zeros((n, m))
    Q = np.zeros((l, m))
    B = np.zeros((m, m))

    beta = np.zeros(m-1)
    alpha = np.zeros(m)

    P[:, 0] = p1; 
    q_cur = A.compute_Ax(p1)
    
    alpha[0] = norm(q_cur)
    q_cur = q_cur / alpha[0]
    Q[:, 0] = q_cur
    for j in range(m):
        q_cur = Q[:, j]
        rj = A.compute_ATx(q_cur) - alpha[j]*P[:, j]
        #reorthogonalize

        rj = rj - np.dot(P[:, j], np.dot(P[:, j].T, rj))
        if j < (m-1):
            beta[j] = norm(rj)
            P[:, j+1] = rj/beta[j]
            q_next = A.compute_Ax(P[:, j+1]) - beta[j]*q_cur
            
            # reortho:
            q_next = q_next - np.dot(Q[:, j], np.dot(Q[:, j].T, q_next))
            alpha[j+1] = norm(q_next)
            q_next = q_next / alpha[j+1]
            Q[:, j+1] = q_next
    rm = rj
    
    for i in range(m):
        B[i, i] = alpha[i]
        if i < (m-1):
            B[i, i+1] = beta[i]

    return P, Q, B, rm
Пример #9
0
def compute_alfs(n, qk):
    ''' Compute the interpolation parameters.

    '''

    alf = np.zeros(n + 1)
    for k in xrange(1, n + 2):
        num = util.norm(np.cross(qk[k - 1], qk[k]))
        den = num + util.norm(np.cross(qk[k + 1], qk[k + 2]))
        if np.allclose(den, 0.0):
            alf[k - 1] = 0.5
            continue
        alf[k - 1] = num / den
    return alf
Пример #10
0
    def get_derived_data(self, symbol, prices_all, sd, ed):
        """
        get derived data macd and bollinger band given the raw data
        """
        # macd
        macd = ut.norm(ut.get_macd(prices_all[symbol])["MACD"].as_matrix())

        # bollinger band
        bb = ut.Bollinger_Bands_given_sym_dates([symbol], sd, ed)
        bb_rm = ut.norm(bb['rolling_mean'].as_matrix())
        bb_ub = ut.norm(bb['upper_band'].as_matrix())
        bb_lb = ut.norm(bb['lower_band'].as_matrix())

        return macd, bb, bb_rm, bb_ub, bb_lb
Пример #11
0
def read_song(source_id, target_sr):
    song_data_raw, source_sr = lr.load(data_source[source_id])
    song_data = lr.resample(song_data_raw, source_sr, target_sr, scale=True)
    song_data = song_data[1500:(1500+50*seq_size)] #song_data.shape[0]/10]
    song_data, data_denom = norm(song_data, return_denom=True)

    return song_data, source_sr, data_denom
 def __init__(self, session, config, model, variational, data):
     cfg = config
     self.config = config
     self.session = session
     self.data = data
     self.variational = variational
     self.model = model
     self.build_elbo(n_samples=cfg['q/n_samples_stats'])
     self.q_params = fw.get_variables('variational')
     self.global_step = fw.get_or_create_global_step()
     self.build_elbo(n_samples=cfg['q/n_samples'], training=True)
     self.build_elbo_loss()
     with tf.name_scope('q_neg_elbo_grad'):
         self.q_neg_elbo_grad = tf.gradients(self.differentiable_elbo_loss,
                                             self.q_params)
     self.q_neg_elbo_grad_norm = [
         util.norm(g) for g in self.q_neg_elbo_grad
     ]
     for param, norm in zip(self.q_params, self.q_neg_elbo_grad_norm):
         tf.summary.scalar(
             'o/neg_elbo_grad_norm_' + util.tensor_name(param), norm)
     self.p_params = fw.get_variables('model')
     self.build_optimizer()
     self.t0 = time.time()
     self.t = np.inf
     self.build_proximity_statistic_summaries()
     for param in self.q_params + self.p_params:
         self.build_summaries(param)
Пример #13
0
def test_rigid_no_noise():

    # Generate some synthetic data.
    n_frames = 40
    n_basis = 1
    n_points = 20

    # Take a randomly generated model.
    gt_model = model.generate_synthetic_model(n_frames, n_basis, n_points)

    # Force scale factor to 1.
    gt_model.C = np.ones((n_frames, 1))

    W = gt_model.W

    # Recover the model
    inf_model = rigid.factor(W)

    # Register with ground truth.
    inf_model.register(gt_model)

    delta = util.norm(inf_model.Ps - gt_model.Ps, axis=1)

    # Ensure the average error is low.
    npt.assert_allclose(delta.mean(), 0, atol=1e-10)
Пример #14
0
def read_song(source_id, target_sr):
    song_data_raw, source_sr = lr.load(data_source[source_id])
    song_data = lr.resample(song_data_raw, source_sr, target_sr, scale=True)
    song_data = song_data[1500:(1500+2*seq_size)] #song_data.shape[0]/10]
    song_data, data_denom = norm(song_data, return_denom=True)

    return song_data, source_sr, data_denom
Пример #15
0
def local_curve_interp(n, Q, Ts=None, Te=None):
    ''' Let {Qk}, k=0,...,n, be a set of three-dimensional data points.
    This algorithm constructs a C1 cubic (p = 3) Bezier curve segment,
    Ck(u), between each pair of points Q_k, Q_(k+1).

    Parameters
    ----------
    n + 1 = the number of data points to interpolate
    Q = the point set in object matrix form
    Ts, Te = the tangent vectors at the start and end data points (if
             available)

    Returns
    -------
    U, Pw = the knot vector and the object matrix of the interpolant

    Source
    ------
    The NURBS Book (2nd Ed.), Pg. 395.

    '''

    if n < 1:
        raise ImproperInput(n)
    Q = nurbs.obj_mat_to_3D(Q)
    T = compute_unit_tangents(n, Q)
    if Ts is not None: T[0] = util.normalize(Ts)
    if Te is not None: T[-1] = util.normalize(Te)
    uk = np.zeros(n + 1)
    P = np.zeros((2 * n + 2, 3))
    P[0] = Q[0]
    for k in xrange(n):
        a = 16.0 - util.norm(T[k] + T[k + 1])**2
        b = 12.0 * np.dot(Q[k + 1] - Q[k], T[k] + T[k + 1])
        c = -36.0 * util.norm(Q[k + 1] - Q[k])**2
        alfs = np.roots((a, b, c))
        dummy, alf = np.sort(alfs)
        P[2 * k + 1] = Q[k] + alf * T[k] / 3.0
        P[2 * k + 2] = Q[k + 1] - alf * T[k + 1] / 3.0
        uk[k + 1] = uk[k] + 3.0 * util.norm(P[2 * k + 1] - Q[k])
    P[-1] = Q[-1]
    Pw = nurbs.obj_mat_to_4D(P)
    uk = uk[1:n].repeat(2) / uk[-1]
    U = np.zeros(2 * n + 6)
    U[-4:] = 1.0
    U[4:-4] = uk
    return U, Pw
Пример #16
0
def distance_along_edge_to_point(edge, distance_along_edge):
    edge_start, edge_end = edge
    edge_vector = util.subtract(edge_end, edge_start)
    edge_length = util.norm(edge_vector)    
    scale_factor = distance_along_edge / edge_length
    scaled_vector = util.scale(scale_factor, edge_vector)
    point = util.add(scaled_vector, edge_start)
    return point
Пример #17
0
def initials(name):
    """ Obtém as iniciais de um nome normalizadas """
    name = util.norm(name)
    # Retira partículas e reordena
    name = nameReorder(nobiliaryRegex.sub(' ', name))
    # Separa nomes por espaços ou pontos, e junta apenas as iniciais
    name = ''.join([word[:1] for word in re.split(r'[\s.]+', name)])
    return re.sub(r'[^a-z]', '', name)
Пример #18
0
def exp1(reps, delta_method="random", fixed_delta=None):
    results = np.zeros(reps)
    for i in range(reps):
        z = random_z()
        delta = get_delta(delta_method, fixed_delta)
        results[i] = norm(dx_dz(z, delta))
    print("exp1: norm of dx/dz with delta_method", delta_method,
          ": min, mean, max, var")
    print(np.min(results), np.mean(results), np.max(results), np.var(results))
Пример #19
0
def exp2(reps, delta_method="random", fixed_delta=None):
    results = np.zeros((reps, s0, s1))
    for i in range(reps):
        z = random_z()
        delta = get_delta(delta_method, fixed_delta)
        results[i, :, :] = dx_dz(z, delta)
    dx_mean = np.mean(results, axis=0)
    print("exp2: mean of dx/dz with delta_method", delta_method,
          ": min, max, norm")
    print(np.min(dx_mean), np.max(dx_mean), norm(dx_mean))
Пример #20
0
 def _read_song(self, fname, proportion=None):
     logging.info("Reading {}".format(fname))
     song_data_raw, source_sr = lr.load(fname)
     logging.info("Got sampling rate {}, resampling to {} ...".format(source_sr, self.cfg.target_sr))
     song_data = lr.resample(song_data_raw, source_sr, self.cfg.target_sr, scale=True)
     logging.info("Normalizing with l2 norm ...")
     if proportion:
         song_data = song_data[: int(proportion * len(song_data)),]
     song_data, data_denom = norm(song_data)
     logging.info("Done")
     return song_data, source_sr, data_denom
Пример #21
0
 def _read_song(self, fname, proportion=None):
     logging.info("Reading {}".format(fname))    
     song_data_raw, source_sr = lr.load(fname)
     logging.info("Got sampling rate {}, resampling to {} ...".format(source_sr, self.cfg.target_sr))
     song_data = lr.resample(song_data_raw, source_sr, self.cfg.target_sr, scale=True)
     logging.info("Normalizing with l2 norm ...")
     if proportion:
         song_data = song_data[:int(proportion*len(song_data)),]
     song_data, data_denom = norm(song_data)
     logging.info("Done")
     return song_data, source_sr, data_denom
Пример #22
0
def Rs_from_M_k(M_k):
    """Estimates rotations from a column triple of M."""

    n_frames = M_k.shape[0] / 2

    # Estimate scales of each rotation matrix by averaging norms of two rows.
    scales = .5 * (norm(M_k[0::2], axis=1) + norm(M_k[1::2], axis=1))

    # Rescale to be close to rotation matrices.
    R = M_k.reshape(n_frames, 2, 3) / scales[:, np.newaxis, np.newaxis]
    Rs = []

    # Upgrade to real rotations.
    for f in range(n_frames):
        Rx = R[f, 0]
        Ry = R[f, 1]
        Rz = normed(np.cross(Rx, Ry))
        U, s, Vh = np.linalg.svd(np.asarray([Rx, Ry, Rz]))
        Rs.append(np.dot(U, Vh))

    return np.asarray(Rs)
Пример #23
0
def chord_length_param(n, Q):
    ''' Compute parameter values based on chord length parameterization.
    Assumes that the parameters lie in the range u in [0, 1]. '''
    Ub = np.zeros(n + 1)
    clk = np.zeros(n + 1)
    for k in xrange(1, n + 1):
        clk[k] = util.norm(Q[k] - Q[k - 1])
    d = np.sum(clk)
    for k in xrange(1, n):
        Ub[k] = Ub[k - 1] + clk[k] / d
    Ub[n] = 1.0
    return Ub
Пример #24
0
def centripetal_param(n, Q):
    ''' Compute parameter values based on the centripetal method.
    Assumes that the parameters lie in the range u in [0, 1]. '''
    Ub = np.zeros(n + 1)
    clk = np.zeros(n + 1)
    for k in xrange(1, n + 1):
        clk[k] = np.sqrt(util.norm(Q[k] - Q[k - 1]))
    d = np.sum(clk)
    for k in xrange(1, n):
        Ub[k] = Ub[k - 1] + clk[k] / d
    Ub[n] = 1.0
    return Ub
Пример #25
0
def Rs_from_M_k(M_k):
    """Estimates rotations from a column triple of M."""
    
    n_frames = M_k.shape[0]/2
    
    # Estimate scales of each rotation matrix by averaging norms of two rows.
    scales = .5 * (norm(M_k[0::2], axis=1) + norm(M_k[1::2], axis=1))
    
    # Rescale to be close to rotation matrices.
    R = M_k.reshape(n_frames, 2, 3) / scales[:,np.newaxis,np.newaxis]    
    Rs = []
    
    # Upgrade to real rotations.
    for f in range(n_frames):
        Rx = R[f,0]
        Ry = R[f,1]
        Rz = normed(np.cross(Rx,Ry))
        U,s,Vh = np.linalg.svd(np.asarray([Rx,Ry,Rz]))
        Rs.append(np.dot(U, Vh))
        
    return np.asarray(Rs)
Пример #26
0
def sample_edge(edge, sample_spacing, distance_along_edge, start_arc_length):
    edge_length = util.norm(util.edge_to_vector(edge))
    edge_points = []
    edge_arc_lengths = []
    while distance_along_edge <= edge_length:
        point_arc_length = distance_along_edge + start_arc_length
        edge_arc_lengths.append(point_arc_length)
        point = distance_along_edge_to_point(edge, distance_along_edge)
        edge_points.append(point)
        distance_along_edge += sample_spacing
    distance_along_edge -= edge_length
    return [edge_points, edge_arc_lengths, distance_along_edge, edge_length]
Пример #27
0
def zigzag(CC,buckling):
   """
     Returns a list containing all the elements of the basis for a
     zig-zag ribbon
              H(0)         |
              |            |
              C(1)         |           d3=dH
            /   \          |             |
           /     \         |             |
          C(2)    C        |             |
          |       |        |            / \ 
          C       C(3)     |           /   \ 
           \     /         |          /     \ 
            \   /          |         d1     d2
              C(4)         |
              |            |
              H(5)         |
   """
   # NNN vectors
   d1=np.array([-1./2.,-1./(2.*np.sqrt(3.)),0.0])
   d2=np.array([ 1./2.,-1./(2.*np.sqrt(3.)),0.0])
   d3=np.array([0.,1./np.sqrt(3.),0.0])
   dH=np.array([0.,1./np.sqrt(3.),0.0])
 
   # ====================================
   #    Normalize the distance vectors
   # ====================================
   d1 = ut.norm(d1,CC)
   d2 = ut.norm(d2,CC)
   d3 = ut.norm(d3,CC)

   # Position of the atoms in the cell (brick)
   vec_buc = np.array([0,0,buckling])
   position_atoms=[np.array([0.,0.,0.]),d1+vec_buc,d2-d3,d1-d3+d2+vec_buc]
   name_atoms=['C','C','C','C']
   a_cell = [d2-d1,d1-d3+d2-d3]
   return name_atoms, position_atoms, a_cell
Пример #28
0
    def collision_detection(self,obj):
        if isinstance(obj,Object.Bullet):
            pot = [self.position[0],
                   self.position[1] - self.earth,
                   self.position[2]]
            after_bullet = obj.position
            before_bullet = obj.back()
            
            before_to_after = util.Vec(after_bullet) - util.Vec(before_bullet)
            pot_to_before = util.Vec(before_bullet) - util.Vec(pot)
            pot_to_after = util.Vec(after_bullet) - util.Vec(pot)

            if util.dot(before_to_after,-1 * pot_to_before) < 0:
                if util.norm(pot_to_before) < self.radius:
                    return True
                else: return False
            elif util.dot(before_to_after,pot_to_after) < 0:
                if util.norm(pot_to_after) < self.radius:
                    return True
                else: return False
            else:
                if util.norm(util.cross3d(before_to_after,pot_to_before)) / util.norm(before_to_after) < self.radius:
                    return True
                else: return False
Пример #29
0
def compaerMode1AndCorrectAnswer():
    path_model = 'data/predict/model_00001_v.txt'
    path_train = 'data/training_set.tsv'
    model_answerList = []
    train_answerList = []
    answer_number = 2500
    right_answer = 0
    for line in open(path_model):
        lst = line.strip('\n').split(',')
        answer = lst[1]
        model_answerList.append(answer)

    for index, line in enumerate(open(path_train)):
        if index == 0:
            continue
        lst = line.strip('\n').split('\t')
        correctAnswer = lst[2]
        train_answerList.append(correctAnswer)

    for i in range(0, answer_number):
        if (model_answerList[i] == train_answerList[i]):
            right_answer += 1

    print util.norm(float(right_answer) / answer_number)
def power(a, u_0, w, e, m):
    u_n = u_0
    for i in range(1, m+1):
        u_next = a.dot(u_n)
        u_next /= util.norm(u_next)
        tolerance = util.norm_inf(u_next - u_n)
        if tolerance <= e:
            w_dot_u = np.transpose(w).dot(u_n)
            # if w_dot_u == 0:
            #     return None, None, i
            eigenvalue = np.transpose(w).dot(a.dot(u_n)) / w_dot_u
            eigenvector = u_next
            return eigenvalue, eigenvector, i
        u_n = u_next
    return None, None, m
Пример #31
0
def flow2hsv(flow, clip=None, clip_pctile=None):
    flowmag = util.norm(flow, axis=2)
    flowang = np.arctan2(util.div_nonz(flow[:,:,1,...],flowmag),
                         util.div_nonz(flow[:,:,0,...],flowmag)) / (2*np.pi) + 0.5
    if clip_pctile is not None:
        clip = np.percentile(flowmag, clip_pctile)
    if clip is None:
        clip = np.amax(flowmag)
    shp = list(flow.shape)
    shp[2] = 1
    extra = np.ones(shp, dtype=flow.dtype)


    flow_pad = np.concatenate((flowang[:,:,np.newaxis,...], extra, flowmag[:,:,np.newaxis,...]/clip), axis=2)
    #flow_pad = np.pad(flow, padw, mode='constant', constant_values=clip)
    return util.hsv2rgb(np.clip(flow_pad,0,1))
Пример #32
0
    def visual_servo(self):
        if self.target is None:
            self.stop()
            return STOP

        delta = diff((self.center[0], self.center[1] + self.correction),
                     self.target)
        if norm(delta) < TOLERANCE:
            self.stop()
            return STOP
        else:
            current_cmd = 0

            cur_time = time.time()
            timeout_x = 0.0
            timeout_y = 0.0

            cmd_to_add = None
            if delta[0] < -SUBTOLERANCE_X:
                cmd_to_add = LEFT
            elif delta[0] > SUBTOLERANCE_X:
                cmd_to_add = RIGHT

            if cmd_to_add:
                timeout_x = min(
                    abs(delta[0]) * P_MOVE_SLEEP_X, MAX_MOVE_SLEEP_TIME_Y)
                print "move time x: ", abs(delta[0]) * P_MOVE_SLEEP_X
                current_cmd |= cmd_to_add

            cmd_to_add = None
            if delta[1] < -SUBTOLERANCE_Y:
                cmd_to_add = UP
            elif delta[1] > SUBTOLERANCE_Y:
                cmd_to_add = DOWN

            if cmd_to_add:
                timeout_y = min(
                    abs(delta[1]) * P_MOVE_SLEEP_Y, MAX_MOVE_SLEEP_TIME_Y)
                current_cmd |= cmd_to_add

            self.send_cmd_fn(timeout_x + cur_time, timeout_y + cur_time,
                             current_cmd)
            return current_cmd
Пример #33
0
def skinned_param(n, m, Q):
    ''' Compute parameter values suitable for skinning. '''
    Ub = np.zeros(m + 1)
    clk = np.zeros((n + 1, m + 1))
    di = np.zeros(n + 1)
    Pki = Q[0]
    for k in xrange(1, m + 1):
        Pk = Q[k]
        for i in xrange(n + 1):
            clk[i, k] = util.norm(Pk[i] - Pki[i - 1])
        Pki = Pk
    for i in xrange(n + 1):
        di[i] = np.sum(clk[i])
    for k in xrange(1, m):
        sumi = 0.0
        for i in xrange(n + 1):
            sumi += clk[i, k] / di[i]
        Ub[k] = Ub[k - 1] + 1.0 / (n + 1) * sumi
    Ub[m] = 1.0
    return Ub
Пример #34
0
    def compare(self, other):
        """
        Comparação heurística, gulosa e tolerante entre conjuntos de autores

        Retorna uma distância normalizada. Quanto menor a distância,
        mais similares os conjuntos.
        """
        # Encontra IDs de autoridade que estejam em ambos os conjuntos
        a, b = (set(x.id for x in xs) for xs in (self, other))
        commonIds = a.intersection(b) - {None,}
        # Tenta uma comparação entre nomes usando cada um dos campos
        # de nome (cn - nome em citações, fn - nome completo)
        results = []
        for f in (lambda x:x.cn, lambda x:x.fn):
            # Obtém duas listas de nomes, excluindo os que possuem IDs
            # de autoridade em comum
            a, b = ([util.norm(nameReorder(f(x)), util.NormLevel.ONLY_LETTERS)
                     for x in xs if x.id not in commonIds]
                    for xs in (self, other))
            results.append(self._compareNames(a, b))
        return min(results) / min(len(self), len(other))
Пример #35
0
def test_shape_no_noise():

    np.random.seed(100)

    # Generate some synthetic data.
    n_frames = 50
    n_basis = 2
    n_points = 15
    gt_model = model.generate_synthetic_model(n_frames, n_basis, n_points)

    W = gt_model.W

    # Recover the model
    inf_model = shape.factor(W, n_basis=n_basis)

    # Register with ground truth.
    inf_model.register(gt_model)

    delta = util.norm(inf_model.Ps - gt_model.Ps, axis=1)

    # Ensure the average error is low.
    npt.assert_allclose(delta.mean(), 0, atol=0.1)
Пример #36
0
def compare(inf, gt, visualize=True):
    print 'Comparing two models:'
    print 'Model 1:', str(inf)
    print 'Model 2:', str(gt)

    assert inf.n_frames == gt.n_frames
    assert inf.n_points == inf.n_points

    P_gt = gt.Ps
    P_inf = inf.Ps

    # Get scale of data as defined in Dai2012.
    sigma = np.std(P_gt, axis=-1).mean()

    # This is the average 3D error rescaled by the scale of the data.
    # I think this is close to what is being used in Dai2012.
    err = norm(P_gt - P_inf, axis=1).mean() / sigma
    print 'Scaled 3D error', err

    if visualize:
        visualize_models([gt, inf])

    return err
Пример #37
0
def compare(inf, gt, visualize=True):
    print "Comparing two models:"
    print "Model 1:", str(inf)
    print "Model 2:", str(gt)

    assert inf.n_frames == gt.n_frames
    assert inf.n_points == inf.n_points

    P_gt = gt.Ps
    P_inf = inf.Ps

    # Get scale of data as defined in Dai2012.
    sigma = np.std(P_gt, axis=-1).mean()

    # This is the average 3D error rescaled by the scale of the data.
    # I think this is close to what is being used in Dai2012.
    err = norm(P_gt - P_inf, axis=1).mean() / sigma
    print "Scaled 3D error", err

    if visualize:
        visualize_models([gt, inf])

    return err
Пример #38
0
    'washo': 'was',
    'gales': 'wel',
    'linguas sorabias': 'wen',
    'valao': 'wln',
    'wolof': 'wol',
    'calmuc': 'xal',
    'xhosa': 'xho',
    'yao': 'yao',
    'yapes': 'yap',
    'iidiche': 'yid',
    'ioruba': 'yor',
    'yupique': 'ypk',
    'zapoteca': 'zap',
    'zenaga': 'zen',
    'zhuang': 'zha',
    'chines': 'chi',
    'zande': 'znd',
    'zulu': 'zul',
    'zunhi': 'zun',
}

def lookup(idiomaLattes):
    return lattesToISO639.get(util.norm(idiomaLattes))

if __name__ == '__main__':
    # Verifica consistência da tabela
    import re
    for k, v in lattesToISO639.iteritems():
        assert(k == util.norm(k))         # sem acentos
        assert(re.match('^[a-z]{3}$', v)) # 3 letras
Пример #39
0
import numpy as np
import util
import collections
import pickle

# Read data from pickle files

f1 = open('dictionary_simple.pckl', 'r')
f2 = open('vec_simple.pckl', 'r')
dic = pickle.load(f1)
vecs = pickle.load(f2)
f1.close()
f2.close()
wordlist = list(dic.keys())

str1 = 'king'
str2 = 'queen'
str3 = 'food'
str4 = 'tree'
# vec1 = vecs[wordlist.index(str1)]
# vec2 = vecs[wordlist.index(str2)]
# vec3 = vecs[wordlist.index(str3)]
# vec4 = vecs[wordlist.index(str4)]
#print(util.findWordsCosine(vec1, vec2))
print(util.norm(vec2))
print(np.dot(vec1,vec3))
Пример #40
0
    def forwardProp(self,allKids,words_embedded,updateWlab,label,theta,freq):
        (W1,W2,W3,W4,Wlab,b1,b2,b3,blab,WL)=self.getParams(theta)
        sl=np.size(words_embedded,1)
        sentree=rnntree.rnntree(self.d,sl,words_embedded)
        collapsed_sentence = range(sl)
        if updateWlab:
            temp_label=np.zeros(self.cat)
            temp_label[label-1]=1.0
            nodeUnder = np.ones([2*sl-1,1])

            for i in range(sl,2*sl-1): # calculate n1, n2 and n1+n2 for each node in the sensentree and store in nodeUnder
                kids = allKids[i]
                n1 = nodeUnder[kids[0]]
                n2 = nodeUnder[kids[1]]
                nodeUnder[i] = n1+n2

            cat_size=self.cat
            sentree.catDelta = np.zeros([cat_size, 2*sl-1])
            sentree.catDelta_out = np.zeros([self.d,2*sl-1])

            # classifier on single words
            for i in range(sl):
                sm = softmax(np.dot(Wlab,words_embedded[:,i]) + blab)
                lbl_sm = (1-self.alpha)*(temp_label - sm)
                sentree.nodeScores[i] = 1.0/2.0*(np.dot(lbl_sm,(temp_label- sm)))
                sentree.catDelta[:, i] = -np.dot(lbl_sm,softmax_prime(sm))

            # sm = sigmoid(self.Wlab*words_embedded + self.blab)

            #lbl_sm = (1-self.alpha)*(label[:,np.ones(sl,1)] - sm)
            #sentree.nodeScores[:sl] = 1/2*(lbl_sm.*(label(:,ones(sl,1)) - sm))
            #sentree.catDelta[:, :sl] = -(lbl_sm).*sigmoid_prime(sm)

            for i in range(sl,2*sl-1):
                kids = allKids[i]

                c1 = sentree.nodeFeatures[:,kids[0]]
                c2 = sentree.nodeFeatures[:,kids[1]]

                # Eq. [2] in the paper: p = f(W[1][c1 c2] + b[1])
                p = tanh(np.dot(W1,c1) + np.dot(W2,c2) + b1)

                # See last paragraph in Section 2.3
                p_norm1 = p/norm(p)

                # Eq. (7) in the paper (for special case of 1d label)
                #sm = sigmoid(np.dot(Wlab,p_norm1) + blab)
                sm=softmax(np.dot(Wlab,p_norm1) + blab)
                beta=0.5
                #lbl_sm = beta * (1.0-self.alpha)*(label - sm)
                lbl_sm = beta * (1.0-self.alpha)*(temp_label - sm)
                #lbl_sm = beta * (1.0-self.alpha) * (temp_label-sm)
                #sentree.catDelta[:, i] = -softmax_prime(sm)[:,label-1]
                #J=-(1.0-self.alpha)*np.log(sm[label-1])
                #sentree.catDelta[:, i] = -np.dot(lbl_sm,sigmoid_prime(sm))
                sentree.catDelta[:, i] = -np.dot(lbl_sm,softmax_prime(sm))
                #J = 1.0/2.0*(np.dot(lbl_sm,(label - sm)))
                J = 1.0/2.0*(np.dot(lbl_sm,(temp_label - sm)))

                sentree.nodeFeatures[:,i] = p_norm1
                sentree.nodeFeatures_unnormalized[:,i] = p
                sentree.nodeScores[i] = J
                sentree.numkids = nodeUnder

            sentree.kids = allKids
        else:
            # Reconstruction Error
            for j in range(sl-1):
                size2=np.size(words_embedded,1)
                c1 = words_embedded[:,0:-1]
                c2 = words_embedded[:,1:]

                freq1 = freq[0:-1]
                freq2 = freq[1:]

                p = tanh(np.dot(W1,c1) + np.dot(W2,c2) + np.reshape(b1,[self.d,1])*([1]*(size2-1)))
                p_norm1 =p/np.sqrt(sum(p**2))

                y1_unnormalized = tanh(np.dot(W3,p_norm1) + np.reshape(b2,[self.d,1])*([1]*(size2-1)))
                y2_unnormalized = tanh(np.dot(W4,p_norm1) + np.reshape(b3,[self.d,1])*([1]*(size2-1)))

                y1 = y1_unnormalized/np.sqrt(sum(y1_unnormalized**2))
                y2 = y2_unnormalized/np.sqrt(sum(y2_unnormalized**2))

                y1c1 = self.alpha*(y1-c1)
                y2c2 = self.alpha*(y2-c2)

                # Eq. (4) in the paper: reconstruction error
                J = 1.0/2.0*sum((y1c1)*(y1-c1) + (y2c2)*(y2-c2))

                # finding the pair with smallest reconstruction error for constructing sentree
                J_min= min(J)
                J_minpos=np.argmin(J)

                sentree.node_y1c1[:,sl+j] = y1c1[:,J_minpos]
                sentree.node_y2c2[:,sl+j] = y2c2[:,J_minpos]
                sentree.nodeDelta_out1[:,sl+j] = np.dot(norm1tanh_prime(y1_unnormalized[:,J_minpos]) , y1c1[:,J_minpos])
                sentree.nodeDelta_out2[:,sl+j] = np.dot(norm1tanh_prime(y2_unnormalized[:,J_minpos]) , y2c2[:,J_minpos])

                words_embedded=np.delete(words_embedded,J_minpos+1,1)
                words_embedded[:,J_minpos]=p_norm1[:,J_minpos]
                sentree.nodeFeatures[:, sl+j] = p_norm1[:,J_minpos]
                sentree.nodeFeatures_unnormalized[:, sl+j]= p[:,J_minpos]
                sentree.nodeScores[sl+j] = J_min
                sentree.pp[collapsed_sentence[J_minpos]] = sl+j
                sentree.pp[collapsed_sentence[J_minpos+1]] = sl+j
                sentree.kids[sl+j,:] = [collapsed_sentence[J_minpos], collapsed_sentence[J_minpos+1]]
                sentree.numkids[sl+j] = sentree.numkids[sentree.kids[sl+j,0]] + sentree.numkids[sentree.kids[sl+j,1]]


                freq=np.delete(freq,J_minpos+1)
                freq[J_minpos] = (sentree.numkids[sentree.kids[sl+j,0]]*freq1[J_minpos] + sentree.numkids[sentree.kids[sl+j,1]]*freq2[J_minpos])/(sentree.numkids[sentree.kids[sl+j,0]]+sentree.numkids[sentree.kids[sl+j,1]])

                collapsed_sentence=np.delete(collapsed_sentence,J_minpos+1)
                collapsed_sentence[J_minpos]=sl+j
        return sentree
Пример #41
0
def point_set_error(Ps, Qs):
    return norm(Ps - Qs, axis=1).mean(axis=-1)
Пример #42
0
# data = np.loadtxt(df)

fname = env.dataset([f for f in os.listdir(env.dataset()) if f.endswith(".wav")][0])
df = env.run("test_data.pkl")

if not os.path.exists(df):
    song_data_raw, source_sr = lr.load(fname)
    print "Got sampling rate {}, resampling to {} ...".format(source_sr, c.target_sr)
    song_data = lr.resample(song_data_raw, source_sr, c.target_sr, scale=True)
    song_data = song_data[:30000,]

    np.save(open(df, "w"), song_data)
else:
    song_data = np.load(open(df))

data, data_denom = norm(song_data)


sess = tf.Session()
saver = tf.train.Saver()

model_fname = env.run("conv_rnn_model.ckpt")
if os.path.exists(model_fname):
    print "Restoring from {}".format(model_fname)
    saver.restore(sess, model_fname)
    epochs = 0
else:
    sess.run(tf.initialize_all_variables())


[ os.remove(f) for f in glob("{}/*.png".format(env.run())) ]
Пример #43
0
    'linguas sorabias': 'wen',
    'valao': 'wln',
    'wolof': 'wol',
    'calmuc': 'xal',
    'xhosa': 'xho',
    'yao': 'yao',
    'yapes': 'yap',
    'iidiche': 'yid',
    'ioruba': 'yor',
    'yupique': 'ypk',
    'zapoteca': 'zap',
    'zenaga': 'zen',
    'zhuang': 'zha',
    'chines': 'chi',
    'zande': 'znd',
    'zulu': 'zul',
    'zunhi': 'zun',
}


def lookup(idiomaLattes):
    return lattesToISO639.get(util.norm(idiomaLattes))


if __name__ == '__main__':
    # Verifica consistência da tabela
    import re
    for k, v in lattesToISO639.iteritems():
        assert (k == util.norm(k))  # sem acentos
        assert (re.match('^[a-z]{3}$', v))  # 3 letras
Пример #44
0
    def forwardProp(self,allKids,words_embedded,updateWlab,label,theta,freq):
        #allkids存的是所有节点,第i行存第i个节点,列表示第i行节点所包含的子节点
        (W1,W2,W3,W4,Wlab,b1,b2,b3,blab,WL)=self.getParams(theta)
        #s1可能是词汇表的大小        
        sl=np.size(words_embedded,1)
        sentree=rnntree.rnntree(self.d,sl,words_embedded)
        collapsed_sentence = range(sl)
        #计算情感误差
        if updateWlab:
            temp_label=np.zeros(self.cat)
            #label表示当前标签,label-1主要是因为list从0开始,即当前标签的位置为1
            temp_label[label-1]=1.0
            nodeUnder = np.ones([2*sl-1,1])
            #n1,n2是kids的子节点数
            for i in range(sl,2*sl-1): # calculate n1, n2 and n1+n2 for each node in the sensentree and store in nodeUnder
                kids = allKids[i] 
                n1 = nodeUnder[kids[0]] #左节点
                n2 = nodeUnder[kids[1]] #右节点
                nodeUnder[i] = n1+n2    #第i个节点的子节点数目

            cat_size=self.cat
            sentree.catDelta = np.zeros([cat_size, 2*sl-1])
            sentree.catDelta_out = np.zeros([self.d,2*sl-1])

            # classifier on single words
            for i in range(sl):
                sm = softmax(np.dot(Wlab,words_embedded[:,i]) + blab)
                #这里代码部分计算情感误差和论文不太一样,这里直接用yi-h(x)来表示情感误差
                lbl_sm = (1-self.alpha)*(temp_label - sm)
                #这里貌似是在计算J
                sentree.nodeScores[i] = 1.0/2.0*(np.dot(lbl_sm,(temp_label- sm)))  #sentree.nodeScores分为2个部分,这里计算0-s1,下面计算2*s1-1
                sentree.catDelta[:, i] = -np.dot(lbl_sm,softmax_prime(sm))

            # sm = sigmoid(self.Wlab*words_embedded + self.blab)

            #lbl_sm = (1-self.alpha)*(label[:,np.ones(sl,1)] - sm)
            #sentree.nodeScores[:sl] = 1/2*(lbl_sm.*(label(:,ones(sl,1)) - sm))
            #sentree.catDelta[:, :sl] = -(lbl_sm).*sigmoid_prime(sm)

            for i in range(sl,2*sl-1):
                #kids,c1,c2 是什么
                kids = allKids[i]

                c1 = sentree.nodeFeatures[:,kids[0]]   #左孩子的词向量
                c2 = sentree.nodeFeatures[:,kids[1]]   #右孩子的词向量

                # Eq. [2] in the paper: p = f(W[1][c1 c2] + b[1])
                p = tanh(np.dot(W1,c1) + np.dot(W2,c2) + b1)

                # See last paragraph in Section 2.3
                p_norm1 = p/norm(p)

                # Eq. (7) in the paper (for special case of 1d label)
                #sm = sigmoid(np.dot(Wlab,p_norm1) + blab)
                sm=softmax(np.dot(Wlab,p_norm1) + blab)
                beta=0.5  #论文里面本来是没有beta这个值的
                #lbl_sm = beta * (1.0-self.alpha)*(label - sm)
                lbl_sm = beta * (1.0-self.alpha)*(temp_label - sm)
                #lbl_sm = beta * (1.0-self.alpha) * (temp_label-sm)
                #sentree.catDelta[:, i] = -softmax_prime(sm)[:,label-1]
                #J=-(1.0-self.alpha)*np.log(sm[label-1])
                #sentree.catDelta[:, i] = -np.dot(lbl_sm,sigmoid_prime(sm))
                sentree.catDelta[:, i] = -np.dot(lbl_sm,softmax_prime(sm))
                #J = 1.0/2.0*(np.dot(lbl_sm,(label - sm)))
                J = 1.0/2.0*(np.dot(lbl_sm,(temp_label - sm)))

                sentree.nodeFeatures[:,i] = p_norm1
                sentree.nodeFeatures_unnormalized[:,i] = p
                sentree.nodeScores[i] = J
                sentree.numkids = nodeUnder

            sentree.kids = allKids
        #计算重构误差
        else:
            # Reconstruction Error
            for j in range(sl-1):
                size2=np.size(words_embedded,1)
                c1 = words_embedded[:,0:-1] 
                c2 = words_embedded[:,1:]

                freq1 = freq[0:-1]
                freq2 = freq[1:]

                p = tanh(np.dot(W1,c1) + np.dot(W2,c2) + np.reshape(b1,[self.d,1])*([1]*(size2-1)))
                p_norm1 =p/np.sqrt(sum(p**2))
                #下方y1,y2实际上就是论文的c1,c2,由p分解而来。
                y1_unnormalized = tanh(np.dot(W3,p_norm1) + np.reshape(b2,[self.d,1])*([1]*(size2-1)))
                y2_unnormalized = tanh(np.dot(W4,p_norm1) + np.reshape(b3,[self.d,1])*([1]*(size2-1)))

                y1 = y1_unnormalized/np.sqrt(sum(y1_unnormalized**2))
                y2 = y2_unnormalized/np.sqrt(sum(y2_unnormalized**2))

                y1c1 = self.alpha*(y1-c1)
                y2c2 = self.alpha*(y2-c2)

                # Eq. (4) in the paper: reconstruction error:重构误差
                #(y1-c1)*(y1-c1)的结果是一个数值
                J = 1.0/2.0*sum((y1c1)*(y1-c1) + (y2c2)*(y2-c2))
                
                #这个for循环的下面部分没看懂
                # finding the pair with smallest reconstruction error for constructing sentree
                #min(J)是什么意思,J是一个值
                J_min= min(J)
                J_minpos=np.argmin(J)
                #重构误差最小的重构向量存入树中(c1',c2')
                sentree.node_y1c1[:,sl+j] = y1c1[:,J_minpos]
                sentree.node_y2c2[:,sl+j] = y2c2[:,J_minpos]
                #可能是更新值
                sentree.nodeDelta_out1[:,sl+j] = np.dot(norm1tanh_prime(y1_unnormalized[:,J_minpos]) , y1c1[:,J_minpos])
                sentree.nodeDelta_out2[:,sl+j] = np.dot(norm1tanh_prime(y2_unnormalized[:,J_minpos]) , y2c2[:,J_minpos])

                words_embedded=np.delete(words_embedded,J_minpos+1,1)
                words_embedded[:,J_minpos]=p_norm1[:,J_minpos]
                sentree.nodeFeatures[:, sl+j] = p_norm1[:,J_minpos]
                sentree.nodeFeatures_unnormalized[:, sl+j]= p[:,J_minpos]
                sentree.nodeScores[sl+j] = J_min
                sentree.pp[collapsed_sentence[J_minpos]] = sl+j
                sentree.pp[collapsed_sentence[J_minpos+1]] = sl+j
                sentree.kids[sl+j,:] = [collapsed_sentence[J_minpos], collapsed_sentence[J_minpos+1]]
                sentree.numkids[sl+j] = sentree.numkids[sentree.kids[sl+j,0]] + sentree.numkids[sentree.kids[sl+j,1]]


                freq=np.delete(freq,J_minpos+1)
                freq[J_minpos] = (sentree.numkids[sentree.kids[sl+j,0]]*freq1[J_minpos] + sentree.numkids[sentree.kids[sl+j,1]]*freq2[J_minpos])/(sentree.numkids[sentree.kids[sl+j,0]]+sentree.numkids[sentree.kids[sl+j,1]])

                collapsed_sentence=np.delete(collapsed_sentence,J_minpos+1)
                collapsed_sentence[J_minpos]=sl+j
        return sentree
Пример #45
0
    def test_data(self):
        p = []
        f = []
        a = []
        t = []
        count = random.randint(0,80)
        pcount = random.randint(0,80)
        last_pos = np.load(self.path + str(count) + "/" + str(pcount) + ".npz")["pos"]

        for i in range(self.batch_size): 
            ## take action
            try_count = count
            try_pcount = pcount


            choice = random.randint(0,1)
            ## translate
            if choice == 0:
                if int(try_count/9)==0:
                    if try_count==0:
                        act = random.randint(0,1)
                        if act==0:
                            try_count+=1
                            try_pcount = 80 - try_pcount
                        if act==1:
                            try_count+=17
                            try_pcount = 80 - try_pcount
                    elif try_count==8:
                        act = random.randint(0,1)
                        if act==0:
                            try_count-=1
                            try_pcount = 80 - try_pcount
                        if act==1:
                            try_count+=1
                            try_pcount = 80 - try_pcount
                    else:
                        act = random.randint(0,2)
                        if act==0:
                            try_count-=1
                            try_pcount = 80 - try_pcount
                        if act==1:
                            try_count+=1
                            try_pcount = 80 - try_pcount
                        if act==2:
                            try_count=17-try_count
                            try_pcount = 80 - try_pcount
                elif int(try_count/9)==8:
                    if try_count==72:
                        act = random.randint(0,1)
                        if act==0:
                            try_count+=1
                            try_pcount = 80 - try_pcount
                        if act==1:
                            try_count+=1
                            try_pcount = 80 - try_pcount
                    elif try_count==80:
                        act = random.randint(0,1)
                        if act==0:
                            try_count-=1
                            try_pcount = 80 - try_pcount
                        if act==1:
                            try_count-=17
                            try_pcount = 80 - try_pcount
                    else:
                        act = random.randint(0,2)
                        if act==0:
                            try_count-=1
                            try_pcount = 80 - try_pcount
                        if act==1:
                            try_count+=1
                            try_pcount = 80 - try_pcount
                        if act==2:
                            try_count=143-try_count
                            try_pcount = 80 - try_pcount
                elif int(try_count/9)%2==0:
                    if try_count==int(try_count/9)*9:
                        act = random.randint(0, 2)
                        if act == 0:
                            try_count -= 1
                            try_pcount = 80 - try_pcount
                        elif act == 1:
                            try_count += 1
                            try_pcount = 80 - try_pcount
                        elif act == 2:
                            try_count += 17
                            try_pcount = 80 - try_pcount
                    elif try_count==int(try_count/9)*9+8:
                        act = random.randint(0, 2)
                        if act == 0:
                            try_count -= 17
                            try_pcount = 80 - try_pcount
                        elif act == 1:
                            try_count -= 1
                            try_pcount = 80 - try_pcount
                        elif act == 2:
                            try_count += 1
                            try_pcount = 80 - try_pcount
                    else:
                        act = random.randint(0, 3)
                        if act == 0:
                            try_count -= 1
                            try_pcount = 80 - try_pcount
                        elif act == 1:
                            try_count += 1
                            try_pcount = 80 - try_pcount
                        elif act == 2:
                            try_count = ((int(try_count/9)*2+2)*9-1)-try_count
                            try_pcount = 80 - try_pcount
                        elif act == 3:
                            try_count = ((int(try_count/9)*2)*9-1)-try_count
                            try_pcount = 80 - try_pcount
                else:
                    if try_count==int(try_count/9)*9:
                        act = random.randint(0, 2)
                        if act == 0:
                            try_count -= 1
                            try_pcount = 80 - try_pcount
                        elif act == 1:
                            try_count += 1
                            try_pcount = 80 - try_pcount
                        elif act == 2:
                            try_count += 17
                            try_pcount = 80 - try_pcount
                    elif try_count==int(try_count/9)*9+8:
                        act = random.randint(0, 2)
                        if act == 0:
                            try_count -= 17
                            try_pcount = 80 - try_pcount
                        elif act == 1:
                            try_count -= 1
                            try_pcount = 80 - try_pcount
                        elif act == 2:
                            try_count += 1
                            try_pcount = 80 - try_pcount
                    else:
                        act = random.randint(0, 3)
                        if act == 0:
                            try_count -= 1
                            try_pcount = 80 - try_pcount
                        elif act == 1:
                            try_count += 1
                            try_pcount = 80 - try_pcount
                        elif act == 2:
                            try_count = ((int(try_count/9)*2)*9-1)-try_count
                            try_pcount = 80 - try_pcount
                        elif act == 3:
                            try_count = ((int(try_count/9)*2+2)*9-1)-try_count
                            try_pcount = 80 - try_pcount
            ## rotate
            else:
                if int(try_pcount/9)==0:
                    if try_pcount==0:
                        act = random.randint(0,1)
                        if act==0:
                            try_pcount+=1
                        if act==1:
                            try_pcount+=17
                    elif try_pcount==8:
                        act = random.randint(0,1)
                        if act==0:
                            try_pcount-=1
                        if act==1:
                            try_pcount+=1
                    else:
                        act = random.randint(0,2)
                        if act==0:
                            try_pcount-=1
                        if act==1:
                            try_pcount+=1
                        if act==2:
                            try_pcount=17-try_pcount
                elif int(try_pcount/9)==8:
                    if try_pcount==72:
                        act = random.randint(0,1)
                        if act==0:
                            try_pcount+=1
                        if act==1:
                            try_pcount+=1
                    elif try_pcount==80:
                        act = random.randint(0,1)
                        if act==0:
                            try_pcount-=1
                        if act==1:
                            try_pcount-=17
                    else:
                        act = random.randint(0,2)
                        if act==0:
                            try_pcount-=1
                        if act==1:
                            try_pcount+=1
                        if act==2:
                            try_pcount=143-try_pcount
                elif int(try_pcount/9)%2==0:
                    if try_pcount==int(try_pcount/9)*9:
                        act = random.randint(0, 2)
                        if act == 0:
                            try_pcount -= 1
                        elif act == 1:
                            try_pcount += 1
                        elif act == 2:
                            try_pcount += 17
                    elif try_pcount==int(try_pcount/9)*9+8:
                        act = random.randint(0, 2)
                        if act == 0:
                            try_pcount -= 17
                        elif act == 1:
                            try_pcount -= 1
                        elif act == 2:
                            try_pcount += 1
                    else:
                        act = random.randint(0, 3)
                        if act == 0:
                            try_pcount -= 1
                        elif act == 1:
                            try_pcount += 1
                        elif act == 2:
                            try_pcount = ((int(try_pcount/9)*2+2)*9-1)-try_pcount
                        elif act == 3:
                            try_pcount = ((int(try_pcount/9)*2)*9-1)-try_pcount
                else:
                    if try_pcount==int(try_pcount/9)*9:
                        act = random.randint(0, 2)
                        if act == 0:
                            try_pcount -= 1
                        elif act == 1:
                            try_pcount += 1
                        elif act == 2:
                            try_pcount += 17
                    elif try_pcount==int(try_pcount/9)*9+8:
                        act = random.randint(0, 2)
                        if act == 0:
                            try_pcount -= 17
                        elif act == 1:
                            try_pcount -= 1
                        elif act == 2:
                            try_pcount += 1
                    else:
                        act = random.randint(0, 3)
                        if act == 0:
                            try_pcount -= 1
                        elif act == 1:
                            try_pcount += 1
                        elif act == 2:
                            try_pcount = ((int(try_pcount/9)*2)*9-1)-try_pcount
                        elif act == 3:
                            try_pcount = ((int(try_pcount/9)*2+2)*9-1)-try_pcount                

            count = try_count
            pcount = try_pcount   
            #print(self.path + str(count) + "/" + str(pcount) + ".jpg") 
            pic =  norm(cv2.imread(self.test_path + str(count) + "/" + str(pcount) + ".jpg") - self.reference_test)
            pic = cv2.resize(pic, (160,120))
            force = np.array([np.load(self.test_path + str(count) + "/" + str(pcount) + ".npz")["force"]])
            pos = np.array([np.load(self.test_path + str(count) + "/" + str(pcount) + ".npz")["pos"]])
            action = pos - last_pos
            last_pos = copy.deepcopy(pos)
            target = pos
            p.append(copy.deepcopy(pic))
            f.append(copy.deepcopy(force))
            a.append(copy.deepcopy(action))
            t.append(copy.deepcopy(target))

        return p,f,a,t
    def add_prediction_op(self):
        """Adds the unrolled RNN:
            h_0 = 0
            for t in 1 to T:
                o_t, h_t = cell(x_t, h_{t-1})
                o_drop_t = Dropout(o_t, dropout_rate)
                y_t = o_drop_t U + b_2
        TODO: There a quite a few things you'll need to do in this function:
            - Define the variables U, b_2.
            - Define the vector h as a constant and inititalize it with
              zeros. See tf.zeros and tf.shape for information on how
              to initialize this variable to be of the right shape.
              https://www.tensorflow.org/api_docs/python/constant_op/constant_value_tensors#zeros
              https://www.tensorflow.org/api_docs/python/array_ops/shapes_and_shaping#shape
            - In a for loop, begin to unroll the RNN sequence. Collect
              the predictions in a list.
            - When unrolling the loop, from the second iteration
              onwards, you will HAVE to call
              tf.get_variable_scope().reuse_variables() so that you do
              not create new variables in the RNN cell.
              See https://www.tensorflow.org/versions/master/how_tos/variable_scope/
            - Concatenate and reshape the predictions into a predictions
              tensor.
        Hint: You will find the function tf.pack (similar to np.asarray)
              useful to assemble a list of tensors into a larger tensor.
              https://www.tensorflow.org/api_docs/python/array_ops/slicing_and_joining#pack
        Hint: You will find the function tf.transpose and the perms
              argument useful to shuffle the indices of the tensor.
              https://www.tensorflow.org/api_docs/python/array_ops/slicing_and_joining#transpose
        Remember:
            * Use the xavier initilization for matrices.
            * Note that tf.nn.dropout takes the keep probability (1 - p_drop) as an argument.
            The keep probability should be set to the value of self.dropout_placeholder
        Returns:
            pred: tf.Tensor of shape (batch_size, max_length, n_classes)
        """
        x1, x2 = self.add_embedding()
        dropout_rate = self.dropout_placeholder

        # choose cell type
        if self.config.cell == "rnn":
            cell = RNNCell(self.config.embed_size, self.config.hidden_size)
        elif self.config.cell == "gru":
            cell = GRUCell(self.config.embed_size, self.config.hidden_size)
        elif self.config.cell == "lstm":
            cell = LSTMCell(self.config.embed_size, self.config.hidden_size)
        else:
            raise ValueError("Unsuppported cell type: " + self.config.cell)

        # Initialize hidden states to zero vectors of shape (num_examples, hidden_size)
        h1 = tf.zeros((tf.shape(x1)[0], self.config.hidden_size), tf.float32)
        h2 = tf.zeros((tf.shape(x2)[0], self.config.hidden_size), tf.float32)

        with tf.variable_scope("RNN1") as scope:
            for time_step in range(self.helper.max_length):
                if time_step != 0:
                    scope.reuse_variables()
                o1_t, h1 = cell(x1[:, time_step, :], h1, scope)
        with tf.variable_scope("RNN2") as scope:
            for time_step in range(self.helper.max_length):
                if time_step != 0:
                    scope.reuse_variables()
                o2_t, h2 = cell(x2[:, time_step, :], h2, scope)

        # h_drop1 = tf.nn.dropout(h1, dropout_rate)
        # h_drop2 = tf.nn.dropout(h2, dropout_rate)

        # use L2-regularization: sum of squares of all parameters

        if self.config.distance_measure == "l2":
            # perform logistic regression on l2-distance between h1 and h2
            distance = norm(h1 - h2 + 0.000001)
            logistic_a = tf.Variable(0.0, dtype=tf.float32, name="logistic_a")
            logistic_b = tf.Variable(0.0, dtype=tf.float32, name="logistic_b")
            self.regularization_term = tf.square(logistic_a) + tf.square(
                logistic_b)
            preds = tf.sigmoid(logistic_a * distance + logistic_b)

        elif self.config.distance_measure == "cosine":
            # perform logistic regression on cosine distance between h1 and h2
            distance = cosine_distance(h1 + 0.000001, h2 + 0.000001)
            logistic_a = tf.Variable(1.0, dtype=tf.float32, name="logistic_a")
            logistic_b = tf.Variable(0.0, dtype=tf.float32, name="logistic_b")
            self.regularization_term = tf.square(logistic_a) + tf.square(
                logistic_b)
            preds = tf.sigmoid(logistic_a * distance + logistic_b)

        elif self.config.distance_measure == "custom_coef":
            # perform logistic regression on the vector |h1-h2|,
            # equivalent to logistic regression on the (scalar) weighted Manhattan distance between h1 and h2,
            # ie. weighted sum of |h1-h2|
            logistic_a = tf.get_variable(
                "coef", [self.config.hidden_size], tf.float32,
                tf.contrib.layers.xavier_initializer())
            logistic_b = tf.Variable(0.0, dtype=tf.float32, name="logistic_b")
            self.regularization_term = tf.reduce_sum(
                tf.square(logistic_a)) + tf.square(logistic_b)
            preds = tf.sigmoid(
                tf.reduce_sum(logistic_a * tf.abs(h1 - h2), axis=1) +
                logistic_b)

        elif self.config.distance_measure == "concat":
            # use softmax for prediction
            U = tf.get_variable(
                "U", (4 * self.config.hidden_size, self.config.n_classes),
                tf.float32, tf.contrib.layers.xavier_initializer())
            b = tf.get_variable("b", (self.config.n_classes, ), tf.float32,
                                tf.constant_initializer(0))
            v = tf.nn.relu(tf.concat([h1, h2, tf.square(h1 - h2), h1 * h2], 1))
            self.regularization_term = tf.reduce_sum(
                tf.square(U)) + tf.reduce_sum(tf.square(b))
            preds = tf.matmul(v, U) + b

        elif self.config.distance_measure == "concat_steroids":
            # use softmax for prediction
            W1 = tf.get_variable(
                "W1", (4 * self.config.hidden_size, self.config.hidden_size),
                tf.float32, tf.contrib.layers.xavier_initializer())
            b1 = tf.get_variable("b1", (self.config.hidden_size, ), tf.float32,
                                 tf.constant_initializer(0))

            W2 = tf.get_variable(
                "W2", (self.config.hidden_size, self.config.n_classes),
                tf.float32, tf.contrib.layers.xavier_initializer())
            b2 = tf.get_variable("b2", (self.config.n_classes, ), tf.float32,
                                 tf.constant_initializer(0))

            v1 = tf.nn.relu(tf.concat(
                [h1, h2, tf.square(h1 - h2), h1 * h2], 1))
            v2 = tf.nn.relu(tf.matmul(v1, W1) + b1)

            self.regularization_term = tf.reduce_sum(
                tf.square(W1)) + tf.reduce_sum(tf.square(b1)) + tf.reduce_sum(
                    tf.square(W2)) + tf.reduce_sum(tf.square(b2))
            preds = tf.matmul(v2, W2) + b2

        else:
            raise ValueError("Unsuppported distance type: " +
                             self.config.distance_measure)

        return preds
Пример #47
0
def laser_of_euclid(points):
    """
       Take a matrix of points and return matrix of scans (1xn)
    """
    return ut.norm(points)
Пример #48
0
def irlba(A, K=6, largest=True,  adjust = 3, aug=None, disps=0, maxit=1000, m_b=20, 
          reorth_two = False, tol=1e-6, V0=None):
    """
    This is a port of IRLBA.m following the code there very closely
    """

    # FIXME do interchange stuff
    m, n = A.dims()
    
    # interchange m and n so that size(A*A) = min(m, n)
    # avoids finding zero values when searching for the smallest singular values
    
    interchange = False
    if n > m and largest == False:
        t = m
        m = n
        n = t
        interchange = True
        raise Exception("Don't do interchange yet")


    W = np.zeros((m, m_b))
    F = np.zeros((n, 1))
    V = np.zeros((n, m_b)) # Preallocate for V
    if V0 == None:
        V[:, 0] = np.arange(1, n+1) 
        V[:, 0] = V[:, 0] / np.sum(V[:, 0]) # np.random.normal(0, 1, n)
    else:
        V[:, :V0.shape[1]] = V0
    # increase the number of desired values by adjust to help increase convergence. 
    # K is re-adjusted as vectors converged. This is only an initial value of K
    K_org = K
    K += adjust

    # sanity checking for input values
    if K <= 0:
        raise Exception("K must be a positive Value")
    if K > min(n, m):
        raise Exception("K must be less than min(n, m) + %d" % adjust)
    if m_b <= 1:
        raise Exception("M_B must be > 1")

    # FIXME clean up parameters A LOT
    if aug == None:
        if largest == True:
            aug = 'RITZ'
        else:
            aug = 'HARM'

    # set tolerance to machine precision 
    tol = max(tol, EPS)
    
    # begin initialization
    B = []
    Bsz = []
    conv = False
    EPS23 = EPS**(2/3.0)
    iteration = 0
    J = 0
    mprod = 0
    R_F = []
    SQRTEPS = np.sqrt(EPS)
    Smax = 1 # holds the maximum value of all computed singular values of B est. ||A||_2
    Smin = [] # holds the minimum value of all computed singular values of B est. cond(A)
    SVTol = min(SQRTEPS, tol) # tolerance to determine whether a singular value has converged
    
    S_B = [] # singular values of B 
    U_B = [] # Left singular vectors of B
    V_B = [] # right singular vectors of B
    V_B_last = [] # holds the last row of the modified V_B
    S_B2 = [] # singular values of [B ||F||]
    U_B2 = [] # left singular vectors of [B ||F||]
    V_B2 = [] # right signular vectors of [b || F||] 

    
    
    while iteration < maxit:

        V, W, F, B, mprod = lanczos.ablanzbd(A, V, W, F, B, K, 
                                             interchange, m_b, n, m, SVTol*Smax, 
                                             reorth_two, iteration)

        # determine the size of the bidiagonal matrix B
        Bsz = B.shape[0]

        # compute the norm of the vector F, and normalize F
        R_F = norm(F)
        F = F/R_F

        # compute singular triplets of B
        U_B, S_B, V_B = ordered_svd(B)

        # estimate ||A|| using the largest singular value ofer all 
        # all iterations and estimate the cond(A) using approximations
        # to the largest and smallest singular values. If a small singular value
        # is less than sqrteps use only Rtiz vectors
        # to augment and require two-sided reorthogonalization 

        if iteration == 0:
            Smax = S_B[0]; 
            Smin = S_B[-1]
        else:
            Smax = max(Smax, S_B[0])
            Smin = min(Smin, S_B[-1])

        Smax = max(EPS23, Smax)

        if Smin/Smax < SQRTEPS:
            reorth_two = True
            aug = 'RITZ'
            
        # re-order the singular values if we're looking for the smallest ones
        if not largest:
            U_B = U_B[:, ::-1]
            S_B = S_B[::-1]
            V_B = V_B[:, ::-1]

        # compute the residuals

        R = np.dot(R_F, U_B[-1,:])

        # convergest tests and displays
        conv, U_B, S_B, V_B, K = convtests(Bsz, disps, tol, K_org, U_B, S_B, V_B, 
                                           abs(R), iteration, 
                                           K, SVTol, Smax)

        if conv: # all singular values within tolerance, return ! 
            break # yay
        if iteration > maxit:
            break # boo

        # compute starting vectors and first block:
        if aug == "HARM":
            # update the SVD of B to be the SVD of [B ||F||F E_m] 

            U_B2, S_B, V_B2 = ordered_svd(np.c_[np.diag(S_B), R.T])

            if not largest:
                # pick the last ones
                U_B2 = U_B2[:, :Bsz]
                V_B2 = V_B2[:, :Bsz]
                S_B = S_B[:Bsz]

                U_B2 = U_B2[:, ::-1]
                S_B = S_B[::-1]
                V_B2 = V_B2[:, ::-1]
                # jesus christ 

            U_B = np.dot(U_B, U_B2)

            VB_D = np.zeros((V_B.shape[0]+1, V_B.shape[1]+1))
            VB_D[:-1, :-1] = V_B
            VB_D[-1, -1] = 1.0
            V_B = np.dot(VB_D, V_B2)
            V_B_last = V_B[-1, :K] # the last row of V_B

            int_v = scipy.linalg.solve(B, np.flipud(np.eye(Bsz, 1)))
            s = np.dot(R_F, int_v)
            V_B = V_B[:Bsz, :] + s*V_B[Bsz:, :]

            # vectors are not orthogonal
            VB_D = np.zeros((V_B.shape[0] +1, K+1))
            VB_D[:-1, :K] = V_B[:, :K]
            VB_D[:-1, K] = -s.T
            VB_D[-1, -1] = 1.0
            V_B, R = np.linalg.qr(VB_D)
            V[:, :(K+1)] = np.dot(np.c_[V, F], V_B)
        
            # upate and compute the K x K+1 part of B

            w0 = np.outer(R[:, K], V_B_last)
            w = np.triu((R[:K+1, :K] + w0).T)

            B = np.dot(np.diag(S_B[:K]), w)

        else:
            V[:, :K] = np.dot(V, V_B[:, :K])
            V[:, K] = F
            B = np.c_[np.diag(S_B[:K]), R[:K]]

        # compute left approximate singular values
        W[:, :K] = np.dot(W, U_B[:, :K])

        iteration += 1

    # results

    if interchange:
        u = np.dot(V, V_B[:, :K_org])
        s = S_B[:K_org]
        v = np.dot(W, U_B[:, :K_org])

    else:

        u = np.dot(W, U_B[:, :K_org])    
        s = S_B[:K_org]
        v = np.dot(V, V_B[:, :K_org])

    return u, s, v
Пример #49
0
def point_set_error(Ps, Qs):
    return norm(Ps - Qs, axis=1).mean(axis=-1)
Пример #50
0
def lookup(idiomaLattes):
    return lattesToISO639.get(util.norm(idiomaLattes))
Пример #51
0
 def compute_length(self):
     spatial_vector = util.edge_to_vector(self.geospatials)
     self.length = util.norm(spatial_vector)
Пример #52
0
    def forwardProp(self,allKids,words_embedded,updateWlab,label,theta,freq):
        (W1,W2,W3,W4,Wlab,b1,b2,b3,blab,WL)=self.getParams(theta)
        #sl是words_embedded的个数,一句话单词的个数
        # allKids一开始没有值,是因为训练之前,语法树本来就没有构建完,树结构是训练完了以后才出现的。但是,allkids内容应该会随着算法的进行而变化
        sl=np.size(words_embedded,1)
        sentree=rnntree.rnntree(self.d,sl,words_embedded)
        collapsed_sentence = range(sl)

        # updateWlab主要是获得情感误差,修正情感的权值
        # 情感误差也是需要p作为输入的,因此也需要计算出p
        if updateWlab:
            temp_label=np.zeros(self.cat)
            #假设cat = 4, temp_label就是(0,0,0,0)。下面这句话的意思是label对应的位置为1
            temp_label[label-1]=1.0
            nodeUnder = np.ones([2*sl-1,1])

            # 这个for循环是计算出,某个节点底下一共有多少个子节点
            # kids存了两个值,分别代表左右孩子。
            # 可以推测出,allkids存的东西,allkids[i]代表第i个非叶子节点,allkids[i][0]是左孩子,allkids[i][1]是右孩子
            for i in range(sl,2*sl-1): # calculate n1, n2 and n1+n2 for each node in the sensentree and store in nodeUnder
                kids = allKids[i]
                n1 = nodeUnder[kids[0]]
                n2 = nodeUnder[kids[1]]
                nodeUnder[i] = n1+n2

            cat_size=self.cat
            sentree.catDelta = np.zeros([cat_size, 2*sl-1])
            sentree.catDelta_out = np.zeros([self.d,2*sl-1])

            # classifier on single words
            # 处理所有单词,即叶子节点
            # 这里有个问题就是,为什么叶子节点也要计算情感误差
            for i in range(sl):
                sm = softmax(np.dot(Wlab,words_embedded[:,i]) + blab)
                #这里不管情感误差是如何计算的,sentree.nodeScores存的是情感误差没错了。
                #sentree.catDelta存的什么不清楚,但是和情感误差有关
                lbl_sm = (1-self.alpha)*(temp_label - sm)
                sentree.nodeScores[i] = 1.0/2.0*(np.dot(lbl_sm,(temp_label- sm)))
                sentree.catDelta[:, i] = -np.dot(lbl_sm,softmax_prime(sm))

            # sm = sigmoid(self.Wlab*words_embedded + self.blab)

            #lbl_sm = (1-self.alpha)*(label[:,np.ones(sl,1)] - sm)
            #sentree.nodeScores[:sl] = 1/2*(lbl_sm.*(label(:,ones(sl,1)) - sm))
            #sentree.catDelta[:, :sl] = -(lbl_sm).*sigmoid_prime(sm)

            #超过sl的部分是单词的父亲节点
            for i in range(sl,2*sl-1):
                kids = allKids[i]
                #c1,c2,是左右孩子的向量
                c1 = sentree.nodeFeatures[:,kids[0]]
                c2 = sentree.nodeFeatures[:,kids[1]]

                # Eq. [2] in the paper: p = f(W[1][c1 c2] + b[1])
                #计算p,显然p是个数值,即得分,用于判断哪两个节点合并
                p = tanh(np.dot(W1,c1) + np.dot(W2,c2) + b1)

                # See last paragraph in Section 2.3
                p_norm1 = p/norm(p)

                # Eq. (7) in the paper (for special case of 1d label)
                #sm = sigmoid(np.dot(Wlab,p_norm1) + blab)
                #这里是计算节点的情感标签,sm
                sm = softmax(np.dot(Wlab,p_norm1) + blab)
                beta=0.5
                #lbl_sm = beta * (1.0-self.alpha)*(label - sm)
                lbl_sm = beta * (1.0-self.alpha)*(temp_label - sm)
                #lbl_sm = beta * (1.0-self.alpha) * (temp_label-sm)
                #sentree.catDelta[:, i] = -softmax_prime(sm)[:,label-1]
                #J=-(1.0-self.alpha)*np.log(sm[label-1])
                #sentree.catDelta[:, i] = -np.dot(lbl_sm,sigmoid_prime(sm))
                sentree.catDelta[:, i] = -np.dot(lbl_sm,softmax_prime(sm))
                #J = 1.0/2.0*(np.dot(lbl_sm,(label - sm)))
                J = 1.0/2.0*(np.dot(lbl_sm,(temp_label - sm)))

                sentree.nodeFeatures[:,i] = p_norm1
                sentree.nodeFeatures_unnormalized[:,i] = p
                sentree.nodeScores[i] = J
                sentree.numkids = nodeUnder

            sentree.kids = allKids
        else:
            # 这里主要是计算重构误差
            # Reconstruction Error
            for j in range(sl-1):
                size2=np.size(words_embedded,1)

                """
                 经过测试,p有多个值
                 也就不难怪这里c1,c2里面分别存了多个单词的向量
                 因此,这个算法并不是一个个依次算p的,而是一次性一起算出来p
                 也因此J的值应该也是有多个值。代表两两单词计算的不同结果。
                """
                c1 = words_embedded[:,0:-1] # 去掉最后一个单词
                c2 = words_embedded[:,1:]  # 去掉第一个单词

                freq1 = freq[0:-1]
                freq2 = freq[1:]

                p = tanh(np.dot(W1,c1) + np.dot(W2,c2) + np.reshape(b1,[self.d,1])*([1]*(size2-1)))
                p_norm1 =p/np.sqrt(sum(p**2))

                y1_unnormalized = tanh(np.dot(W3,p_norm1) + np.reshape(b2,[self.d,1])*([1]*(size2-1)))
                y2_unnormalized = tanh(np.dot(W4,p_norm1) + np.reshape(b3,[self.d,1])*([1]*(size2-1)))

                y1 = y1_unnormalized/np.sqrt(sum(y1_unnormalized**2))
                y2 = y2_unnormalized/np.sqrt(sum(y2_unnormalized**2))

                y1c1 = self.alpha*(y1-c1)
                y2c2 = self.alpha*(y2-c2)

                # Eq. (4) in the paper: reconstruction error
                J = 1.0/2.0*sum((y1c1)*(y1-c1) + (y2c2)*(y2-c2))

                # finding the pair with smallest reconstruction error for constructing sentree
                J_min= min(J)
                J_minpos=np.argmin(J)

                """
                只有非叶子节点才会有重构节点,因此,sentree.node_y1c1需要从sl+j开始存y1c1.
                """
                sentree.node_y1c1[:,sl+j] = y1c1[:,J_minpos]
                sentree.node_y2c2[:,sl+j] = y2c2[:,J_minpos]
                sentree.nodeDelta_out1[:,sl+j] = np.dot(norm1tanh_prime(y1_unnormalized[:,J_minpos]) , y1c1[:,J_minpos])
                sentree.nodeDelta_out2[:,sl+j] = np.dot(norm1tanh_prime(y2_unnormalized[:,J_minpos]) , y2c2[:,J_minpos])

                #一对节点被选中以后,需要删除words_embedded对应的向量
                #还要把合成的节点加入words_embedded
                words_embedded=np.delete(words_embedded,J_minpos+1,1)
                words_embedded[:,J_minpos]=p_norm1[:,J_minpos]
                sentree.nodeFeatures[:, sl+j] = p_norm1[:,J_minpos]
                sentree.nodeFeatures_unnormalized[:, sl+j]= p[:,J_minpos]
                sentree.nodeScores[sl+j] = J_min
                # pp存的可能是父节点信息,因为两个孩子拥有同一个父亲
                sentree.pp[collapsed_sentence[J_minpos]] = sl+j
                sentree.pp[collapsed_sentence[J_minpos+1]] = sl+j
                sentree.kids[sl+j,:] = [collapsed_sentence[J_minpos], collapsed_sentence[J_minpos+1]]
                sentree.numkids[sl+j] = sentree.numkids[sentree.kids[sl+j,0]] + sentree.numkids[sentree.kids[sl+j,1]]


                freq=np.delete(freq,J_minpos+1)
                freq[J_minpos] = (sentree.numkids[sentree.kids[sl+j,0]]*freq1[J_minpos] + sentree.numkids[sentree.kids[sl+j,1]]*freq2[J_minpos])/(sentree.numkids[sentree.kids[sl+j,0]]+sentree.numkids[sentree.kids[sl+j,1]])

                collapsed_sentence=np.delete(collapsed_sentence,J_minpos+1)
                collapsed_sentence[J_minpos]=sl+j
            print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
            print(sentree.pp)
            print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
            print(sentree.kids)
        return sentree
Пример #53
0
    def supAnalyser(self,X,freq,vocabulary,top=20):
        result_score=[]
        result_word=[]
        for i in range(self.cat):
            result_score.append([0.0]*top)
            result_word.append(['']*top)

        num_sent=np.size(X,0)
        allKids=[[]]*num_sent

        for i in range(num_sent):
            x=X[i]
            sl=len(x)
            words_embedded=self.WL[:,x]
            unsup_tree = self.forwardProp([],words_embedded,False,None,self.theta,freq)
            allKids[i]=unsup_tree.kids

            sup_tree=rnntree.rnntree(self.d,sl,words_embedded)

            nodeUnder = np.ones([2*sl-1,1])

            for j in range(sl,2*sl-1): # calculate n1, n2 and n1+n2 for each node in the sensentree and store in nodeUnder
                kids = allKids[i][j]
                n1 = nodeUnder[kids[0]]
                n2 = nodeUnder[kids[1]]
                nodeUnder[j] = n1+n2

            #sentree.catDelta = np.zeros([cat_size, 2*sl-1])
            #sentree.catDelta_out = np.zeros([self.d,2*sl-1])

            for j in range(2*sl-1):
                kids = allKids[i][j]

                c1 = sup_tree.nodeFeatures[:,kids[0]]
                c2 = sup_tree.nodeFeatures[:,kids[1]]

                # Eq. [2] in the paper: p = f(W[1][c1 c2] + b[1])
                p = tanh(np.dot(self.W1,c1) + np.dot(self.W2,c2) + self.b1)

                # See last paragraph in Section 2.3
                p_norm1 = p/norm(p)

                # Eq. (7) in the paper (for special case of 1d label)
                #sm = sigmoid(np.dot(Wlab,p_norm1) + blab)
                sm=softmax(np.dot(self.Wlab,p_norm1) + self.blab)
                #max_score=max(sm)
                for ind in range(self.cat):
                    max_score=sm[ind]
                    #ind=list(sm).index(max_score)
                    min_score=min(result_score[ind])
                    if max_score>min_score:
                        min_ind=result_score[ind].index(min_score)
                        result_score[ind][min_ind]=max_score
                        if j<sl:
                            result_word[ind][min_ind]=vocabulary[x[j]]
                        else:
                            stk=[]
                            stk.extend(list(kids))
                            stk.reverse()
                            words=[]
                            while len(stk)!=0:
                                current=stk.pop()
                                if current<sl:
                                    words.append(vocabulary[x[current]])
                                else:
                                    toExtend=[]
                                    toExtend.extend(list(allKids[i][current]))
                                    toExtend.reverse()
                                    stk.extend(toExtend)

                            result_word[ind][min_ind]=' '.join(words)
        return (result_score,result_word)
    def supAnalyser(self, X, freq, vocabulary, top=20):
        result_score = []
        result_word = []
        for i in range(self.cat):
            result_score.append([0.0] * top)
            result_word.append([''] * top)

        num_sent = np.size(X, 0)
        allKids = [[]] * num_sent

        for i in range(num_sent):
            x = X[i]
            sl = len(x)
            words_embedded = self.WL[:, x]
            unsup_tree = self.forwardProp([], words_embedded, False, None,
                                          self.theta, freq)
            allKids[i] = unsup_tree.kids

            sup_tree = rnntree.rnntree(self.d, sl, words_embedded)

            nodeUnder = np.ones([2 * sl - 1, 1])

            for j in range(
                    sl, 2 * sl - 1
            ):  # calculate n1, n2 and n1+n2 for each node in the sensentree and store in nodeUnder
                kids = allKids[i][j]
                n1 = nodeUnder[kids[0]]
                n2 = nodeUnder[kids[1]]
                nodeUnder[j] = n1 + n2

            #sentree.catDelta = np.zeros([cat_size, 2*sl-1])
            #sentree.catDelta_out = np.zeros([self.d,2*sl-1])

            for j in range(2 * sl - 1):
                kids = allKids[i][j]

                c1 = sup_tree.nodeFeatures[:, kids[0]]
                c2 = sup_tree.nodeFeatures[:, kids[1]]

                # Eq. [2] in the paper: p = f(W[1][c1 c2] + b[1])
                p = tanh(np.dot(self.W1, c1) + np.dot(self.W2, c2) + self.b1)

                # See last paragraph in Section 2.3
                p_norm1 = p / norm(p)

                # Eq. (7) in the paper (for special case of 1d label)
                #sm = sigmoid(np.dot(Wlab,p_norm1) + blab)
                sm = softmax(np.dot(self.Wlab, p_norm1) + self.blab)
                max_score = max(sm)
                ind = list(sm).index(max_score)
                min_score = min(result_score[ind])
                if max_score > min_score:
                    min_ind = result_score[ind].index(min_score)
                    result_score[ind][min_ind] = max_score
                    if j < sl:
                        result_word[ind][min_ind] = vocabulary[x[j]]
                    else:
                        stk = []
                        stk.extend(list(kids))
                        stk.reverse()
                        words = []
                        while len(stk) != 0:
                            current = stk.pop()
                            if current < sl:
                                words.append(vocabulary[x[current]])
                            else:
                                toExtend = []
                                toExtend.extend(list(allKids[i][current]))
                                toExtend.reverse()
                                stk.extend(toExtend)

                        result_word[ind][min_ind] = ' '.join(words)
        return (result_score, result_word)
Пример #55
0
def ablanzbd(A, V, W, F, B, K, 
             interchange, m_b, n, m,  
             SVTol, two_reorth=False, iteration = 0):
    """
    a fairly close port of the lanczos bidiagonalization step 
    """
    def mat_prod(x):
        if interchange:
            return A.compute_ATx(x)
        else:
            return A.compute_Ax(x)

    def adjoint_prod(x):
        if interchange:
            return A.compute_Ax(x)
        else:
            return A.compute_ATx(x)
        
    J = 0

    # noramlization of starting vector
    if iteration == 0:
        V[:, 0] = V[:, 0] / norm(V[:, 0])
        B = []
    else:
        J = K

    W[:, J] =  mat_prod(V[:, J])
    
    # input vectors are singular vectors and AV[:, J] which must be 
    # orthogonalized
    
    if iteration > 0:
        W[:, J] = util.orthog(W[:, J], W[:, :J]) # FIXME possible idnex error

    S = norm(W[:, J])
    
    # check for linearly-dependent vectors
    if S < SVTol:
        W[:, J] = np.random.normal(0, 1, W.shape[1])
        W[:, J] = util.orthog(W[:, J], W[:, :J])
        W[:, J] = W[:, J]/ norm(W[:, J])
        
    else:
        W[:, J] = W[:, J]/ S

    # begin main iteration loop for block lanczos
    while J  < m_b:

        F =  adjoint_prod(W[:, J])

        # One step of the block classical Gram-Schmidt process
        F = F - V[:, J] * S
        
        # full reorthogonalization, short vectors
        F = util.orthog(F, V[:, :J])

        
        if (J+1) < m_b:
            R = norm(F)

            if R <= SVTol:
                F = np.random.normal(0, 1, (V.shape[0], 1))
                F = util.orthog(F, V[:, :J])

                V[:, J+1] = F.flatten() / norm(F)
                R = 0
            else:
                V[:, J+1] = F / R

            # compute block diagonal B
            if len(B) == 0:
                B = np.array([[S, R]])
            else:
                Bnew = np.zeros((B.shape[0] + 1, 
                                 B.shape[1] + 1))
                Bnew[:B.shape[0], 
                  :B.shape[1]] = B
                Bnew[-1, -2] = S
                Bnew[-1, -1] = R
                B = Bnew

            # W = A*V
            W[:, J+1] = mat_prod(V[:, J+1])
    
            # one step of block classical Gram-Schmidt process:
            W[:, J+1] = W[:, J+1] - np.dot( W[:, J] , R)
            
            if two_reorth:
                W[:, J+1] = util.orthog(W[:, J+1], W[:, :J]) # FIXME I THINK THESE INCDES ARE OFF
                
            # compute norm of W
            S = norm(W[:, J+1])
            
            if S <= SVTol:
                W[:, J+1] = np.random.normal(0, 1, (W.shape[0], 1))
                W[:, J+1] = util.orthog(W[:, J+1], W[:, :J])
                W[:, J+1] = W[:, J+1] / norm(W[:, J+1])
                S = 0
            else:
                W[:, J+1] = W[:, J+1]/S
                
                
        else:
            # Add last block to matrix B
            Bnew = np.zeros((B.shape[0]+1,  B.shape[1]))
            Bnew[:-1, :] = B

            Bnew[-1, -1:] = S
            B = Bnew

        J += 1

    return V, W, F, B, m
Пример #56
0
 def pr(name, low, high, val):
     return '%s=%f [normalized %f]' % (name, val, util.norm(low, high, val))
    def forwardProp(self, allKids, words_embedded, updateWlab, label, theta,
                    freq):
        (W1, W2, W3, W4, Wlab, b1, b2, b3, blab, WL) = self.getParams(theta)
        sl = np.size(words_embedded, 1)
        sentree = rnntree.rnntree(self.d, sl, words_embedded)
        collapsed_sentence = range(sl)
        if updateWlab:
            temp_label = np.zeros(self.cat)
            temp_label[label - 1] = 1.0
            nodeUnder = np.ones([2 * sl - 1, 1])

            for i in range(
                    sl, 2 * sl - 1
            ):  # calculate n1, n2 and n1+n2 for each node in the sensentree and store in nodeUnder
                kids = allKids[i]
                n1 = nodeUnder[kids[0]]
                n2 = nodeUnder[kids[1]]
                nodeUnder[i] = n1 + n2

            cat_size = self.cat
            sentree.catDelta = np.zeros([cat_size, 2 * sl - 1])
            sentree.catDelta_out = np.zeros([self.d, 2 * sl - 1])

            # classifier on single words
            for i in range(sl):
                sm = softmax(np.dot(Wlab, words_embedded[:, i]) + blab)
                lbl_sm = (1 - self.alpha) * (temp_label - sm)
                sentree.nodeScores[i] = 1.0 / 2.0 * (np.dot(
                    lbl_sm, (temp_label - sm)))
                sentree.catDelta[:, i] = -np.dot(lbl_sm, softmax_prime(sm))

            # sm = sigmoid(self.Wlab*words_embedded + self.blab)

            #lbl_sm = (1-self.alpha)*(label[:,np.ones(sl,1)] - sm)
            #sentree.nodeScores[:sl] = 1/2*(lbl_sm.*(label(:,ones(sl,1)) - sm))
            #sentree.catDelta[:, :sl] = -(lbl_sm).*sigmoid_prime(sm)

            for i in range(sl, 2 * sl - 1):
                kids = allKids[i]

                c1 = sentree.nodeFeatures[:, kids[0]]
                c2 = sentree.nodeFeatures[:, kids[1]]

                # Eq. [2] in the paper: p = f(W[1][c1 c2] + b[1])
                p = tanh(np.dot(W1, c1) + np.dot(W2, c2) + b1)

                # See last paragraph in Section 2.3
                p_norm1 = p / norm(p)

                # Eq. (7) in the paper (for special case of 1d label)
                #sm = sigmoid(np.dot(Wlab,p_norm1) + blab)
                sm = softmax(np.dot(Wlab, p_norm1) + blab)
                beta = 0.5
                #lbl_sm = beta * (1.0-self.alpha)*(label - sm)
                lbl_sm = beta * (1.0 - self.alpha) * (temp_label - sm)
                #lbl_sm = beta * (1.0-self.alpha) * (temp_label-sm)
                #sentree.catDelta[:, i] = -softmax_prime(sm)[:,label-1]
                #J=-(1.0-self.alpha)*np.log(sm[label-1])
                #sentree.catDelta[:, i] = -np.dot(lbl_sm,sigmoid_prime(sm))
                sentree.catDelta[:, i] = -np.dot(lbl_sm, softmax_prime(sm))
                #J = 1.0/2.0*(np.dot(lbl_sm,(label - sm)))
                J = 1.0 / 2.0 * (np.dot(lbl_sm, (temp_label - sm)))

                sentree.nodeFeatures[:, i] = p_norm1
                sentree.nodeFeatures_unnormalized[:, i] = p
                sentree.nodeScores[i] = J
                sentree.numkids = nodeUnder

            sentree.kids = allKids
        else:
            # Reconstruction Error
            for j in range(sl - 1):
                size2 = np.size(words_embedded, 1)
                c1 = words_embedded[:, 0:-1]
                c2 = words_embedded[:, 1:]

                freq1 = freq[0:-1]
                freq2 = freq[1:]

                p = tanh(
                    np.dot(W1, c1) + np.dot(W2, c2) +
                    np.reshape(b1, [self.d, 1]) * ([1] * (size2 - 1)))
                p_norm1 = p / np.sqrt(sum(p**2))

                y1_unnormalized = tanh(
                    np.dot(W3, p_norm1) + np.reshape(b2, [self.d, 1]) *
                    ([1] * (size2 - 1)))
                y2_unnormalized = tanh(
                    np.dot(W4, p_norm1) + np.reshape(b3, [self.d, 1]) *
                    ([1] * (size2 - 1)))

                y1 = y1_unnormalized / np.sqrt(sum(y1_unnormalized**2))
                y2 = y2_unnormalized / np.sqrt(sum(y2_unnormalized**2))

                y1c1 = self.alpha * (y1 - c1)
                y2c2 = self.alpha * (y2 - c2)

                # Eq. (4) in the paper: reconstruction error
                J = 1.0 / 2.0 * sum((y1c1) * (y1 - c1) + (y2c2) * (y2 - c2))

                # finding the pair with smallest reconstruction error for constructing sentree
                J_min = min(J)
                J_minpos = np.argmin(J)

                sentree.node_y1c1[:, sl + j] = y1c1[:, J_minpos]
                sentree.node_y2c2[:, sl + j] = y2c2[:, J_minpos]
                sentree.nodeDelta_out1[:, sl + j] = np.dot(
                    norm1tanh_prime(y1_unnormalized[:, J_minpos]),
                    y1c1[:, J_minpos])
                sentree.nodeDelta_out2[:, sl + j] = np.dot(
                    norm1tanh_prime(y2_unnormalized[:, J_minpos]),
                    y2c2[:, J_minpos])

                words_embedded = np.delete(words_embedded, J_minpos + 1, 1)
                words_embedded[:, J_minpos] = p_norm1[:, J_minpos]
                sentree.nodeFeatures[:, sl + j] = p_norm1[:, J_minpos]
                sentree.nodeFeatures_unnormalized[:, sl + j] = p[:, J_minpos]
                sentree.nodeScores[sl + j] = J_min
                sentree.pp[collapsed_sentence[J_minpos]] = sl + j
                sentree.pp[collapsed_sentence[J_minpos + 1]] = sl + j
                sentree.kids[sl + j, :] = [
                    collapsed_sentence[J_minpos],
                    collapsed_sentence[J_minpos + 1]
                ]
                sentree.numkids[sl + j] = sentree.numkids[sentree.kids[
                    sl + j, 0]] + sentree.numkids[sentree.kids[sl + j, 1]]

                freq = np.delete(freq, J_minpos + 1)
                freq[J_minpos] = (
                    sentree.numkids[sentree.kids[sl + j, 0]] * freq1[J_minpos]
                    +
                    sentree.numkids[sentree.kids[sl + j, 1]] * freq2[J_minpos]
                ) / (sentree.numkids[sentree.kids[sl + j, 0]] +
                     sentree.numkids[sentree.kids[sl + j, 1]])

                collapsed_sentence = np.delete(collapsed_sentence,
                                               J_minpos + 1)
                collapsed_sentence[J_minpos] = sl + j
        return sentree
Пример #58
0
def lookup(idiomaLattes):
    return lattesToISO639.get(util.norm(idiomaLattes))