def train(eta, max_epoch, batch_size, eps):
    hp = HyperParameters(1,
                         1,
                         eta=eta,
                         max_epoch=max_epoch,
                         batch_size=batch_size,
                         eps=eps)
    net = NeuralNet(hp)
    net.train(data_reader)
    show_result(net)
Beispiel #2
0
def train(reader, title):
    draw_source_data(reader, title, show=True)
    num_input = reader.x_train.shape[1]
    num_output = 1
    hp = HyperParameters(num_input, num_output, eta=0.5, max_epoch=100000, batch_size=1, eps=2e-3,
                         net_type=NetType.BinaryClassifier)
    net = NeuralNet(hp)
    net.train(reader, checkpoint=1)
    print(test(net, reader))
    draw_source_data(reader, title, show=False)
    draw_split_line(net)

def draw_split_line(net):
    b12 = -net.b[0, 0] / net.w[1, 0]
    w12 = -net.w[0, 0] / net.w[1, 0]
    x = np.linspace(0, 1, 10)
    y = w12 * x + b12
    plt.plot(x, y)
    plt.axis([-0.1, 1.1, -0.1, 1.1])
    plt.show()


def draw_predicate_data(net):
    x = np.array([0.58, 0.92, 0.62, 0.55, 0.39, 0.29]).reshape(3, 2)
    a = net.inference(x)
    draw_two_category_points(x[:, 0], x[:, 1], a[:, 0], show=False, is_predicate=True)


if __name__ == '__main__':
    reader = TanhDataReader(file_name)
    reader.read_data()
    reader.to_zero_one()

    hp = HyperParameters(2, 1, eta=0.1, max_epoch=10000, batch_size=10, eps=1e-3, net_type=NetType.BinaryTanh)
    net1 = NeuralNet(hp)
    net1.train(reader, checkpoint=10)

    draw_source_data(reader, show=False)
    draw_predicate_data(net1)
    draw_split_line(net1)
Beispiel #4
0
import numpy as np
from helper_class1.data_reader import DataReader
from helper_class1.hyper_parameters import HyperParameters
from helper_class1.neural_net import NeuralNet

file_name = '../../../ai-edu/A-基础教程/A2-神经网络基本原理简明教程/data/ch05.npz'

if __name__ == "__main__":
    reader = DataReader(file_name)
    reader.read_data()
    reader.normalize_x()
    reader.normalize_y()

    hp = HyperParameters(2, 1, eta=0.01, max_epoch=200, batch_size=10, eps=1e-5)
    net = NeuralNet(hp)
    net.train(reader, checkpoint=0.1)
    x1 = 15
    x2 = 93
    x = np.array([x1, x2]).reshape(1, 2)
    x_new = reader.normalize_predicate_data(x)
    z = net.inference(x_new)
    print('z=', z)
    z_true = z * reader.y_norm[0, 1] + reader.y_norm[0, 0]
    print('z_true=', z_true)
Beispiel #5
0
    return plt.plot(x, y, c=color)


if __name__ == '__main__':
    num_category = 3
    reader = DataReader(file_name)
    reader.read_data()
    reader.to_one_hot(num_category, base=1)
    draw_source_data(reader.x_raw, reader.y_train, show=True)
    reader.normalize_x()

    hp = HyperParameters(2,
                         3,
                         eta=0.1,
                         max_epoch=40000,
                         batch_size=10,
                         eps=1e-3,
                         net_type=NetType.MultipleClassifier)
    net1 = NeuralNet(hp)
    net1.train(reader, checkpoint=1)

    xt_raw = np.array([5, 1, 7, 6, 5, 6, 2, 7]).reshape(4, 2)
    xt = reader.normalize_predicate_data(xt_raw)
    output = net1.inference(xt)
    r = np.argmax(output, axis=1) + 1
    print("output=", output)
    print("r=", r)

    draw_source_data(reader.x_train, reader.y_train)
    draw_result(net1, xt, output)