Exemplo n.º 1
0
def rbm_grad_exact(W, x, vis_gauss=False):

    batch_size = float(len(x))
    v, h = W.v, W.h
    G = 0 * W
    H = sigmoid(W*x)
    G += 1./batch_size * W.outp(x, H) 
    if not vis_gauss:
        Z = brute_force_Z(W)

        def prob(V):
            return exp( free_energy(W, V)[0] - Z )

        for i in xrange(2**v):
            V = int_to_bin(i, v)
            H = sigmoid(W* V)
            G -= prob(V) * W.outp(V, H) 

            loss = - ( free_energy(W, x).mean(0) - Z )

    if vis_gauss:
        Z = brute_force_Z_vis_gauss(W)

        def prob(H):
            b = W.T() * H
            z = .5*dot(b,b.T) + dot(H,W[2])
            return float(exp(z-Z) )

        for i in xrange(2**h):
            H = int_to_bin(i, h)
            b = W.T()*H
            G -= prob(H) * W.outp(b,H)
        loss = - (-.5*amap(dot,x,x).mean() + free_energy(W, x).mean(0) - Z)

    return batch_size * G, dict(loss=batch_size * loss)            
Exemplo n.º 2
0
    def processData(self):

        print self.magSampleList
        #plot(array(self.magSampleList))
        #show()

        self.mags = array(self.magSampleList)

        xs = self.mags[:,0]
        ys = self.mags[:,1]
        zs = self.mags[:,2]

        cxs, x_offset, x_scale = self.applyCalibration(xs)
        cys, y_offset, y_scale = self.applyCalibration(ys)
        czs, z_offset, z_scale = self.applyCalibration(zs)


        def residuals(params,x,y,z):
            xo = params[0]
            xs = params[1]
            yo = params[2]
            ys = params[3]
            zo = params[4]
            zs = params[5]

            xc = empty(shape(x))
            for i in range(len(x)):
                xc[i] = (x[i] - xo) * xs

            yc = empty(shape(y))
            for i in range(len(y)):
                yc[i] = (y[i] - yo) * ys

            zc = empty(shape(z))
            for i in range(len(z)):
                zc[i] = (z[i] - zo) * zs

            res = []
            for i in range(len(xc)):
                norm = l2norm(array([xc[i],yc[i],zc[i]])) - 1.0
                res.append(norm)

            return array(res)

        p0 = [x_offset, x_scale, y_offset, y_scale, z_offset, z_scale]
        ls =  leastsq(residuals, p0, args=(xs,ys,zs))

        x_offset = ls[0][0]
        x_scale = ls[0][1]
        y_offset = ls[0][2]
        y_scale = ls[0][3]
        z_offset = ls[0][4]
        z_scale = ls[0][5]

        cxs = self.applyCalibration(xs, calibration=(x_offset,x_scale))[0]
        cys = self.applyCalibration(ys, calibration=(y_offset,y_scale))[0]
        czs = self.applyCalibration(zs, calibration=(z_offset,z_scale))[0]


        calibratedMag = empty((len(cxs),3))
        calibratedMag[:,0] = cxs
        calibratedMag[:,1] = cys
        calibratedMag[:,2] = czs

        magnitudes = amap(lambda v: l2norm(v), calibratedMag)

        self.calibratedMag = calibratedMag

        mlab.points3d(cxs,cys,czs, scale_mode='none', scale_factor=0.02)
        #mlab.points3d(array([-1.0,1.0]),array([0.0,0.0]),array([0.0,0.0]), scale_mode='none', scale_factor=0.02)
        sphere = mlab.points3d(0,0,0, opacity=0.5, resolution=100, color=(1.0,0.0,0.0), scale_mode='none', scale_factor=2.0)
        sphere.actor.property.backface_culling = True
        mlab.show()
        figure()
        plot(magnitudes)
        show()

        # write calibration to a file

        f = open(self.filename,mode='w')
        f.write("id,x_offset,x_scale,y_offset,y_scale,z_offset,z_scale\n")
        f.write("%d,%f,%f,%f,%f,%f,%f\n"%(self.id,x_offset,x_scale,y_offset,y_scale,z_offset,z_scale))
        f.write("\n")

        for l in self.magSampleList:
            f.write("%d,%d,%d\n"%(l[0],l[1],l[2]))

        f.close()

        resError = []
        for i in range(len(cxs)):
            resError.append(self.calculateResidualError(cxs[i],cys[i],czs[i]))
        print len(resError)
        print mean(resError)
Exemplo n.º 3
0
#    Ft_per_Fs = -TQFe/(TQb + TQc + TQf)

    ######################
    # Moment arm for proximal
    PP1 = twoDmag_ang( rp, qp1 )
    AB1 = AB + twoDmag_ang( rb, qb1 )
    PB1 = PA + AB1
    dirB1P1 = (PP1 - PB1)/p.norm(PP1 - PB1)
    mF = p.cross( AB1, dirB1P1 )

    return (t,q,g, mF)


aa = (p.arange(83.0)+20.0) * D2R

u = p.amap(q2tg,aa)

ut = u[:,0]
uq = u[:,1]; uqd = uq*R2D
ug = u[:,2]
ufma= u[:,3]

dt0 = ut[1]-ut[0]
dt1 = ut[-1]-ut[-2]
dg0 = ug[1]-ug[0]
dg1 = ug[-1]-ug[-2]

##############
# label Skeleton Plot
p.figure(10); p.grid(True)
p.title('Skeleton of Important Points')
Exemplo n.º 4
0
    def make_V_O_M(self):
        from numpy import zeros, ones
        V = zeros((self.T, self.batch_size, self.V))
        O = zeros((self.T, self.batch_size, self.O))
        M = zeros((self.T, self.batch_size, 1))

        from pylab import rand, randn

	for b in range(self.batch_size):
            from pylab import randint

            if self.batch_size < self.char_size**self.num_chars:
                # our minibatch is too small and requires random
                # sampling
                data = randint(self.char_size, size=self.num_chars)

            elif self.batch_size == self.char_size**self.num_chars:
                # our minibatch is large enoguh to contain all possible
                # sequences, so each possible sequence will appear precisely
                # once in the batch.
                from pylab import base_repr, amap
                data = amap(int, 
                         base_repr(
                            b, 
                            base=self.char_size, 
                            padding=self.num_chars))[-self.num_chars:]
            else:
                raise TypeError("self.batch_size (%s) is bigger than %s" % 
                                (self.batch_size, self.char_size**self.num_chars))

                
            # input
            V[:, b, -1] = 1

            V[:self.num_chars, b, :] = 0
            V[:self.num_chars, b, :self.char_size] = \
                expand(data, 
                       self.char_size)


            if self.teacher_force:
                # I'll admit I haven't tested teacher_force.
                V[-self.num_chars:, b, :] = 0
                V[-self.num_chars:, b, :self.char_size] = \
                    expand(data, 
                           self.char_size)


            # a pre signal. Cool. 
            V[-self.num_chars-1, b, :] = 0
            V[-self.num_chars-1, b, -2] = 1

            
            # output: 
            O[:, b, -1] = 1
            O[-self.num_chars:, b, :] = 0
            O[-self.num_chars:, b, :self.char_size] = \
                expand(data, 
                       self.char_size)

            if self.only_predict_the_memory_bits:
                M[:, b, :] = 0
                M[-self.num_chars:, b, :] = 1
            else:
                M[:, b, :] = 1

        return V, O, M
Exemplo n.º 5
0
def softmax_slow(a):
    def softmax_1(a):
        e=exp(a-a.max())
        return e/e.sum()
    return amap(softmax_1,a)
Exemplo n.º 6
0
def import_letter(filename):
    if not re.search('\.png$', filename):
        filename += '.png'
    arr = imread(filename)
    f_rev = lambda a: float32(not aa)
    return amap(lambda y: map(f_rev, y), arr[:,:,0]).flatten()
Exemplo n.º 7
0
def plot_locatables(locatables, style='kx', **kwargs):
    get_x = lambda l: l.position.x
    get_y = lambda l: l.position.y
    pylab.plot(pylab.amap(get_x, locatables), pylab.amap(get_y, locatables),
               style, **kwargs)
Exemplo n.º 8
0
def int_to_bin(i, v):
    assert(0 <= i < 2**v)
    return (amap(int,list(binary_repr(i).zfill(v)))[newaxis,:]).astype('f')