コード例 #1
0
import numpy as np 
import matplotlib.pyplot as plt

import sys

server = None

net = Neural()

x = np.linspace(0, 4, 1000)
y = np.sin(x)

x_test = np.linspace(4, 8, 100)
x_test = y.reshape(y.shape[0], 1)

x = y.reshape(y.shape[0], 1)
y = x.reshape(x.shape[0], 1)


net.setparam(n_input=1, n_hidden=10, n_output=1, lr=0.00001)
net.init_weight()
net.train(x, y, 200)
net.accuracy()

cmd = sys.argv[1:]
if cmd:
    if cmd[0] == 'server' or 'serve':
        plt.plot(y)
        plt.plot([net.predict(i) for i in x_test])
        plt.show()
コード例 #2
0
PRINT_FROM = 10
PRINT_TO = 50
STEP_SHOW = 3
RHO = 0.9
EPSILON = 1e-4
STEP = 50
print "test"

trX, teX = load.loadMel("afterLDA100.db")
# trY, teY = load.loadRes()
trZ, teZ = load.loadLeter()
print "loaded"
neur = Neural()
# neur.load_from_file(WEIGHTS_FILE)
neur.init_model(ARR_SHAPE)
neur.train(trX, teX, trZ, teZ, epochs=TIMES)

neur.save_to_file(WEIGHTS_FILE)


# trT = neur.result(trX)
# teT = neur.result(teX)
# print "start train"
# neur2 = Neural()
# neur2.init_model([(55, 300), (300, 28)])
# neur2.train(trT, teT, trZ, teZ, epochs=TIMES)

# neur = Neural()
# neur.load_from_file(DIRECTORY_PATH + WEIGHTS_FILE)
# mel, res = procces_file(FILE)
# print res
コード例 #3
0
    def run(self):
        filename = self.file_path_label["text"]
        if (filename == ""):
            print("未選取檔案")
            tk.messagebox.showinfo("Error", "未選取資料集")
            return
        df = pd.read_table(filename, sep=" ", header=None)

        # 檢查是否二類問題
        df_label = np.split(df, [len(df.columns) - 1], axis=1)[1]
        if (len(df_label.groupby(len(df.columns) - 1).groups) != 2):
            print("非二類問題")
            tk.messagebox.showinfo("Error", "資料集非二類問題")
            return
            # 非二類問題
        # label非0/1組合 改變label-> 0~1
        if (0 not in df_label.groupby(len(df.columns) - 1).groups) or (
                1 not in df_label.groupby(len(df.columns) - 1).groups):
            df[len(df.columns) - 1] = df[len(df.columns) - 1] % 2

        # split traning data and testing data
        train_df = df.sample(frac=0.666666)
        test_df = df.drop(train_df.index)

        train_dfs = np.split(train_df, [len(train_df.columns) - 1], axis=1)
        train_X_df = train_dfs[0]
        train_y_df = train_dfs[1]
        train_X = train_X_df.values.tolist()
        train_y = train_y_df.values.reshape(-1, ).tolist()

        test_dfs = np.split(test_df, [len(test_df.columns) - 1], axis=1)
        test_X_df = test_dfs[0]
        test_y_df = test_dfs[1]
        test_X = test_X_df.values.tolist()
        test_y = test_y_df.values.reshape(-1, ).tolist()

        #learning_rate = 0.8
        learning_rate = self.learning_rate.get()

        # run training and show result
        n = Neural(train_X, train_y, learning_rate)
        train_result_list = []
        print("### training start ###")
        for i in range(int(self.epoch_spinbox.get())):
            self.training_epoch_text_label["text"] = i + 1
            train_result = n.train()
            train_result_list.append(train_result)
            if train_result["acc"] > float(self.early_stop_spinbox.get()):
                break
        print("### training end ###")
        self.draw_training_acc_figure(train_result_list)
        self.training_acc_text_label["text"] = train_result_list[
            len(train_result_list) - 1]["acc"]
        self.bias_text_label["text"] = train_result_list[len(train_result_list)
                                                         - 1]["bias"]
        self.weight_text.delete(1.0, END)
        self.weight_text.insert(
            1.0, train_result_list[len(train_result_list) - 1]["weight"])

        # run testing and show result
        print("### predict start ###")
        test_result = n.test(test_X, test_y)
        print("### predict end ###")

        self.testing_acc_text_label["text"] = test_result["acc"]

        # draw training data and predict line
        self.draw_training_data_figure(
            df, test_df, train_result_list[len(train_result_list) - 1])
コード例 #4
0
from neural import Neural
from sklearn.datasets import load_iris

net = Neural()

data = load_iris()

x, y = data.data, data.target

net.setparam(n_input=4, n_hidden=41, n_output=1, lr=0.000001)
net.init_weight()
net.train(x, y.reshape(y.shape[0], 1), 20000)
net.accuracy()