Exemple #1
0
	def nextBatch(self, batchSize, window, C):
		batch = np.zeros((batchSize, len(self.trigrams.tri)))
		label = np.zeros((batchSize, 1))
		bind = 0
		assert(batchSize % C == 0)
		assert(2 * window >= C)
		while True:
			s = max(0, self.worPtr - window)
			e = min(len(self.data), self.worPtr + window + 1)
			self.trigrams.triList(self.data[self.worPtr], batch[bind : bind + C])
			if e - s - 1 < C:
				self.worPtr = self.worPtr + 1
				continue
			visited = [self.worPtr]
			for i in range(C):
				add = rint(s, e)
				while add in visited:
					add = rint(s, e)
				label[bind] = self.vocab.get(self.data[add], 0)
				visited.append(add)
				bind = bind + 1
			self.worPtr = self.worPtr + 1
			if self.worPtr == len(self.data):
				self.worPtr = 0
			if bind == batchSize:
				return [batch, label]
		return [batch, label]
Exemple #2
0
def test_universal_str(m, n):
    u = 256
    randstr = lambda: ''.join(map(chr, rint(0, u, rint(3, 25))))
    S = [randstr() for i in range(n)]
    h = uhashstr(u, m)

    H = [h(s) for s in S]

    import matplotlib.pyplot as plt
    #plt.hist(H)
    plt.hist(H, m)
    plt.xlim((0, m))
    plt.show()
Exemple #3
0
 def __init__(self, u, m, p = None):
     if p is None:
         p = primes[np.searchsorted(primes, max(u, m))]
     self.p, self.u, self.m = p, u, m
     self.a = rint(1, p)
     self.h = uhashint(p, m)
     self.c = self.a**np.arange(100).astype(object)
def fix_img(cap, frame, path, resize_shape, final_shape, mean, use_flip=False):
    num = random.sample(list(range(4)), 1)[0]
    cap.decode(path, 3, num, final_shape[1], 1, frame.ctypes._data)

    flip_rand = rint(0, 1)
    if flip_rand == 1 and use_flip:
        clip = np.flip(frame, 2).copy()
    else:
        clip = frame.copy()
    return clip
Exemple #5
0
def roll_die(val):
    from numpy.random import randint as rint
    out = 0
    plus_parts = val.split('+')
    for pp in plus_parts:
        minus_parts = pp.split('-')
        for mp in minus_parts[1:]:
            try:
                if 'd' in mp:
                    n, d = mp.split('d')
                else:
                    d = 1  #We have a constant bonus, the die rolls will be 1
                    n = mp
                if n.strip() == '':
                    n = 1
                n = int(n)
                d = int(d)
                out -= sum(rint(1, d + 1, n))  #rolling the die
            except ValueError:
                return None

        try:
            if 'd' in minus_parts[0]:
                n, d = minus_parts[0].split('d')
            else:
                d = 1  #We have a constant bonus, the die rolls will be 1
                n = minus_parts[0]
            if n.strip() == '':
                n = 1
            n = int(n)
            d = int(d)
            out += sum(rint(1, d + 1, n))
        except ValueError:
            return None
    print out

    return out
Exemple #6
0
def roll_die(val):
    from numpy.random import randint as rint
    out = 0
    plus_parts = val.split('+')
    for pp in plus_parts:
        minus_parts = pp.split('-')
        for mp in minus_parts[1:]:
            try: 
                if 'd' in mp:
                    n,d = mp.split('d')
                else:
                    d =1 #We have a constant bonus, the die rolls will be 1
                    n = mp
                if n.strip() == '':
                    n = 1
                n = int(n)
                d = int(d)
                out -= sum(rint(1,d+1,n)) #rolling the die
            except ValueError:
                return None
        
        try:
            if 'd' in minus_parts[0]:
                n,d = minus_parts[0].split('d')
            else:
                d = 1#We have a constant bonus, the die rolls will be 1
                n = minus_parts[0]
            if n.strip() == '':
                n = 1
            n = int(n)
            d = int(d)
            out += sum(rint(1,d+1,n))
        except ValueError:
            return None
    print out

    return out
Exemple #7
0
def test_universal_int(u, m, n, l = None):
    print 'u:', u
    print 'm:', m
    print 'n:', n
    X = rint(0, u, n)
    if l:
        H = [uhashint(u, m) for i in range(l)]
        Y = []
        for h in H:
            Y.extend([h(x) for x in X])
    else:
        h = uhashint(u, m)
        Y = [h(x) for x in X]
    import matplotlib.pyplot as plt
    plt.hist(Y, m)
    plt.xlim((0, m))
    plt.show()
Exemple #8
0
    'z', 'c', 'v', 'b', 'n', 'm'
],
           [
               'A', 'D', 'T', 'X', 'Q', 'W', 'R', 'P', 'S', 'F', 'G', 'H', 'J',
               'K', 'L', 'Z', 'C', 'V', 'B', 'N', 'M'
           ]]
colorNames = ['cyan', 'yellow', 'black', 'magenta', 'red']
colors = [(-1., 1., 1.), (1., 1., -1.), (-1., -1., -1.), (1., -1., 1.),
          (1., -1., -1.)]
targets = ['A', 'D', 'T', 'X']

responseTimes = numpy.zeros(numStim * numBlocks)
output = []

# dependent variables
stimLetter = rint(0, numLetters, (numStim, numBlocks))
stimColor = rint(0, numColors, (numStim, numBlocks))
stimCase = rint(0, numCases, (numStim, numBlocks))
stimTarget = []
for i in range(numBlocks):
    stimTarget.append(targets[i % 4])

# getting user input
Participant = raw_input("Please enter participant number: ")
Session = raw_input("Please enter session number: ")

#create a window
mywin = visual.Window([1300, 700],
                      monitor="testMonitor",
                      units="deg",
                      color=[0.0, 0.0, 0.0])
Exemple #9
0
#tree_deth = [12,14]

tries = 1

with open("MNIST.db", 'rb') as input_:
    datas = pickle.load(input_)

    Y = asarray(datas["target"]).astype('int64')

    print numpy.unique(Y, return_counts=True)

    for i in xrange(len(Y)):
        Y[i] = Y[i] + 1
    x = csr_matrix(preprocessing.normalize(datas["data"], copy=False, axis=0))

    ns = rint(0, x.shape[0], size=x.shape[0])
    x = x[ns]
    Y = Y[ns]

    x_train = x[:60000, :]
    x_validate = x[60000:, :]

    Y_train = Y[:60000]
    Y_validate = Y[60000:]

    x_sp_t = x_train
    x_sp_v = x_validate

    for gamma in [5500]:

        for d in tree_deth:
Exemple #10
0
    def trainVec(self, dim, window, C, batchSize, epoch, minEps=0.0001):

        model = NN3(len(self.trigrams), dim, len(self.trigrams), hType=0, oType=1, epsilon=0.001)

        decay = (model.epsilon - minEps) / (epoch - 1)

        batch = np.zeros((batchSize, len(self.trigrams)))
        label = np.zeros((batchSize, len(self.trigrams)))
        b = 0
        l = 1
        for itr in range(epoch):
            for sentence in self.corpus:
                for c in range(len(sentence)):
                    s = max(0, c - window)

                    e = min(len(sentence), c + window + 1)

                    if e - s - 1 < C:
                        continue
                    if self.exist:

                        ind = self.triList(sentence[c])

                        batch[b, ind] = 1
                    else:

                        batch[b] = self.triHot(sentence[c])

                    visited = [c]

                    for i in range(C):

                        add = rint(s, e)
                        while add in visited:

                            add = rint(s, e)
                        if self.exist:

                            ind = self.triList(sentence[add])

                            label[b, ind] = 1

                        else:

                            label[b] += self.triHot(sentence[add])

                        visited.append(add)
                    b = (b + 1) % batchSize
                    if b == 0:

                        model.batch(batch, label)

                        batch = np.zeros((batchSize, len(self.trigrams)))

                        label = np.zeros((batchSize, len(self.trigrams)))

                        print("Batch:", l)
                        l = l + 1
            model.epsilon = model.epsilon - decay
        if b != 0:
            model.batch(batch, label)
            print("Batch:", l)
        self.triVec = model.W
Exemple #11
0
from numpy.random import randint as rint

sdie = 1
our_dots = 0
some_rnd = 0

for i in range(100):
    root = Node(Board(0, 0, 1), None)

    print('[', end='')
    while not root.b.done():
        if root.b.side == sdie:
            if len(root.sons) == 0:
                root.expand()
            n = len(root.sons)
            n = rint(n)
            root = root.sons[n]
        else:
            root.mcts(1000)
            root = root.sons[root.chose()]
            print(root.prob01(), end=', ')
    print(']')

    root.mcts(10)

    if root.true_eval == sdie:
        some_rnd += 1
    else:
        our_dots += 1

    sdie *= -1
def gen_dataset(nt=1000, prior=0.7, dataset='toy'):
    Xs, ys, Xtest, ytest = load_dataset(dataset=dataset)
    Xp = Xs[ys == 1]
    Xn = Xs[ys == -1]
    lXp = len(Xp)
    lXn = len(Xn)

    n1 = int(nt * (1 - prior * (1 - prior)))
    n2 = nt - n1
    n1a = int(nt * prior * prior * prior)
    n1b = int(nt * prior * prior * (1 - prior))
    n1c = int(nt * prior * (1 - prior) * (1 - prior))
    n1d = n1 - n1a - 2 * (n1b + n1c)
    n2a = int(nt * prior * prior * (1 - prior))
    n2b = n2 - n2a

    Xt1 = np.concatenate(
        (np.hstack(
            (Xp[rint(lXp, size=n1a)], Xp[rint(lXp,
                                              size=n1a)], Xp[rint(lXp,
                                                                  size=n1a)])),
         np.hstack((Xp[rint(lXp, size=n1b)], Xp[rint(lXp, size=n1b)],
                    Xn[rint(lXn, size=n1b)])),
         np.hstack((Xp[rint(lXp, size=n1c)], Xn[rint(lXn, size=n1c)],
                    Xn[rint(lXn, size=n1c)])),
         np.hstack((Xn[rint(lXn, size=n1b)], Xp[rint(lXp, size=n1b)],
                    Xp[rint(lXp, size=n1b)])),
         np.hstack((Xn[rint(lXn, size=n1c)], Xn[rint(lXn, size=n1c)],
                    Xp[rint(lXp, size=n1c)])),
         np.hstack((Xn[rint(lXn, size=n1d)], Xn[rint(lXn, size=n1d)],
                    Xn[rint(lXn, size=n1d)]))))

    Xt2 = np.concatenate(
        (np.hstack(
            (Xp[rint(lXp, size=n2a)], Xn[rint(lXn,
                                              size=n2a)], Xp[rint(lXp,
                                                                  size=n2a)])),
         np.hstack((Xn[rint(lXn, size=n2b)], Xp[rint(lXp, size=n2b)],
                    Xn[rint(lXn, size=n2b)]))))

    return Xt1.astype(np.float32), Xt2.astype(np.float32), Xtest.astype(
        np.float32), ytest[:, None]
def gen_knn_dataset(nt=1000, prior=0.7, dataset='toy'):
    Xs, ys, Xtest, ytest = load_dataset(dataset=dataset)
    Xs = Xs.reshape((len(Xs), -1))
    Xtest = Xtest.reshape((len(Xtest), -1))
    Xs_ids = np.array(range(len(Xs)))
    Xp_ids = Xs_ids[ys == 1]
    Xn_ids = Xs_ids[ys == -1]
    lXp = len(Xp_ids)
    lXn = len(Xn_ids)

    n1 = int(nt * (1 - prior * (1 - prior)))
    n2 = nt - n1
    n1a = int(nt * prior * prior * prior)
    n1b = int(nt * prior * prior * (1 - prior))
    n1c = int(nt * prior * (1 - prior) * (1 - prior))
    n1d = n1 - n1a - 2 * (n1b + n1c)
    n2a = int(nt * prior * prior * (1 - prior))
    n2b = n2 - n2a

    n1a1s = Xp_ids[rint(lXp, size=n1a)]
    n1a2s = Xp_ids[rint(lXp, size=n1a)]
    n1a3s = Xp_ids[rint(lXp, size=n1a)]

    n1b1s = Xp_ids[rint(lXp, size=n1b)]
    n1b2s = Xp_ids[rint(lXp, size=n1b)]
    n1b3s = Xn_ids[rint(lXn, size=n1b)]

    n1c1s = Xp_ids[rint(lXp, size=n1c)]
    n1c2s = Xn_ids[rint(lXn, size=n1c)]
    n1c3s = Xn_ids[rint(lXn, size=n1c)]

    n1d1s = Xn_ids[rint(lXn, size=n1b)]
    n1d2s = Xp_ids[rint(lXp, size=n1b)]
    n1d3s = Xp_ids[rint(lXp, size=n1b)]

    n1e1s = Xn_ids[rint(lXn, size=n1c)]
    n1e2s = Xn_ids[rint(lXn, size=n1c)]
    n1e3s = Xp_ids[rint(lXp, size=n1c)]

    n1f1s = Xn_ids[rint(lXn, size=n1d)]
    n1f2s = Xn_ids[rint(lXn, size=n1d)]
    n1f3s = Xn_ids[rint(lXn, size=n1d)]

    n2a1s = Xp_ids[rint(lXp, size=n2a)]
    n2a2s = Xp_ids[rint(lXp, size=n2a)]
    n2a3s = Xn_ids[rint(lXn, size=n2a)]

    n2b1s = Xn_ids[rint(lXn, size=n2b)]
    n2b2s = Xn_ids[rint(lXn, size=n2b)]
    n2b3s = Xp_ids[rint(lXp, size=n2b)]

    cons_a = np.concatenate(
        (n1a1s, n1b1s, n1c1s, n1d1s, n1e1s, n1f1s, n2a1s, n2b1s))
    cons_b = np.concatenate(
        (n1a2s, n1b2s, n1c2s, n1d2s, n1e2s, n1f2s, n2a2s, n2b2s))
    cons_c = np.concatenate(
        (n1a3s, n1b3s, n1c3s, n1d3s, n1e3s, n1f3s, n2a3s, n2b3s))
    cons = (cons_a, cons_b, cons_a, cons_c)

    return Xs, cons, Xtest, ytest
Exemple #14
0
 def __init__(self, u, m, p = None, k = 40):
     if p is None:
         p = primes[np.searchsorted(primes, max(u, m))]
     self.p, self.u, self.m, self.k = p, u, m, k
     self.a = rint(1, p, k)
     self.b = rint(1, p, k)
Exemple #15
0
 def __init__(self, u, m , p = None):
     if p is None:
         p = primes[np.searchsorted(primes, max(u, m))]
     self.p, self.u, self.m = p, u, m
     self.a, self.b = rint(1, p, 2)