Пример #1
0
output_file = "/home/hugh/connect_comp/ProgrammingAssignment/Task1.txt"

mlp = MultiLayerPerceptron((2, 2, 1), hidden_activation="sigmoid", max_iters=10000, linear_factor=0.2, learning_rate=0.7,
                           verbose=(True, 1000,), output_activation="linear", output_file=output_file)


x = np.array([[1, 0], [0, 1], [0, 0], [1, 1]])
y = np.array([1, 1, 0, 0])

errors = mlp.fit(x, y)

plt.figure()
plt.plot(errors, color="blue", label="Sigmoid + Linear")
plt.title("Mean Squared Error Over Time")
plt.xlabel("$Epochs$")
plt.ylabel("$Error$")
plt.show()
#plt.savefig("/home/hugh/connect_comp/ProgrammingAssignment/Task1.png")

print("1, 1")
print(mlp.predict(np.array([1, 1])))
print("0, 0")
print(mlp.predict(np.array([0, 0])))
print("0, 1")
print(mlp.predict(np.array([0, 1])))
print("1, 0")
print(mlp.predict(np.array([1, 0])))

#mlp.to_str()

class QNeuralNework(object):
    """
    action: パターンの数だけ保持
    学習アルゴリズム: Q学習
    a = getNextAction(s)
    lean(S,a,r,S_next)
    ※ ゲームルールによらない汎用性を持たす
    """
    ALPHA = 0.5
    GAMMA = 0.9
    DATASET_NUMBER = 100
    LEAN_EPOCHS = 500
    LEAN_RATE = 0.2
    GREEDY_RATIO = 0.5

    def __init__(self, _numInput=3, numAction=3):
        self.action_paturn = range(numAction)
        self.learningObj = MultiLayerPerceptron(numInput=_numInput, numHidden=5, numOutput=numAction, activate1="tanh",
                                                activate2="sigmoid")
        self.X = []
        self.Y = []
        self.learnFlg = True

    def learn(self, o, a, r, o_next):
        """Q学習 or NeuralNetworkを使って,Q値を学習"""
        dQs = self.learningObj.predict(o)

        qk = dQs[a]
        maxQ = np.max(dQs)
        dQs[a] = qk + self.ALPHA * (r + self.GAMMA * maxQ - qk)

        self.X.append(np.asarray(o))
        self.Y.append(np.asarray(dQs))
        if len(self.X) > self.DATASET_NUMBER:
            self.X.pop(0)
            self.Y.pop(0)

        err = self.learningObj.fit(np.asarray(self.X), np.asarray(self.Y), learning_rate=self.LEAN_RATE,
                                   epochs=self.LEAN_EPOCHS)
        return err

    def getNextAction(self, o):
        a = None

        # 最大Q値の行動選択
        # 観測(observe)から、NNでQ値(配列)を取得
        Q_t = self.learningObj.predict(o)
        maxQt_idx = np.argmax(Q_t)
        best_actions = maxQt_idx

        # Q値最大の中からランダム選択
        a = np.random.choice(np.atleast_1d(best_actions))

        # if self.learnFlg:
        #     return a

        # greedyの行動選択
        import random
        if self.GREEDY_RATIO < random.random():
            return a
        else:
            return np.random.choice(self.action_paturn)
Пример #3
0
errors = mlp.fit(x_train, y_train)

plt.plot(errors, color="green", label="Tanh + Linear")

plt.legend()
#plt.show()
plt.savefig("/home/hugh/connect_comp/ProgrammingAssignment/Task3a.png")

correct = 0
total = 0
#file = open("/home/hugh/connect_comp/ProgrammingAssignment/Task3.txt", 'a+')
#file.write("\n\n")
#file.close()
for i in range(len(y_test)):

    with open("/home/hugh/connect_comp/ProgrammingAssignment/Task3.txt",
              'a+') as fh:
        prediction = mlp.predict(x_test[i])
        #fh.write("Prediction {0}\n".format(prediction))
        #fh.write("Actual {0}\n".format(y_test[i]))
        if prediction == y_test[i]:
            correct += 1

        total += 1

#with open("/home/hugh/connect_comp/ProgrammingAssignment/Task3.txt", 'a+') as fh:
#fh.write("Percentage accuracy on test set: {0}".format((correct/total)*100))

#mlp.to_str()
# -*- coding: utf-8 -*-
"""

MLP_test.py
多層パーセプトロン
"""

import numpy as np
import sys
from MultiLayerPerceptron import MultiLayerPerceptron
from ndprint import ndprint, ndprints

if __name__ == '__main__':
    # データセットの生成
    # ------------------
    X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
    y = np.array([0, 1, 1, 0])

    # 多層パーセプトロンの初期化
    mlp = MultiLayerPerceptron(numInput=2, numHidden=5, numOutput=1, activate1="tanh", activate2="identity")
    # パーセプトロンの学習
    mlp.fit(X, y,learning_rate=0.2, epochs=10000)
    # パーセプトロンを実行
    y0 = mlp.predict(X[0])

    for x, y in zip(X, y):
        print 'X:%s, y:%0.2f, pred:%0.2f' % (ndprint(x), y, mlp.predict(x))