Beispiel #1
0
def go(state_in_string):
    #sanity check
    if type(state_in_string) == list:
        state_in_string = np.array(state_in_string)
    if type(state_in_string) == np.ndarray:
        state_in_string = state_in_string.flatten()
        state_in_string = ''.join(map(str, map(str, state_in_string)))
    if type(state_in_string) != str:
        tl.cprint('graphic:go:- wrong datatype of string/state')

    size = int(np.ceil((np.sqrt(len(state_in_string)))))
    print('size = {}, len = {}'.format(size, len(state_in_string)))
    box = int(pm.imsize / size)
    margin = int(box / 2)
    radius = int(0.8 * margin)

    circle_int_location, star_int_location = location(state_in_string,
                                                      size=size)
    coordinate1 = location_to_cordinate(circle_int_location,
                                        size=size,
                                        box=box)
    coordinate2 = location_to_cordinate(star_int_location, size=size, box=box)
    print('coordinate1 =', coordinate1, 'coordinate2 =', coordinate2)

    output = board(size)
    for centre1 in coordinate1:
        output = cv2.circle(output, centre1, radius, (0, 0, 255), -1)
    for centre2 in coordinate2:
        output = cv2.circle(output, centre2, radius, (255, 0, 0), -1)

    cv2.imwrite(pm.output_adress + '/' + state_in_string + '.jpg', output)
    return output
Beispiel #2
0
 def __init__(self,
              shape=None,
              opt='adamax',
              param='he',
              trainable=True,
              mode='valid'):
     self.x, self.y, self.dy, self.dx, self.dw, self.delta = np.repeat(
         None, 6)
     self.w = 0  # By defalut weights is 0 if shape is not specified, works good for add layer
     self.opt = eval(opt + '()')
     self.shape = shape
     self.trainable = trainable
     self.mode = mode  #only for convolution
     # Set param and sanity check
     if type(param) == str:  #  param init is inputed
         self.param = eval(param + '()')
         if shape != None:  # Dosn't init weights of Relu, cre , etc
             self.init_param()
     if type(param) in {list, np.ndarray}:  # set param and sanity check
         if self.shape == None or self.shape == param.shape:
             self.set_param(param)
         else:
             tl.cprint(
                 'layer:init:- layer shape ={} not matched with param shape {}'
                 .format(self.shape, param.shape))
Beispiel #3
0
def init():

    if os.path.exists('index.npy'):
        index = np.loadtxt('index.npy')
        index = int(index) + 1
    else:
        index = 10
    np.savetxt('index.npy', [index])

    Q = tl.initialize_Q()
    fl_Qtemp = os.getcwd() + '/data/Q' + str(index)
    pickle.dump(Q, open(fl_Qtemp, 'wb'))
    print(
        'init:- State saved at /TicTac/state and Q saved at TicTac/{}'.format(
            fl_Qtemp))
    print(
        'init:- Q initialized to zero value, filename ={}'.format('Q' +
                                                                  str(index)))

    if os.path.exists('state'):
        S = pickle.load(open('state', 'rb'))
        print('init:- State file found and loaded')
    else:
        S = list(Q.keys())
        print('init:- len of all state = {}'.format(len(S)))
        S = list(filter(tl.Valid_start_state, S))
        print('init:- Valid start state filtered in, now valid state len = {}'.
              format(len(S)))
        pickle.dump(S, open('state', 'wb'))

    # valid start start filtering
    tl.cprint('init:- Successfully done!\n')
Beispiel #4
0
 def forward(self, x, label):
     self.label = label.copy()
     self.x = x.copy()
     if np.sum(np.where(self.x < 0, -1, 0)) < 0:  #sanity check
         tl.cprint(
             'nn.py:cre:- Input are negative for cross entropy loss, input={}'
             .format(self.x))
         return
     self.label_entropy = -np.log2(self.label + 0.0001) * self.label
     self.x_entropy = -np.log2(self.x + 0.0001) * self.label
     self.y = self.x_entropy - self.label_entropy
     self.y = np.sum(self.y)  #KL divergence
     return self.y
Beispiel #5
0
def location(state_in_string, size=3):
    if len(state_in_string) != int(size * size):
        tl.cprint(
            'grapchic:location:- state_in_string is of length {}, non-square'.
            format(len(state_in_string)))

    circle_int_location = []
    star_int_location = []
    for num, loc in enumerate(state_in_string):
        if loc == '1':
            circle_int_location.append(num)
        if loc == '2':
            star_int_location.append(num)
    return circle_int_location, star_int_location
Beispiel #6
0
def model(data, update=True):
    img, label = data
    acc = []
    for x, y in zip(img, label):

        x = conv1.forward(x)
        x = add1.forward(x)
        x = conv2.forward(x)
        x = add2.forward(x)
        x = x.reshape([10])
        x = lin.forward(x)
        x = softmax.forward(x)
        loss = cre.forward(x, y)

        if update == True:
            dx = cre.backward()
            dx = softmax.backward(dx)
            dx = lin.backward(dx)
            dx = dx.reshape([10, 1, 1])
            dx = add2.backward(dx)
            dx = conv2.backward(dx)
            dx = add1.backward(dx)
            dx = conv1.backward(dx)

            conv1.update()
            conv2.update()
            add1.update()
            add2.update()
            lin.update()

        pred = np.argmax(x)
        l = np.argmax(y)
        if pred == l:
            acc.append(1)
        else:
            acc.append(0)
        ac = np.mean(acc)
        Accuracy.append(ac)

        tl.fprint(
            'pred={}, label = {},   acc = {:.4f},   loss = {:.4f}'.format(
                pred, l, ac, loss))
        if np.amax(np.abs(conv1.w)) > 5:
            tl.cprint('overflow')
            break
    return acc
Beispiel #7
0
def play():
    filename = input('Enter Q file adrress')
    Q = load_state(filename)
    toss = input('Do you want to start')
    state = '000000000'
    output = grid(state)
    plot(output)

    tl.cprint('you are STAR')
    p2 = 2
    p1 = 1
    if toss in {'y', 'Y', 'yes', 'Yes'}:
        while True:  #tl.Result(state)[0]==0:

            print('Current state is {}'.format(state))
            action = input('Enter your action')
            state = tl.SAS(state, action, p2)
            output = grid(state)
            plot(output)

            print('Current state is {}'.format(state))
            action = tl.Action(state, Q[state])
            print('Computer chose action {}'.format(action))
            state = tl.SAS(state, action, p1)

            output = grid(state)
            plot(output)

    else:
        while True:  #tl.Result(state)[0]==0:
            print('Current state is {}'.format(state))
            action = tl.Action(state, Q[state])
            print('Computer choose action {}'.format(action))
            state = tl.SAS(state, action, p1)
            output = grid(state)
            plot(output)
            print('Current state is {}'.format(state))
            action = input('Enter your action')
            state = tl.SAS(state, action, p2)
            output = grid(state)
            plot(output)
Beispiel #8
0
    return x, y, t


TT = []
SS = []
for i in range(1000):
    T = []
    S = []

    for x, y in zip(X, Y):
        x1, y1, t = model(x, y)
        T.append(t)
        if np.argmax(x1) == np.argmax(y1):
            S.append(1)
        else:
            S.append(0)
    TT.append(np.mean(T))
    SS.append(np.mean(S))
    tl.cprint('\n delta ={}'.format(
        np.mean(np.abs([l1.delta, l2.delta, l3.delta]))))
    tl.cprint('loss ={}, acc = {}\n'.format(np.mean(T), np.mean(S)))
    # st = 0.9*st+0.1*t
    # ST.append(st)
    # print(t,np.sum(np.abs(x-y)),st)

W = [[l1.w, l1.b], [l2.w, l2.b], [l3.w, l3.b]]
pickle.dump(W, open('data/W' + str(index_nn), 'wb'))
plt.plot(TT)
plt.savefig('data/L' + str(index_nn) + '.png')
plt.plot(SS)
plt.savefig('data/acc' + str(index_nn) + '.png')
Beispiel #9
0
        l = np.argmax(y)
        if pred == l:
            acc.append(1)
        else:
            acc.append(0)
        ac = np.mean(acc)
        Accuracy.append(ac)

        tl.fprint(
            'pred={}, label = {},   acc = {:.4f},   loss = {:.4f}'.format(
                pred, l, ac, loss))
        if np.amax(np.abs(conv1.w)) > 5:
            tl.cprint('overflow')
            break
    return acc


for i in range(5):
    acc = model((tni, tnl))
    tl.cprint('\n TicTac:mnist:- Training accuracy = {}, epoch ={}\n'.format(
        np.mean(acc), i),
              color='blue')
    W = [conv1.w, add1.w, conv2.w, add2.w, lin.w, lin.b]
    Acc = model((tti, ttl), update=False)
    tl.cprint('\n TicTac:mnist:- Test accuracy = {}, epoch = {}\n'.format(
        np.mean(Acc), i),
              color='blue')
pickle.dump(W, open('data/mnist/param' + str(index), 'wb'))
plt.plot(Accuracy)
plt.savefig('data/mnist/acc' + str(index) + '.png')
print('Trained weights saved at data/mnist/param{}'.format(index))