python类norm()的实例源码

attract-repel.py 文件源码 项目:attract-repel 作者: nmrksic 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def normalise_word_vectors(word_vectors, norm=1.0):
    """
    This method normalises the collection of word vectors provided in the word_vectors dictionary.
    """
    for word in word_vectors:
        word_vectors[word] /= math.sqrt((word_vectors[word]**2).sum() + 1e-6)
        word_vectors[word] = word_vectors[word] * norm
    return word_vectors
attract-repel.py 文件源码 项目:attract-repel 作者: nmrksic 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def normalise_vector(v1):
    return v1 / norm(v1)
attract-repel.py 文件源码 项目:attract-repel 作者: nmrksic 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def distance(v1, v2, normalised_vectors=False):
    """
    Returns the cosine distance between two vectors. 
    If the vectors are normalised, there is no need for the denominator, which is always one. 
    """
    if normalised_vectors:
        return 1 - dot(v1, v2)
    else:
        return 1 - dot(v1, v2) / ( norm(v1) * norm(v2) )
BLISS.py 文件源码 项目:Lattice-Based-Signatures 作者: krishnacharya 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def Verify(**kwargs):
    msg, A, m, n, sd, q, eta, z, c, kappa = kwargs['msg'], kwargs['A'], kwargs['m'], kwargs['n'], kwargs['sd'], kwargs['q'], kwargs['eta'], kwargs['z'], kwargs['c'], kwargs['kappa']
    B2 = eta*sd*np.sqrt(m)
    reduced_prod = util.vector_to_Zq(np.matmul(A,z) + q*c, 2*q)
    #print np.sqrt(z.dot(z)),B2
    #print LA.norm(z,np.inf),float(q)/4
    if np.sqrt(z.dot(z)) > B2  or LA.norm(z,np.inf) >= float(q)/4:      
        return False    
    if np.array_equal(c, hash_iterative(np.array_str(reduced_prod)+msg, n, kappa)):
        return True
    return False
snake.py 文件源码 项目:snake 作者: rhinech 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def normal_direction(p, p1, p2):
        """Compute normal direction at p in the segment p1->p->p2."""

        e1 = (p1 - p) / norm(p1 - p)
        e2 = Snake.rotate(e1, np.pi / 2.)
        x = np.dot(p2 - p, e1)
        y = np.dot(p2 - p, e2)
        theta = np.arctan2(y, x)
        return Snake.rotate(e1, theta / 2.)
spatial_image_analysis.py 文件源码 项目:tissue_analysis 作者: VirtualPlants 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def vector_correlation(vect1,vect2):
    """
    Compute correlation between two vector, which is the the cosine of the angle between two vectors in Euclidean space of any number of dimensions.
    The dot product is directly related to the cosine of the angle between two vectors if they are normed !!!
    """
    # -- We make sure that we have normed vectors.
    from numpy.linalg import norm
    if (np.round(norm(vect1)) != 1.):
        vect1 = vect1/norm(vect1)
    if (np.round(norm(vect2)) != 1.):
        vect2 = vect2/norm(vect2)

    return np.round(np.dot(vect1,vect2),3)
qcqp.py 文件源码 项目:qcqp 作者: cvxgrp 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def admm_phase2(x0, prob, rho, tol=1e-2, num_iters=1000, viol_lim=1e4):
    logging.info("Starting ADMM phase 2 with rho %.3f", rho)

    bestx = np.copy(x0)

    z = np.copy(x0)
    xs = [np.copy(x0) for i in range(prob.m)]
    us = [np.zeros(prob.n) for i in range(prob.m)]

    if prob.rho != rho:
        prob.rho = rho
        zlhs = 2*(prob.f0.P + rho*prob.m*sp.identity(prob.n)).tocsc()
        prob.z_solver = SLA.factorized(zlhs)

    last_z = None
    for t in range(num_iters):
        rhs = 2*rho*(sum(xs)-sum(us)) - prob.f0.qarray
        z = prob.z_solver(rhs)

        # TODO: parallel x/u-updates
        for i in range(prob.m):
            xs[i] = onecons_qcqp(z + us[i], prob.fi(i))
        for i in range(prob.m):
            us[i] += z - xs[i]

        # TODO: termination condition
        if last_z is not None and LA.norm(last_z - z) < tol:
            break
        last_z = z

        maxviol = max(prob.violations(z))
        logging.info("Iteration %d, violation %.3f", t, maxviol)

        if maxviol > viol_lim: break
        bestx = np.copy(prob.better(z, bestx))

    return bestx
position_hand.py 文件源码 项目:SLP-Annotator 作者: PhonologicalCorpusTools 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def vector_norm(data, axis=None, out=None):
    """Return length, i.e. Euclidean norm, of ndarray along axis.

    >>> v = numpy.random.random(3)
    >>> n = vector_norm(v)
    >>> numpy.allclose(n, numpy.linalg.norm(v))
    True
    >>> v = numpy.random.rand(6, 5, 3)
    >>> n = vector_norm(v, axis=-1)
    >>> numpy.allclose(n, numpy.sqrt(numpy.sum(v*v, axis=2)))
    True
    >>> n = vector_norm(v, axis=1)
    >>> numpy.allclose(n, numpy.sqrt(numpy.sum(v*v, axis=1)))
    True
    >>> v = numpy.random.rand(5, 4, 3)
    >>> n = numpy.empty((5, 3))
    >>> vector_norm(v, axis=1, out=n)
    >>> numpy.allclose(n, numpy.sqrt(numpy.sum(v*v, axis=1)))
    True
    >>> vector_norm([])
    0.0
    >>> vector_norm([1])
    1.0

    """
    data = numpy.array(data, dtype=numpy.float64, copy=True)
    if out is None:
        if data.ndim == 1:
            return math.sqrt(numpy.dot(data, data))
        data *= data
        out = numpy.atleast_1d(numpy.sum(data, axis=axis))
        numpy.sqrt(out, out)
        return out
    else:
        data *= data
        numpy.sum(data, axis=axis, out=out)
        numpy.sqrt(out, out)
Distance.py 文件源码 项目:time_series_anomaly_detection 作者: massful 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def cos_distance(cls, x, y ):
        x, y = np.mat(x), np.mat(y)
        #num = float(x.dot(y.T))
        num = float(x * y.T)
        denom = la.norm(x) * la.norm(y)
        #dist = 0.5 + 0.5 * (num / denom)
        dist = 1.0 if denom == 0 else num / denom 
        return 3 * (1 - dist)
save_problem.py 文件源码 项目:onsager_deep_learning 作者: mborgerding 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def save_problem(base,prob):
    print('saving {b}.mat,{b}.npz norm(x)={x:.7f} norm(y)={y:.7f}'.format(b=base,x=la.norm(prob.xval), y=la.norm(prob.yval) ) )
    D=dict(A=prob.A,x=prob.xval,y=prob.yval)
    np.savez( base + '.npz', **D)
    savemat(base + '.mat',D,oned_as='column')
problems.py 文件源码 项目:onsager_deep_learning 作者: mborgerding 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def bernoulli_gaussian_trial(M=250,N=500,L=1000,pnz=.1,kappa=None,SNR=40):

    A = np.random.normal(size=(M, N), scale=1.0 / math.sqrt(M)).astype(np.float32)
    if kappa >= 1:
        # create a random operator with a specific condition number
        U,_,V = la.svd(A,full_matrices=False)
        s = np.logspace( 0, np.log10( 1/kappa),M)
        A = np.dot( U*(s*np.sqrt(N)/la.norm(s)),V).astype(np.float32)
    A_ = tf.constant(A,name='A')
    prob = TFGenerator(A=A,A_=A_,pnz=pnz,kappa=kappa,SNR=SNR)
    prob.name = 'Bernoulli-Gaussian, random A'

    bernoulli_ = tf.to_float( tf.random_uniform( (N,L) ) < pnz)
    xgen_ = bernoulli_ * tf.random_normal( (N,L) )
    noise_var = pnz*N/M * math.pow(10., -SNR / 10.)
    ygen_ = tf.matmul( A_,xgen_) + tf.random_normal( (M,L),stddev=math.sqrt( noise_var ) )

    prob.xval = ((np.random.uniform( 0,1,(N,L))<pnz) * np.random.normal(0,1,(N,L))).astype(np.float32)
    prob.yval = np.matmul(A,prob.xval) + np.random.normal(0,math.sqrt( noise_var ),(M,L))
    prob.xinit = ((np.random.uniform( 0,1,(N,L))<pnz) * np.random.normal(0,1,(N,L))).astype(np.float32)
    prob.yinit = np.matmul(A,prob.xinit) + np.random.normal(0,math.sqrt( noise_var ),(M,L))
    prob.xgen_ = xgen_
    prob.ygen_ = ygen_
    prob.noise_var = noise_var

    return prob
raputil.py 文件源码 项目:onsager_deep_learning 作者: mborgerding 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def add_noise(self,Y0):
        'add noise at the given SNR, returns Y0+W,wvar'
        wvar = (la.norm(Y0)**2/Y0.size) * 10**(-self.SNR_dB/10)
        if self.cpx:
            Y =(Y0 + crandn(Y0.shape) * sqrt(wvar/2)).astype(np.complex64,copy=False)
        else:
            Y = (Y0 + np.random.normal(scale=sqrt(wvar),size=Y0.shape) ).astype(np.float32,copy=False)
        return Y,wvar
raputil.py 文件源码 项目:onsager_deep_learning 作者: mborgerding 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def _test_awgn(self,cpx):
            snr = np.random.uniform(3,20)
            p = Problem(cpx=cpx,SNR_dB=snr)
            X = p.genX(5)
            self.assertEqual( np.iscomplexobj(X) , cpx )
            Y0 = p.fwd(X)
            self.assertEqual( np.iscomplexobj(Y0) , cpx )
            Y,wvar = p.add_noise(Y0)
            self.assertEqual( np.iscomplexobj(Y) , cpx )
            snr_obs = -20*np.log10( la.norm(Y-Y0)/la.norm(Y0))
            self.assertTrue( abs(snr-snr_obs) < 1.0, 'gross error in add_noise')
            wvar_obs = la.norm(Y0-Y)**2/Y.size
            self.assertTrue( .5 < wvar_obs/wvar < 1.5, 'gross error in add_noise wvar')
ik.py 文件源码 项目:promplib 作者: baxter-flowers 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def cost_position(x_des, fk):
        return norm(fk[:3] - x_des[:3]) ** 2
interactive.py 文件源码 项目:promplib 作者: baxter-flowers 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def distance_from_goal(self, trajectory, goal):
        reached_goal = self.fk.get(trajectory[-1])
        return norm(reached_goal[0] - goal[0])
interactive.py 文件源码 项目:promplib 作者: baxter-flowers 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def set_goal(self, x_des, joint_des=None, refining=True):
        """
        Set a new task-space goal, and determine which primitive will be used
        Flushes the previous goal log and update id with this goal in the same time
        :param x_des: desired task-space goal
        :param joint_des desired joint-space goal **ONLY used for plots**
        :param refining: True to refine the trajectory by optimization after conditioning
        :return: True if the goal has been taken into account, False if a new demo is needed to reach it
        """
        def distance_from_goal(trajectory, goal):
            reached_goal = self.fk.get(trajectory[-1])
            return norm(reached_goal[0] - goal[0])

        self.promp_write_index = -1
        self.goal_id += 1
        self.goal_log = []
        if self.num_primitives > 0:
            for promp_index, promp in enumerate(self.promps):
                if self._is_a_target(promp, x_des):
                    self.generated_trajectory = promp.generate_trajectory(x_des, refining, joint_des, 'set_goal_{}'.format(self.goal_id))
                    distance = distance_from_goal(self.generated_trajectory, x_des)
                    if distance < self.epsilon_ok:
                        self.goal = x_des
                        self.promp_read_index = promp_index
                        self.goal_log.append({"mp_id": promp_index, "is_a_target": True, "is_reached": True, "precision": distance})
                        return True
                    else:
                        self.promp_write_index = promp_index
                        self.promp_read_index = -1  # A new demo is requested
                        self.goal_log.append({"mp_id": promp_index, "is_a_target": True, "is_reached": False, "precision": distance})
                        return False
                else:
                    _ = promp.generate_trajectory(x_des, refining, joint_des, 'set_goal_{}_not_a_target'.format(self.goal_id))  # Only for plotting
                    self.promp_read_index = -1  # A new promp is requested
                    self.goal_log.append({"mp_id": promp_index, "is_a_target": False, "is_reached": False})
            return False
test_baseline.py 文件源码 项目:nnmnkwii 作者: r9y9 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_diffvc():
    # MLPG is performed dimention by dimention, so static_dim 1 is enough, 2 just for in
    # case.
    static_dim = 2
    T = 10

    for windows in _get_windows_set():
        np.random.seed(1234)
        src_mc = np.random.rand(T, static_dim * len(windows))
        tgt_mc = np.random.rand(T, static_dim * len(windows))

        # pseudo parallel data
        XY = np.concatenate((src_mc, tgt_mc), axis=-1)
        gmm = GaussianMixture(n_components=4)
        gmm.fit(XY)

        paramgen = MLPG(gmm, windows=windows, diff=False)
        diff_paramgen = MLPG(gmm, windows=windows, diff=True)

        mc_converted1 = paramgen.transform(src_mc)
        mc_converted2 = diff_paramgen.transform(src_mc)

        assert mc_converted1.shape == (T, static_dim)
        assert mc_converted2.shape == (T, static_dim)

        src_mc = src_mc[:, :static_dim]
        tgt_mc = tgt_mc[:, :static_dim]
        assert norm(tgt_mc - mc_converted1) < norm(src_mc - mc_converted1)
alignment.py 文件源码 项目:nnmnkwii 作者: r9y9 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def __init__(self, dist=lambda x, y: norm(x - y), radius=1, verbose=0):
        self.verbose = verbose
        self.dist = dist
        self.radius = radius
alignment.py 文件源码 项目:nnmnkwii 作者: r9y9 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def __init__(self, n_iter=3, dist=lambda x, y: norm(x - y),
                 radius=1, max_iter_gmm=100, n_components_gmm=16, verbose=0):
        self.n_iter = n_iter
        self.dist = dist
        self.radius = radius
        self.max_iter_gmm = max_iter_gmm
        self.n_components_gmm = n_components_gmm
        self.verbose = verbose
similarity_test.py 文件源码 项目:kor2vec 作者: dongjun-Lee 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def normalize(array):
    norm = la.norm(array)
    return array / norm


问题


面经


文章

微信
公众号

扫码关注公众号