def SIN(max_epochs, learning_rate, no_hidden):
    np.random.seed(213)
    inputs = []
    outputs = []
    for i in range(0, 200):
        four_inputs_vector = list(np.random.uniform(-1.0, 1.0, 4))
        four_inputs_vector = [float(four_inputs_vector[0]),float(four_inputs_vector[1]), 
                              float(four_inputs_vector[2]),float(four_inputs_vector[3])]
        inputs.append(four_inputs_vector)
    inputs=np.array(inputs)

    for i in range(200):
        outputs.append(np.sin([inputs[i][0] - inputs[i][1] + inputs[i][2] - inputs[i][3]]))

    no_in = 4
    no_out = 1
    NN = MLP(no_in, no_hidden, no_out)
    NN.randomise()
    print('\nMax Epoch:\n' + str(max_epochs), file=log)
    print('\nLearning Rate:\n' + str(learning_rate), file=log)
    print('\nBefore Training:\n', file=log)

    for i in range(150):
        NN.forward(inputs[i],'tanh')
        print('Target:\t{}\t Output:\t {}'.format(str(outputs[i]),str(NN.O)), file=log)
    print('Training:\n', file=log)
    
    
#    training process
    for i in range(0, max_epochs):
        error = 0
        NN.forward(inputs[:150],'tanh')
        error = NN.backward(inputs[:150], outputs[:150],'tanh')
        NN.updateWeights(learning_rate)
       #prints error every 5% of epochs
        if (i + 1) % (max_epochs / 20) == 0:
            print(' Error at Epoch:\t' + str(i + 1) + '\t  is \t' + str(error), file=log)
    
    difference=float(0)
    print('\n Testing :\n', file=log)
    for i in range(150, len(inputs)):
        NN.forward(inputs[i], 'tanh')
        print('Target:\t{}\t Output:\t {}'.format(str(outputs[i]), str(NN.O)), file=log)
        difference+=np.abs(outputs[i][0]-NN.O[0])

    accuracy=1-(difference/50)
    accuracylist.append(accuracy)
    print('\nAccuracy:{}'.format(accuracy),file=log)
    print('\ntestError:{}'.format(difference/50),file=log)
예제 #2
0
def XOR(max_epochs, learning_rate):
    np.random.seed(1)
    inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
    outputs = np.array([[0], [1], [1], [0]])

    NI = 2
    NH = 4
    NO = 1
    NN = MLP(NI, NH, NO)

    NN.randomise()
    print('\nMax Epoch:\n' + str(max_epochs), file=log)
    print('\nLearning Rate:\n' + str(learning_rate), file=log)
    print('\nBefore Training:\n', file=log)
    for i in range(len(inputs)):
        NN.forward(inputs[i], 'sigmoid')
        print('Target:\t {}  Output:\t {}'.format(str(outputs[i]), str(NN.O)),
              file=log)
    print('\nTraining:\n', file=log)

    for i in range(0, max_epochs):
        NN.forward(inputs, 'sigmoid')
        error = NN.backward(inputs, outputs, 'sigmoid')
        NN.updateWeights(learning_rate)

        if (i + 1) % (max_epochs / 20) == 0:
            print(' Error at Epoch:\t' + str(i + 1) + '\t\t  is \t\t' +
                  str(error),
                  file=log)

    print('\n After Training :\n', file=log)

    accuracy = float(0)
    for i in range(len(inputs)):
        NN.forward(inputs[i], 'sigmoid')
        print('Target:\t {}  Output:\t {}'.format(str(outputs[i]), str(NN.O)),
              file=log)
        if (outputs[i][0] == 0):
            accuracy += 1 - NN.O[0]
        elif (outputs[i][0] == 1):
            accuracy += NN.O[0]
    print('\nAccuracy:{}'.format(accuracy / 4), file=log)
예제 #3
0
파일: RL.py 프로젝트: Tarkof/DRL
class RL_Agent():
    def __init__(self, episode_size=150):
        self.model = MLP((SCREEN_HEIGHT_g, SCREEN_WIDTH_g), 300)
        #self.load("models/model_1185.npz")
        self.activations = []
        self.frames = []
        self.states_alive = []

        self.episode_size = episode_size
        self.episode_decisions = np.zeros((8))

        self.episodes_wins = 0
        self.episodes_nb = 0
        self.iter = 0

    def explore_exploit(self, prediction):
        choice = np.random.rand()
        if choice <= .8:
            return prediction
        return one_hot(round(np.random.rand() * 7))

    def update(self, frame, is_dead):
        frame = preprocessing(frame)
        act_h, y_pred = self.model.forward_keep_activations(frame)
        y_pred = self.explore_exploit(y_pred)
        self.activations.append((act_h, y_pred))
        self.frames.append(frame)

        #print(y_pred)

        self.episode_decisions += one_hot(y_pred)

        self.iter += 1
        if is_dead or self.iter == self.episode_size:
            for activation, frame in zip(self.activations, self.frames):
                if is_dead:  # Agent is dead
                    #y_true = np.array([0, 0, 0, 0.5, 0.5, 0, 0, 0])
                    y_true = wrong_move(one_hot(activation[1]))
                    #print("\n", activation[1],'\n', y_true, '\n\n')
                    grads = self.model.gradients(frame, activation[0],
                                                 activation[1], y_true)
                    self.model.backward(grads)
                else:
                    y_true = one_hot(activation[1])
                    grads = self.model.gradients(frame, activation[0],
                                                 activation[1], y_true)
                    self.model.backward(grads)

            print(
                f"Episode #{self.episodes_nb} : {'LOSE' if is_dead else 'WIN'}   Decisions : {self.episode_decisions}"
            )

            self.reset()
            self.episodes_nb += 1
            self.episode_decisions.fill(0)

            if not is_dead:
                self.episodes_wins += 1

            if self.episodes_nb % 100 == 0:
                print(f"Last 100 episodes : {self.episodes_wins}%")
                self.episodes_wins = 0

        return np.argmax(y_pred)

    def reset(self):
        self.activations = []
        self.iter = 0

    def load(self, path):
        loaded = np.load(path)
        self.model.Wh = loaded['Wh']
        self.model.Bh = loaded['Bh']
        self.model.Wo = loaded['Wo']
        self.model.Bo = loaded['Bo']

    def save(self):
        np.savez_compressed("models/model_" + str(self.episodes_nb),
                            Wh=self.model.Wh,
                            Bh=self.model.Bh,
                            Wo=self.model.Wo,
                            Bo=self.model.Bo)
def letter(max_epochs, learning_rate):
    np.random.seed(1)

    inputs = []
    outputs = []
    doutput = []
    columns = [
        "letter", "x-box", "y-box", "width", "height", "onpix", "x-bar",
        "y-bar", "x2bar", "y2bar", "xybar", "x2ybr", "xy2br", "x-ege", "xegvy",
        "y-ege", "yegvx"
    ]

    df = pd.read_csv("letter-recognition.data", names=columns)
    doutput = df["letter"]

    for i in range(len(doutput)):
        outputs.append(ord(str(doutput[i])) - ord('A'))

    inputs = df.drop(["letter"], axis=1)
    inputs = np.array(inputs)
    inputs = inputs / 15  #normalization

    #train set
    inputs_train = inputs[:16000]
    categorical_y = np.zeros((16000, 26))
    for i, l in enumerate(outputs[:16000]):
        categorical_y[i][l] = 1
    outputs_train = categorical_y

    #test set
    inputs_test = inputs[16000:]
    #    categorical_y = np.zeros((4000, 26))
    #    for i, l in enumerate(outputs[16000:]):
    #        categorical_y[i][l] = 1
    #    outputs_test=categorical_y

    #training process
    no_in = 16
    no_hidden = 10
    no_out = 26

    NN = MLP(no_in, no_hidden, no_out)
    NN.randomise()
    print('\nMax Epoch:\n' + str(max_epochs), file=log)
    print('\nLearning Rate:\n' + str(learning_rate), file=log)
    print('\nTraining Process:\n', file=log)

    for i in range(0, max_epochs):
        NN.forward(inputs_train, 'tanh')
        error = NN.backward(inputs_train, outputs_train, 'tanh')
        NN.updateWeights(learning_rate)

        if (i + 1) % (max_epochs / 20) == 0:
            print(' Error at Epoch:\t' + str(i + 1) + '\t  is \t' + str(error),
                  file=log)

    #testing process
    def to_character0(outputvector):
        listov = list(outputvector)
        a = listov.index(max(listov))
        return chr(a + ord('A'))

    prediction = []
    for i in range(4000):
        NN.forward(inputs_test[i], 'tanh')
        #    print('Target:\t{}\t Output:\t{}'.format(str(outputs_test[i]),str(NN.O)))
        #    print('Target:\t{}\t Output:\t{}'.format(str(doutput[16000+i]),str(to_character0(NN.O))))
        prediction.append(to_character0(NN.O))

    def to_character(n):
        return chr(int(n) + ord('A'))

    correct = {to_character(i): 0 for i in range(26)}
    letter_num = {to_character(i): 0 for i in range(26)}

    print('==' * 30, file=log)
    for i, _ in enumerate(doutput[16000:]):
        letter_num[doutput[16000 + i]] += 1
        # Print some predictions
        if i % 300 == 0:
            print('Expected: {} | Output: {}'.format(doutput[16000 + i],
                                                     prediction[i]),
                  file=log)
        if doutput[16000 + i] == prediction[i]:
            correct[prediction[i]] += 1

    print('==' * 30, file=log)
    # Calculate the accuracy
    accuracy = sum(correct.values()) / len(prediction)
    print('Test sample size: {} | Correctly predicted sample size: {}'.format(
        len(prediction), sum(correct.values())),
          file=log)
    print('Accuracy: %.3f' % accuracy, file=log)

    # Performance on each class
    print('==' * 30, file=log)
    for k, v in letter_num.items():
        print('{} => Sample Number: {} | Correct Number: {} | Accuracy: {}'.
              format(k, v, correct[k], correct[k] / v),
              file=log)