コード例 #1
0
def mse_avg_table(train_x, train_y, test_x, test_y, file_name):
    with open(file_name, 'w', newline='') as fileOut:
        writer = csv.writer(fileOut,
                            delimiter=' ',
                            quotechar=' ',
                            quoting=csv.QUOTE_MINIMAL)
        writer.writerow([
            'hidden_neuron', 'avg_mse_train', 'st_d_train', 'avg_mse_test',
            'st_d_test'
        ])
        for i in range(
                1, 42, 5
        ):  # THIS RUNS FOR A LONG TIME!!! BECAUSE ITS 100 times training the neural network
            errors_train = []
            errors_test = []
            for j in range(100):
                print(str(j), '-ta iteracja')
                network = rbf.RBF(i)
                errors_train.append(
                    network.fit_two_stages(train_x, train_y, True))
                errors_test.append(network.fit_two_stages(
                    test_x, test_y, True))
            print(str(i) + ' neuron, errors train: ', errors_train)
            print(str(i) + ' neuron, errors test: ', errors_test)
            writer.writerow([
                j,
                round(np.average(errors_train), 4),
                round(np.std(errors_train), 4),
                round(np.average(errors_test), 4),
                round(np.std(errors_test), 4)
            ])
コード例 #2
0
def classification_test_table(data_test, data_train):
    test_dataset = transform_data(data_test, len(data_test), 4, 3)
    train_dataset = transform_data(data_train, len(data_train), 4, 3)
    with open('results/classification/classification_effect_table.txt',
              'w',
              newline='') as fileOut:
        writer = csv.writer(fileOut,
                            delimiter=' ',
                            quotechar=' ',
                            quoting=csv.QUOTE_MINIMAL)
        writer.writerow([
            'hidden_neurons', 'train_accuracy', 'st_d_train', 'test_accuracy',
            'st_d_test'
        ])
        for i in range(1, 42, 5):
            acc_train = []
            acc_test = []
            for j in range(100):
                network = rbf.RBF(i)
                network.fit_two_stages(train_dataset[:, :-3],
                                       train_dataset[:, -3:], False)
                acc_train.append(accuracy_test(train_dataset, network))
                acc_test.append(accuracy_test(test_dataset, network))
            writer.writerow([
                i + 1,
                round(np.average(acc_train), 1),
                round(np.std(acc_train), 2),
                round(np.average(acc_test), 1),
                round(np.std(acc_test), 2)
            ])
コード例 #3
0
def task4(hidden_neurons, train_x, train_y, test_x):
    time_jump = 5000
    color = ['brown', 'darkslategray', 'b', 'r', 'c', 'm', 'lime', 'g']
    plt.title("Approximation throughout epochs")
    plt.xlabel('x')
    plt.ylabel('f(x)')
    plt.grid(alpha=.4, linestyle='--')
    net = rbf.RBF(hidden_neurons)
    net.fit_two_stages(train_x, train_y, True, 1, 1)
    plt.plot(test_x, net.predict(test_x), c=color[0], label=str(1) + " epoch")
    for j in range(1, 6):
        net.fit_two_stages(train_x, train_y, True, 1, time_jump)
        plt.plot(test_x,
                 net.predict(test_x),
                 c=color[j],
                 label=str(j * time_jump) + " epoch",
                 linewidth=1)
    plt.scatter(train_data[:, 0],
                train_data[:, 1],
                c='k',
                label="Train data",
                marker='*',
                alpha=0.2)
    plt.legend(loc=2)
    plt.savefig(
        'results/approximation/approximation_throughout_epochs_data_1.png')
    plt.clf()
コード例 #4
0
    def _fit(self):  #train the network
        try:
            if self.radioButton_centrain.isChecked():
                self.centskip = self.spinBox_centers.value()
                if self.spinBox_centers.value() == 0: self.centskip = 1
                self.centers = self.trainx[::self.centskip]
            elif self.radioButton_rand.isChecked():
                self.randnums = self.spinBox_rand.value()
                self.centers = input.rancen(self.trainx, self.randnums)
            elif self.radioButton.isChecked():
                self.numlin = self.spinBox_equalcen.value()
                if self.numlin == 0: self.numlin = 1
                self.centers = input.lincen(self.trainx, self.numlin)
            elif self.radioButton_2.isChecked():
                if self.tempcents2 != []:
                    self.centers = self.tempcents2
            #self.listWidget.addItem("Начало обучения")
            if self.radioButton_3.isChecked():
                self.NeuralNetwork = rbf.RBF(self.centers,
                                             float(self.doubleSpinBox.value()),
                                             self.func)
            else:
                try:
                    self.NeuralNetwork = rbf.TwoLayerRBF(
                        self.centers, float(self.doubleSpinBox.value()),
                        self.func)
                except Exception as e:
                    self.warning.label.setText(str(e))
                    self.warning.show()

            start = time.time()
            self.NeuralNetwork.fit(self.trainx, self.trainy)
            finish = time.time()
            self.listWidget.addItem("Обучение завершено")
            self.listWidget.addItem("Время обучения:")
            tm = round(finish - start, 3)
            if tm > 60:
                tm /= 60
                tm = str(tm) + "мин"
            else:
                tm = str(tm) + "c"
            self.listWidget.addItem(tm)
            self.listWidget.addItem("")
            self.pushButton_saveweights.setEnabled(True)
            self.pushButton.setEnabled(True)
            if self.workx != []:
                self.pushButton_predict.setEnabled(True)
        except Exception as e:
            self.warning.label.setText(str(e))
            self.warning.show()
            self.listWidget.addItem("Ошибка")
            self.listWidget.addItem("")
コード例 #5
0
def classification_accuracy(train_data, test_data, columns):
    plt.title("Classification accuracy\n" + "(number of inputs " +
              str(len(columns)) + ", columns " + str(columns) + ")")
    plt.xlabel('number of radial neurons')
    plt.ylabel('accuracy')
    plt.grid(alpha=.4, linestyle='--')
    x = [0, 1, 11, 21, 31, 41]
    y = [0]
    for n_neurons in range(1, 42, 10):
        net = rbf.RBF(n_neurons)
        net.fit_two_stages(train_data[:, :-3], train_data[:, -3:], False)
        y.append(accuracy_test(test_data, net))
    plt.plot(x, y, label='train data', alpha=0.8)
    y = [0]
    for n_neurons in range(1, 42, 10):
        net = rbf.RBF(n_neurons)
        net.fit_two_stages(test_data[:, :-3], test_data[:, -3:], False)
        y.append(accuracy_test(test_data, net))
    plt.plot(x, y, label='test data', alpha=0.8)
    plt.legend()
    plt.savefig('results/classification/classification_accuracy_' +
                str(len(columns)) + '_inputs_c' + str(columns) + '.png')
    plt.clf()
コード例 #6
0
def select_ai(cl, data, targets, original_import):
    """ Select an AI. Create in instance of the needed class and return it. """
    ai = None

    # MLP
    if cl == "MLP":
        ai = mlp.MLP(data,
                     targets,
                     hidden_nodes=MLP_HIDDEN_NODES,
                     beta=MLP_BETA,
                     momentum=MLP_MOMENTUM)
        print "MLP created."

    # RBF
    elif cl == "RBF":
        # Adjust target format for RBF: [win, draw, loss]
        targets_rbf = ny.zeros((len(targets), 3))
        for i in range(len(targets)):
            win = 1 if targets[i] == WIN else 0
            draw = 1 if targets[i] == DRAW else 0
            loss = 1 if targets[i] == LOSS else 0
            targets_rbf[i] = [win, draw, loss]

        ai = rbf.RBF(data,
                     targets_rbf,
                     sigma=RBF_SIGMA,
                     rbfs_amount=RBF_NODES,
                     use_kmeans=RBF_KMEANS,
                     normalize=RBF_NORMALIZE)

        print "RBF created."

    # DTree
    elif cl == "DTree":
        data = []
        for value in original_import:
            zipped = zip(DATA_ATTRIBUTES, [datum for datum in value])
            data.append(dict(zipped))
        ai = dtree.DTree(data, DATA_ATTRIBUTES, DT_TARGET_ATTRIBUTE)

        print "DTree created."

    return ai
コード例 #7
0
def _plot_radial_functions(hidden_neurons,
                           train_x,
                           train_y,
                           test_x,
                           backward=True,
                           coefficient=1.0,
                           l_rate=0.2,
                           b_rate=0.2,
                           epochs=1000):
    network = rbf.RBF(hidden_neurons)
    network.fit_two_stages(train_x, train_y, backward, coefficient, epochs,
                           l_rate, b_rate)
    plt.plot(test_x, network.predict(test_x), label='network', linewidth=3)
    G = network.calculate_matrix(test_x, coefficient)
    for i in range(hidden_neurons):
        for j in range(len(test_x)):
            G[j][i] *= network.weights[i]
        lab = str(i) + 'neuron'
        plt.plot(test_x, G[:, i], label=lab, linewidth=2, alpha=0.7)
コード例 #8
0
def decision_boundaries():
    for i in range(3):
        for j in range(i + 1, 4):
            columns = [i, j]
            plt.title("Distribution of points for inputs number " +
                      str(i + 1) + " and " + str(j + 1))
            plt.xlabel('input ' + str(i + 1))
            plt.ylabel('input ' + str(j + 1))
            plt.grid(alpha=.4, linestyle='--')
            iris = datasets.load_iris()
            X = iris.data[:, columns]
            y = iris.target
            network = rbf.RBF(31)
            network.fit_two_stages(X, y, False)
            # Plotting decision regions
            plot_decision_regions(X, y, clf=network, legend=2)
            plt.savefig('results/classification/2222distribution_input_' +
                        str(i + 1) + '_and_' + str(j + 1))
            plt.clf()
コード例 #9
0
def _plot_5_predict(num,
                    step,
                    train_x,
                    train_y,
                    test_x,
                    backward=False,
                    coefficient=1.0,
                    l_rate=0.01,
                    b_rate=0.1,
                    epochs=10000):
    for i in range(1, num, step):
        network = rbf.RBF(i)
        network.fit_two_stages(train_x, train_y, backward, coefficient, epochs,
                               l_rate, b_rate)
        lab = str(i) + ' neuron(s)'
        plt.plot(test_x,
                 network.predict(test_x),
                 label=lab,
                 linewidth=2,
                 alpha=0.7)
コード例 #10
0
from sklearn.decomposition import PCA
print("PCA :", end=" ")
pca = PCA(0.8)

pca.fit(x_train)
x_train = pca.transform(x_train)
x_test = pca.transform(x_test)

print(pca.n_components_)
for i in range(len(y_train)):
    y_train[i] = y_train[i] % 2

for i in range(len(y_test)):
    y_test[i] = y_test[i] % 2

r = rbf.RBF(k=5, bias=True, kmeans=True)
start = time.process_time()
r.fit(x_train, y_train, showAcc=True, lr=0.01, epochs=13)

pos = -1
for i in range(0, 10000):
    k = x_test[i]

    pr = r.predictOne(k)
    if pr == y_test[i]:
        pos = i
        break
if pos != -1:
    k = x_test[pos]
    pr = r.predictOne(k)
    print(" Prediction : ", end=" ")
コード例 #11
0
    #from gym import wrappers
    env = gym.make('MountainCar-v0')
    #rewards = []
    runs = 1
    episodes = 300
    numOfTilings = 8
    alpha = 0.5
    n = 1
    nrbf_tiling = 8
    # use optimistic initial value, so it's ok to set epsilon to 0
    EPSILON = 0.0
    centers = rbf.generate_grid_centers(nrbf_tiling)
    print(centers)
    widths = 0.1 * np.ones(len(centers))

    rbfun = rbf.RBF(centers, widths, env.action_space.n)
    fMap = rbf.Rbf_2D_Feature_Map(rbfun)
    reward_fn = rbf.RbfReward(fMap,
                              np.array([-1. / len(centers) for _ in centers]),
                              env)

    valueFunction = ValueFunction(alpha, numOfTilings)
    for episode in range(0, episodes):
        returns, states_visited, time = run_episode(env, valueFunction, n,
                                                    False, EPSILON, reward_fn)
        #for s in states_visited:
        #    print(s, ",", reward_fn.get_reward(s))
        print("-" * 10)
        print(episode, returns, time)
        #print('episode:', episode, "steps", step)
    #pickle the controller (value function)