Esempio n. 1
0
 def test_distance(self):
     first = np.array([1, 0, 1, 0, 1, 0, 1], dtype=int)
     second = np.array([1, 1, 1, 0, 0, 0, 0], dtype=int)
     t1 = TernaryString.fromArray(first)
     t2 = TernaryString.fromArray(second)
     self.assertEqual(t1.distance(t2), t2.distance(t1))
     self.assertEqual(abs(first - second).sum(), t1.distance(t2))
Esempio n. 2
0
 def test_distance(self):
    first = np.array([1,0,1,0,1,0,1], dtype=int)
    second = np.array([1,1,1,0,0,0,0], dtype=int)
    t1 = TernaryString.fromArray(first)
    t2 = TernaryString.fromArray(second)
    self.assertEqual(t1.distance(t2), t2.distance(t1))
    self.assertEqual(abs(first - second).sum(), t1.distance(t2))
Esempio n. 3
0
    def __call__(self, rbm, size=1, initial=None, useAlternate=False):
        sample = []
        uniform = BernoulliTernary(rbm)
        if self.weights is None:
            wsample = uniform.batch(10000)
            self.weights = []
            base = array([exp(-rbm.energy(x, useAlternate)) for x in wsample])
            for temp in self.temps:
                self.weights.append(((base**(1. / temp)) / 10000.).sum())

        for n in xrange(self.chains):
            # initialize the chain
            x = random.random_sample(rbm.vsize + rbm.hsize)
            idx = 1
            for l in xrange(self.burn + size / self.chains):
                print "ST: chain", n, l
                if self.binary:
                    yv = random.binomial(1, x[:rbm.vsize], rbm.vsize)
                else:
                    yv = x[:rbm.vsize]
                yh = dot(yv, rbm.w) + dot(rbm.lh, x[rbm.vsize:]) + rbm.bh
                yh = 1. / (1. + exp(-yh / self.temps[idx]))
                yh = random.binomial(1, yh, rbm.hsize)
                yv = dot(rbm.w, yh) + dot(rbm.lv, yv) + rbm.bv
                yv = 1. / (1. + exp(-yv / self.temps[idx]))
                if self.binary:
                    yv = random.binomial(1, yv, rbm.vsize)
                y = append(yv, yh, axis=0)

                ratio = exp(
                    (rbm.energy(TernaryString.fromArray(x), useAlternate) -
                     rbm.energy(TernaryString.fromArray(y), useAlternate)) /
                    self.temps[idx])
                if random.random_sample() < ratio:
                    x = y
                ex = -rbm.energy(TernaryString.fromArray(x))

                if random.random_sample() < self.pdown[idx]:
                    idx2 = idx - 1
                    p1 = 1 - self.pdown[idx2]
                    p2 = self.pdown[idx]
                else:
                    idx2 = idx + 1
                    p1 = self.pdown[idx2]
                    p2 = 1. - self.pdown[idx]
                e1 = exp(ex / self.temps[idx]) * p1 * self.weights[idx]
                e2 = exp(ex / self.temps[idx2]) * p2 * self.weights[idx2]
                if random.random_sample() < e2 / e1:
                    idx = idx2
                if l >= self.burn:
                    if self.binary:
                        sample.append(TernaryString.fromArray(x))
                    else:
                        sample.append(x)
        return sample
Esempio n. 4
0
 def __call__(self, rbm, size=1, initial = None, useAlternate=False):
    sample = []
    uniform = BernoulliTernary(rbm)
    if self.weights is None:
       wsample = uniform.batch(10000)
       self.weights = []
       base = array([exp(-rbm.energy(x, useAlternate)) for x in wsample]) 
       for temp in self.temps:
          self.weights.append(((base ** (1. / temp)) / 10000.).sum())
    
    
    for n in xrange(self.chains):
       # initialize the chain
       x = random.random_sample(rbm.vsize + rbm.hsize)
       idx = 1
       for l in xrange(self.burn + size / self.chains):
          print "ST: chain", n, l
          if self.binary:
             yv = random.binomial(1, x[:rbm.vsize], rbm.vsize)
          else:
             yv = x[:rbm.vsize]
          yh = dot(yv, rbm.w) + dot(rbm.lh, x[rbm.vsize:]) + rbm.bh
          yh = 1. / (1. + exp(-yh / self.temps[idx]))
          yh = random.binomial(1, yh, rbm.hsize)
          yv = dot(rbm.w, yh) + dot(rbm.lv, yv) +rbm.bv
          yv = 1. / (1. + exp(-yv / self.temps[idx]))
          if self.binary:
             yv = random.binomial(1, yv, rbm.vsize)
          y = append(yv, yh, axis=0)
          
          
          ratio = exp((rbm.energy(TernaryString.fromArray(x), useAlternate)-rbm.energy(TernaryString.fromArray(y),useAlternate)) / self.temps[idx]) 
          if random.random_sample() < ratio:
             x = y
          ex = -rbm.energy(TernaryString.fromArray(x))
          
          if random.random_sample() < self.pdown[idx]:
             idx2 = idx - 1
             p1 = 1 - self.pdown[idx2]
             p2 = self.pdown[idx]
          else:
             idx2 = idx + 1
             p1 = self.pdown[idx2]
             p2 = 1. - self.pdown[idx]
          e1 = exp(ex / self.temps[idx]) * p1 * self.weights[idx]
          e2 = exp(ex / self.temps[idx2]) * p2 * self.weights[idx2]
          if random.random_sample() < e2 / e1:
             idx = idx2 
          if l >= self.burn:
             if self.binary:
                sample.append(TernaryString.fromArray(x))
             else:
                sample.append(x)
    return sample
Esempio n. 5
0
 def mutate(self, x):
     """This method uses an iterative algorithm to flip bits in
   a TernaryString with a given probability. The algorithm essentially
   uses the binary representation of the bit-flipping probability in order
   to convert random byte sampling (``p=.5``) to account for any probability,
   with resolution of ``1e-16``.
   
   """
     flipped = TernaryString.bernoulli(self.p(), self.config.space.dim)
     base = x.base & ~flipped.base | ~x.base & flipped.base
     known = x.known & flipped.known
     return TernaryString(base, known, self.config.space.dim)
Esempio n. 6
0
    def __call__(self, rbm, size=1, initial=None, useAlternate=False):
        sample = []
        for n in xrange(self.chains):
            # initialize the chain
            x = random.random_sample((len(self.temps), rbm.vsize + rbm.hsize))
            idx = 1
            for l in xrange(self.burn + size / self.chains):
                print "PT: chain", n, l
                y = x.copy()
                ex = zeros(len(self.temps))
                for i, t in enumerate(self.temps):
                    if self.binary:
                        yv = random.binomial(1, x[i, :rbm.vsize], rbm.vsize)
                    else:
                        yv = x[i, :rbm.vsize]
                    yh = dot(yv, rbm.w) + dot(rbm.lh, x[i,
                                                        rbm.vsize:]) + rbm.bh
                    yh = 1. / (1. + exp(-yh / t))
                    yh = random.binomial(1, yh, rbm.hsize)
                    yv = dot(rbm.w, yh) + dot(rbm.lv, yv) + rbm.bv
                    yv = 1. / (1. + exp(-yv / t))
                    if self.binary:
                        yv = random.binomial(1, yv, rbm.vsize)
                    y[i] = append(yv, yh, axis=0)

                    ratio = exp((rbm.energy(TernaryString.fromArray(
                        x[i]), useAlternate) - rbm.energy(
                            TernaryString.fromArray(y[i]), useAlternate)) /
                                self.temps[idx])
                    if random.random_sample() < ratio:
                        x[i] = y[i]
                    ex[i] = rbm.energy(TernaryString.fromArray(x[i]))

                #shift temps
                for j in xrange(len(self.temps) - 1):
                    j1 = j
                    j2 = j + 1
                    e1 = ex[j1]
                    e2 = ex[j2]
                    ratio = exp((e1 - e2) *
                                (1. / self.temps[j1] - 1. / self.temps[j2]))
                    if random.random_sample() < ratio:
                        z = x[j1]
                        x[j1] = x[j2]
                        x[j2] = z

                if l >= self.burn:
                    if self.binary:
                        sample.append(TernaryString.fromArray(x[0]))
                    else:
                        sample.append(x[0])
        return sample
Esempio n. 7
0
 def test_arrays(self):
    first = np.array([1,0,1,0,1,0,1], dtype=int)
    second = np.array([1,1,1,0,0,0,0], dtype=int)
    t1 = TernaryString.fromArray(first)
    t2 = TernaryString.fromArray(second)
    self.assertEqual(t1.known, 127L)
    self.assertEqual(t1.base, 85L)
    self.assertEqual(t1.length, 7)
    self.assertEqual(t2.known, 127L)
    self.assertEqual(t2.base, 7L)
    self.assertEqual(t2.length, 7)
    self.assertTrue(np.abs(first - t1.toArray()).max() < 0.01)
    self.assertTrue(np.abs(second - t2.toArray()).max() < 0.01)
Esempio n. 8
0
 def test_arrays(self):
     first = np.array([1, 0, 1, 0, 1, 0, 1], dtype=int)
     second = np.array([1, 1, 1, 0, 0, 0, 0], dtype=int)
     t1 = TernaryString.fromArray(first)
     t2 = TernaryString.fromArray(second)
     self.assertEqual(t1.known, 127L)
     self.assertEqual(t1.base, 85L)
     self.assertEqual(t1.length, 7)
     self.assertEqual(t2.known, 127L)
     self.assertEqual(t2.base, 7L)
     self.assertEqual(t2.length, 7)
     self.assertTrue(np.abs(first - t1.toArray()).max() < 0.01)
     self.assertTrue(np.abs(second - t2.toArray()).max() < 0.01)
Esempio n. 9
0
   def __call__(self, rbm, size=1, initial = None, useAlternate=False):
      sample = []
      for n in xrange(self.chains):
         # initialize the chain
         x = random.random_sample((len(self.temps), rbm.vsize + rbm.hsize))
         idx = 1
         for l in xrange(self.burn + size / self.chains):
            print "PT: chain", n, l
            y = x.copy()
            ex = zeros(len(self.temps))
            for i, t in enumerate(self.temps):
               if self.binary:
                  yv = random.binomial(1, x[i,:rbm.vsize], rbm.vsize)
               else:
                  yv = x[i,:rbm.vsize]
               yh = dot(yv, rbm.w) + dot(rbm.lh, x[i,rbm.vsize:]) + rbm.bh
               yh = 1. / (1. + exp(-yh / t))
               yh = random.binomial(1, yh, rbm.hsize)
               yv = dot(rbm.w, yh) + dot(rbm.lv, yv) +rbm.bv
               yv = 1. / (1. + exp(-yv / t))
               if self.binary:
                  yv = random.binomial(1, yv, rbm.vsize)
               y[i] = append(yv, yh, axis=0)
            
            
               ratio = exp((rbm.energy(TernaryString.fromArray(x[i]), useAlternate)-rbm.energy(TernaryString.fromArray(y[i]),useAlternate)) / self.temps[idx]) 
               if random.random_sample() < ratio:
                  x[i] = y[i]
               ex[i] = rbm.energy(TernaryString.fromArray(x[i]))
            
            
            #shift temps
            for j in xrange(len(self.temps) - 1):
               j1 = j
               j2 = j+1
               e1 = ex[j1]
               e2 = ex[j2]
               ratio = exp((e1 - e2) * (1./self.temps[j1] - 1./self.temps[j2]))
               if random.random_sample() < ratio:
                  z = x[j1]
                  x[j1] = x[j2]
                  x[j2] = z

            
            if l >= self.burn:
               if self.binary:
                  sample.append(TernaryString.fromArray(x[0]))
               else:
                  sample.append(x[0])
      return sample    
Esempio n. 10
0
 def __call__(self, orgs, prob=1.0):
     if np.random.random_sample() > prob:
         return orgs[0], orgs[1]
     if isinstance(orgs[0], np.ndarray):
         idx = np.random.random_integers(0, len(orgs[0]) - 1)
         return (np.append(orgs[0][:idx], orgs[1][idx:], axis=0),
                 np.append(orgs[1][:idx], orgs[0][idx:], axis=0))
     elif isinstance(orgs[0], TernaryString):
         idx = np.random.random_integers(0, orgs[0].length - 1)
         ret1 = TernaryString(orgs[0].base, orgs[0].known, orgs[0].length)
         ret2 = TernaryString(orgs[1].base, orgs[1].known, orgs[1].length)
         ret1[idx:] = orgs[1][idx:]
         ret2[idx:] = orgs[0][idx:]
         return ret1, ret2
     else:
         raise ValueError("Received unknown type in OnePointDualCrosser.")
Esempio n. 11
0
 def random(self):
     """Get a random point in binary space. Use the constraint to 
     generate a random point in the space if possible, otherwise
     use a random byte string.
     
     """
     return TernaryString.random(self.dim)
Esempio n. 12
0
 def complete(self, vs, dummy=False):
     hs = self.meanFieldUp(len(vs), vs)
     xs = [append(vs[i], h, axis=0) for i, h in enumerate(hs)]
     return [
         TernaryString.fromArray(random.binomial(1, x, shape(x)))
         for x in xs
     ]
Esempio n. 13
0
 def random(self):
     """Get a random point in binary space. Use the constraint to 
     generate a random point in the space if possible, otherwise
     use a random byte string.
     
     """
     return TernaryString.random(self.dim)
Esempio n. 14
0
 def complete2(self, data):
     completed = []
     for v in data:
         x = zeros(self.vsize + self.hsize)
         x[: self.vsize] = v
         h = dot(v, self.wg) + self.bhg
         x[self.vsize :] = 1.0 / (1.0 + exp(-h))
         completed.append(TernaryString.fromArray(x))
     return completed
Esempio n. 15
0
 def complete2(self, data):
     completed = []
     for v in data:
         x = zeros(self.vsize + self.hsize)
         x[:self.vsize] = v
         h = dot(v, self.wg) + self.bhg
         x[self.vsize:] = 1. / (1. + exp(-h))
         completed.append(TernaryString.fromArray(x))
     return completed
Esempio n. 16
0
 def __call__(self,
              rbm,
              size=1,
              initial=None,
              useAlternate=False,
              clampIdx=None):
     from pyec.util.TernaryString import TernaryString
     sample = []
     vsize = rbm.vsize
     hsize = rbm.hsize
     if useAlternate:
         w = rbm.wg
         bv = rbm.bvg
         bh = rbm.bhg
     else:
         w = rbm.w
         bv = rbm.bv
         bh = rbm.bh
     if clampIdx is None:
         clampIdx = 0
     if initial is not None:
         initialArr = initial.toArray(rbm.dim)
     for i in xrange(self.chains):
         if initial is not None:
             x = initialArr
         else:
             T0 = 1. / 4
             x = random.random_sample(vsize + hsize).round()
             h = dot(x[:vsize], w) + bh
             h = 1. / (1. + exp(-h * T0))
             if self.meanField:
                 x[vsize:] = h
             else:
                 x[vsize:] = random.binomial(1, h, hsize)
         for j in xrange(self.burn + size / self.chains):
             print "Gibbs: chain ", i, j
             T = maximum((3 * self.burn + j) / float(4 * self.burn), 1.0)
             v = dot(w, x[vsize:]) + bv
             if self.meanField:
                 x[clampIdx:vsize] = (1. / (1. + exp(-v * T)))[clampIdx:]
             else:
                 x[clampIdx:vsize] = random.binomial(
                     1, 1. / (1. + exp(-v * T)), vsize)[clampIdx:]
             h = dot(x[:vsize], w) + bh
             x[vsize:] = 1. / (1. + exp(-h * T))
             if self.binary and not self.meanField:
                 x[vsize:] = random.binomial(1, x[vsize:], hsize)
             if j >= self.burn:
                 if self.binary:
                     y = x
                     if self.meanField:
                         y = random.binomial(1, x, vsize + hsize)
                     sample.append(TernaryString.fromArray(y))
                 else:
                     sample.append(x)
     return sample
Esempio n. 17
0
 def partition(self):
     """ compute the partition function - only for small dimension !!! """
     from pyec.util.TernaryString import TernaryString
     total = 0
     vsize = self.vsize
     hsize = self.hsize
     all = (1L << (vsize + hsize)) - 1L
     for i in xrange(1 << (vsize + hsize)):
         total += exp(self.__call__(TernaryString(long(i), all)))
     return total
Esempio n. 18
0
 def mutate(self, x):
    """This method uses an iterative algorithm to flip bits in
    a TernaryString with a given probability. The algorithm essentially
    uses the binary representation of the bit-flipping probability in order
    to convert random byte sampling (``p=.5``) to account for any probability,
    with resolution of ``1e-16``.
    
    """
    flipped = TernaryString.bernoulli(self.p(), self.config.space.dim)
    base = x.base & ~flipped.base | ~x.base & flipped.base
    known = x.known & flipped.known
    return TernaryString(base, known, self.config.space.dim)
Esempio n. 19
0
 def complete(self, data, sample=True):
     completed = []
     for v in data:
         x = zeros(self.vsize + self.hsize)
         x[: self.vsize] = v
         h = dot(v, self.w) + self.bh
         if sample:
             x[self.vsize :] = random.binomial(1, 1.0 / (1.0 + exp(-h)), self.hsize)
             completed.append(TernaryString.fromArray(x))
         else:
             x[self.vsize :] = 1.0 / (1.0 + exp(-h))
             completed.append(x)
     return completed
Esempio n. 20
0
 def complete(self, data, sample=True):
     completed = []
     for v in data:
         x = zeros(self.vsize + self.hsize)
         x[:self.vsize] = v
         h = dot(v, self.w) + self.bh
         if sample:
             x[self.vsize:] = random.binomial(1, 1. / (1. + exp(-h)),
                                              self.hsize)
             completed.append(TernaryString.fromArray(x))
         else:
             x[self.vsize:] = 1. / (1. + exp(-h))
             completed.append(x)
     return completed
Esempio n. 21
0
 def completeV(self, data, sample=True):
     completed = []
     for h in data:
         h2 = h.toArray(self.bh.size)
         x = zeros(self.vsize + self.hsize)
         x[self.vsize :] = h2
         v = dot(self.w, h2) + self.bv
         if sample:
             x[: self.vsize] = random.binomial(1, 1.0 / (1.0 + exp(-v)), self.vsize)
             completed.append(TernaryString.fromArray(x))
         else:
             x[: self.vsize] = 1.0 / (1.0 + exp(-v))
             completed.append(x)
     return completed
Esempio n. 22
0
 def __call__(self, rbm, size=1, initial=None, useAlternate=False, clampIdx=None):
    from pyec.util.TernaryString import TernaryString
    sample = []
    vsize = rbm.vsize
    hsize = rbm.hsize
    if useAlternate:
       w = rbm.wg
       bv = rbm.bvg
       bh = rbm.bhg
    else:
       w = rbm.w
       bv = rbm.bv
       bh = rbm.bh
    if clampIdx is None:
       clampIdx = 0
    if initial is not None:
       initialArr = initial.toArray(rbm.dim)
    for i in xrange(self.chains):
       if initial is not None:
          x = initialArr
       else:
          T0 = 1. / 4
          x = random.random_sample(vsize+hsize).round()
          h = dot(x[:vsize], w) + bh
          h = 1. / (1. + exp(-h*T0))
          if self.meanField:
             x[vsize:] = h
          else:
             x[vsize:] = random.binomial(1, h, hsize)
       for j in xrange(self.burn + size / self.chains):
          print "Gibbs: chain ", i, j
          T = maximum(( 3*self.burn + j ) / float(4*self.burn), 1.0)
          v = dot(w, x[vsize:]) + bv
          if self.meanField:
             x[clampIdx:vsize] = (1. / (1. + exp(-v*T)))[clampIdx:]
          else:
             x[clampIdx:vsize] = random.binomial(1, 1. / (1. + exp(-v*T)), vsize)[clampIdx:]
          h = dot(x[:vsize], w) + bh
          x[vsize:] = 1. / (1. + exp(-h*T))
          if self.binary and not self.meanField:
             x[vsize:] = random.binomial(1, x[vsize:], hsize)
          if j >= self.burn:
             if self.binary:
                y = x
                if self.meanField:
                   y = random.binomial(1, x, vsize+hsize)
                sample.append(TernaryString.fromArray(y))
             else:
                sample.append(x)
    return sample
Esempio n. 23
0
 def __call__(self, network):
    numBytes = int(ceil(len(network.variables) / 8.0))
    numFull  = len(network.variables) / 8
    initial = ''
    if numBytes != numFull:
       extra = len(network.variables) % 8
       initMask = 0
       for i in xrange(extra):
          initMask <<= 1
          initMask |= 1
       initial = struct.pack('B',initMask)
          
    base = long(binascii.hexlify(random.bytes(numBytes)), 16)
    known = long(binascii.hexlify(initial + '\xff'*numFull), 16)
    return TernaryString(base, known)
Esempio n. 24
0
 def __call__(self, orgs, prob):
     if np.random.random_sample() > prob:
         return orgs[0]
     if self.config.space.type == np.ndarray:
         rnd = np.random.random_sample(len(orgs[0])).round()
         return rnd * orgs[0] + (1 - rnd) * orgs[1]
     elif self.config.space.type == TernaryString:
         rnd = np.random.bytes(len(str(orgs[0])) * 8)
         rnd = long(binascii.hexlify(rnd), 16)
         base = rnd & orgs[0].base | ~rnd & orgs[1].base
         known = rnd & orgs[0].known | ~rnd & orgs[1].known
         return TernaryString(base, known, self.config.space.dim)
     else:
         err = "Unknown type for UniformCrossover: {0}"
         raise NotImplementException(err.format(self.config.space.type))
Esempio n. 25
0
 def completeV(self, data, sample=True):
     completed = []
     for h in data:
         h2 = h.toArray(self.bh.size)
         x = zeros(self.vsize + self.hsize)
         x[self.vsize:] = h2
         v = dot(self.w, h2) + self.bv
         if sample:
             x[:self.vsize] = random.binomial(1, 1. / (1. + exp(-v)),
                                              self.vsize)
             completed.append(TernaryString.fromArray(x))
         else:
             x[:self.vsize] = 1. / (1. + exp(-v))
             completed.append(x)
     return completed
Esempio n. 26
0
 def complete(self, vs, dummy=False):
    hs = self.meanFieldUp(len(vs), vs)
    xs = [append(vs[i], h, axis=0) for i,h in enumerate(hs)]
    return [TernaryString.fromArray(random.binomial(1, x, shape(x))) for x in xs] 
Esempio n. 27
0
    def __call__(self, rbm, size=1, initial=None, useAlternate=False):
        from pyec.util.TernaryString import TernaryString
        sample = []
        vsize = rbm.vsize
        hsize = rbm.hsize
        if useAlternate:
            w = rbm.wg
            bv = rbm.bvg
            bh = rbm.bhg
        else:
            w = rbm.w
            bv = rbm.bv
            bh = rbm.bh
        lv = rbm.lv
        lh = rbm.lh

        biases = append(bv, bh, axis=0)
        wp = 1. - exp(-2 * w)
        wn = 1. - exp(2 * w)
        lvp = 1. - exp(-2 * lv)
        lvn = 1. - exp(2 * lv)
        lhp = 1. - exp(-2 * lh)
        lhn = 1. - exp(2 * lh)

        if initial is not None:
            initialArr = initial.toArray(rbm.dim)
        start = time()
        for n in xrange(self.chains):
            start = time()
            if initial is not None:
                x = initialArr
            else:
                x = random.random_sample(vsize + hsize).round()
                h = dot(x[:vsize], w) + bh
                x[vsize:] = random.binomial(1, 1. / (1. + exp(-h)), hsize)
            last = copy(x)
            repeats = 0
            for l in xrange(self.burn + size / self.chains):
                print "SW: chain ", n, l
                startInner = time()
                clusters = array([i for i in xrange(vsize + hsize)], dtype=int)
                r = random.random_sample((vsize, hsize))
                rh = random.random_sample((hsize, hsize))
                rv = random.random_sample((vsize, vsize))
                for i in xrange(vsize):
                    for j in xrange(hsize):
                        i2 = j + vsize
                        if (w[i,j] < 0.0 and x[i] != x[i2] and r[i,j] < wn[i,j]) or \
                         (w[i,j] > 0.0 and x[i] == x[i2] and r[i,j] < wp[i,j]):
                            #if d[i,j] > 0.0:
                            if clusters[i] != clusters[i2]:
                                # take the lesser number
                                if clusters[i] < clusters[i2]:
                                    c1 = clusters[i]
                                    c2 = clusters[i2]
                                else:
                                    c1 = clusters[i2]
                                    c2 = clusters[i]
                                ifi2 = maximum(1 - abs(clusters - c2), 0)
                                noti2 = 1 - ifi2
                                clusters = ifi2 * c1 + noti2 * clusters

                for i in xrange(vsize):
                    for j in xrange(vsize):
                        if i == j: continue
                        i2 = j
                        if (lv[i,j] < 0.0 and x[i] != x[i2] and rv[i,j] < lvn[i,j]) or \
                         (lv[i,j] > 0.0 and x[i] == x[i2] and rv[i,j] < lvp[i,j]):
                            #if d[i,j] > 0.0:
                            if clusters[i] != clusters[i2]:
                                # take the lesser number
                                if clusters[i] < clusters[i2]:
                                    c1 = clusters[i]
                                    c2 = clusters[i2]
                                else:
                                    c1 = clusters[i2]
                                    c2 = clusters[i]
                                ifi2 = maximum(1 - abs(clusters - c2), 0)
                                noti2 = 1 - ifi2
                                clusters = ifi2 * c1 + noti2 * clusters

                for i in xrange(hsize):
                    for j in xrange(hsize):
                        if i == j: continue
                        i2 = j
                        if (lh[i,j] < 0.0 and x[i] != x[i2] and rh[i,j] < lhn[i,j]) or \
                         (lh[i,j] > 0.0 and x[i] == x[i2] and rh[i,j] < lhp[i,j]):
                            #if d[i,j] > 0.0:
                            if clusters[i] != clusters[i2]:
                                # take the lesser number
                                if clusters[i] < clusters[i2]:
                                    c1 = clusters[i]
                                    c2 = clusters[i2]
                                else:
                                    c1 = clusters[i2]
                                    c2 = clusters[i]
                                ifi2 = maximum(1 - abs(clusters - c2), 0)
                                noti2 = 1 - ifi2
                                clusters = ifi2 * c1 + noti2 * clusters

                computed = zeros(x.size)
                for k in xrange(x.size):
                    if computed[k] > 0.0:
                        continue

                    incluster = maximum(1 - abs(clusters - clusters[k]), 0)
                    bias = (incluster * biases).sum()
                    if self.binary:
                        state = float(
                            random.binomial(1, 1. / (1. + exp(-bias)), 1))
                    else:
                        state = float(1. / (1. + exp(-bias)))
                    computed += incluster
                    x = incluster * state + (1 - incluster) * x

                if l >= self.burn:
                    if self.binary:
                        sample.append(TernaryString.fromArray(x))
                    else:
                        sample.append(x)
                        x = random.binomial(1, state, x.size)
            print "SW sample ", n, ": ", time() - start

        return sample
Esempio n. 28
0
 def test_random(self):
     total = np.zeros(100)
     for i in xrange(10000):
         total += TernaryString.random(100).toArray() / 10000.0
     print "Total max: ",
     self.assertTrue(np.abs(total - .5).max() < .025)
Esempio n. 29
0
 def extent(self):
     lower = 0L | (self.spec.known & self.spec.base)
     upper = -1L & (self.spec.known & self.spec.base | ~self.spec.known)
     return (TernaryString(lower, self.spec.known, self.spec.length),
             TernaryString(upper, self.spec.known, self.spec.length))
Esempio n. 30
0
 def values(self):
    mask = 1L << self.index
    yield TernaryString(0L, mask)
    yield TernaryString(mask, mask)
    raise StopIteration 
Esempio n. 31
0
 def configurations(self):
    cnt = 1L << (len(self.parents))
    for i in xrange(cnt):
       yield TernaryString(self.expand(long(i)), self.known)
    raise StopIteration
Esempio n. 32
0
 def test_random(self):
    total = np.zeros(100)
    for i in xrange(10000):
       total += TernaryString.random(100).toArray() / 10000.0
    print "Total max: ", 
    self.assertTrue(np.abs(total - .5).max() < .025)
Esempio n. 33
0
 def __call__(self, rbm, size=1, initial = None, useAlternate=False):
    from pyec.util.TernaryString import TernaryString
    sample = []
    vsize = rbm.vsize
    hsize = rbm.hsize
    if useAlternate:
       w = rbm.wg
       bv = rbm.bvg
       bh = rbm.bhg
    else:
       w = rbm.w
       bv = rbm.bv
       bh = rbm.bh
    lv = rbm.lv
    lh = rbm.lh
    
    biases = append(bv, bh, axis=0) 
    wp = 1. - exp(-2 * w )
    wn =  1. - exp(2 * w )
    lvp = 1. - exp(-2 * lv)
    lvn = 1. - exp(2 * lv)
    lhp = 1. - exp(-2 * lh)
    lhn = 1. - exp(2 * lh)
    
          
    if initial is not None:
       initialArr = initial.toArray(rbm.dim)
    start = time()
    for n in xrange(self.chains):
       start = time()
       if initial is not None:
          x = initialArr
       else:
          x = random.random_sample(vsize + hsize).round()
          h = dot(x[:vsize], w) + bh
          x[vsize:] = random.binomial(1, 1. / (1. + exp(-h)), hsize)
       last = copy(x)
       repeats = 0
       for l in xrange(self.burn + size / self.chains):
          print "SW: chain ", n, l
          startInner = time()
          clusters = array([i for i in xrange(vsize + hsize)], dtype=int)
          r = random.random_sample((vsize, hsize))
          rh = random.random_sample((hsize, hsize))
          rv = random.random_sample((vsize, vsize))
          for i in xrange(vsize):
             for j in xrange(hsize):
                i2 = j + vsize
                if (w[i,j] < 0.0 and x[i] != x[i2] and r[i,j] < wn[i,j]) or \
                 (w[i,j] > 0.0 and x[i] == x[i2] and r[i,j] < wp[i,j]):
                #if d[i,j] > 0.0:
                   if clusters[i] != clusters[i2]:
                      # take the lesser number
                      if clusters[i] < clusters[i2]:
                         c1 = clusters[i]
                         c2 = clusters[i2]
                      else:
                         c1 = clusters[i2]
                         c2 = clusters[i]
                      ifi2 = maximum(1 - abs(clusters - c2), 0)
                      noti2 = 1 - ifi2
                      clusters = ifi2 * c1 + noti2 * clusters
          
          for i in xrange(vsize):
             for j in xrange(vsize):
                if i == j: continue
                i2 = j
                if (lv[i,j] < 0.0 and x[i] != x[i2] and rv[i,j] < lvn[i,j]) or \
                 (lv[i,j] > 0.0 and x[i] == x[i2] and rv[i,j] < lvp[i,j]):
                #if d[i,j] > 0.0:
                   if clusters[i] != clusters[i2]:
                      # take the lesser number
                      if clusters[i] < clusters[i2]:
                         c1 = clusters[i]
                         c2 = clusters[i2]
                      else:
                         c1 = clusters[i2]
                         c2 = clusters[i]
                      ifi2 = maximum(1 - abs(clusters - c2), 0)
                      noti2 = 1 - ifi2
                      clusters = ifi2 * c1 + noti2 * clusters
          
          for i in xrange(hsize):
             for j in xrange(hsize):
                if i == j: continue
                i2 = j
                if (lh[i,j] < 0.0 and x[i] != x[i2] and rh[i,j] < lhn[i,j]) or \
                 (lh[i,j] > 0.0 and x[i] == x[i2] and rh[i,j] < lhp[i,j]):
                #if d[i,j] > 0.0:
                   if clusters[i] != clusters[i2]:
                      # take the lesser number
                      if clusters[i] < clusters[i2]:
                         c1 = clusters[i]
                         c2 = clusters[i2]
                      else:
                         c1 = clusters[i2]
                         c2 = clusters[i]
                      ifi2 = maximum(1 - abs(clusters - c2), 0)
                      noti2 = 1 - ifi2
                      clusters = ifi2 * c1 + noti2 * clusters
          
          computed = zeros(x.size)
          for k in xrange(x.size):
             if computed[k] > 0.0:
                continue
             
             
             incluster = maximum(1 - abs(clusters - clusters[k]), 0)
             bias = (incluster * biases).sum()
             if self.binary:
                state = float(random.binomial(1, 1. / (1. + exp(-bias)), 1))
             else:
                state = float(1. / (1. + exp(-bias)))
             computed += incluster 
             x = incluster * state + (1-incluster) * x
                
          if l >= self.burn:
             if self.binary:
                sample.append(TernaryString.fromArray(x))
             else:
                sample.append(x)
                x = random.binomial(1, state, x.size)
       print "SW sample ", n, ": ", time() - start   
       
    return sample