def train(reader, title):
    draw_source_data(reader, title, show=True)
    # net train
    num_input = reader.XTrain.shape[1]
    num_output = 1
    hp = HyperParameters_1_1(num_input, num_output, eta=0.5, max_epoch=5000, batch_size=1, eps=2e-3,
                             net_type=NetType.BinaryClassifier)
    net = NeuralNet_1_2(hp)
    net.train(reader, checkpoint=1)
    # test
    print(Test(net, reader))
    # visualize
    draw_source_data(reader, title, show=False)
    draw_split_line(net)
    plt.show()
Ejemplo n.º 2
0
                            show=True,
                            isPredicate=True)


# 主程序
if __name__ == '__main__':
    num_category = 3
    reader = DataReader_1_3(file_name)
    reader.ReadData()
    reader.ToOneHot(num_category, base=1)
    # show raw data before normalization
    ShowData(reader.XRaw, reader.YTrain)
    reader.NormalizeX()

    num_input = 2
    params = HyperParameters_1_1(num_input,
                                 num_category,
                                 eta=0.1,
                                 max_epoch=100,
                                 batch_size=10,
                                 eps=1e-3,
                                 net_type=NetType.MultipleClassifier)
    net = NeuralNet_1_2(params)
    net.train(reader, checkpoint=1)

    xt_raw = np.array([5, 1, 7, 6, 5, 6, 2, 7]).reshape(4, 2)
    xt = reader.NormalizePredicateData(xt_raw)
    output = net.inference(xt)
    print(output)

    ShowResult(reader.XTrain, reader.YTrain, xt, output)
    plt.plot(X[:,0], Y[:,0], '.', c='b')
    # create and draw visualized validation data
    TX1 = np.linspace(0,1,100).reshape(100,1)
    TX2 = np.hstack((TX1, TX1[:,]**2))
    TX3 = np.hstack((TX2, TX1[:,]**3))
    TX4 = np.hstack((TX3, TX1[:,]**4))
    TX5 = np.hstack((TX4, TX1[:,]**5))
    TX6 = np.hstack((TX5, TX1[:,]**6))
    TX7 = np.hstack((TX6, TX1[:,]**7))
    TX8 = np.hstack((TX7, TX1[:,]**8))
    TY = net.inference(TX8)
    plt.plot(TX1, TY, 'x', c='r')
    plt.title(title)
    plt.show()
#end def

if __name__ == '__main__':
    dataReader = DataReaderEx(file_name)
    dataReader.ReadData()
    dataReader.Add()
    print(dataReader.XTrain.shape)

    # net
    num_input = 8
    num_output = 1    
    hp = HyperParameters_1_1(num_input, num_output, eta=0.2, max_epoch=50000, batch_size=10, eps=1e-3, net_type=NetType.Fitting)
    #params = HyperParameters(eta=0.2, max_epoch=1000000, batch_size=10, eps=1e-3, net_type=NetType.Fitting)
    net = NeuralNet_1_2(hp)
    net.train(dataReader, checkpoint=500)
    ShowResult(net, dataReader, "Polynomial")