def train(reader, title): draw_source_data(reader, title) plt.show() # net train num_input = 2 num_output = 1 params = HyperParameters(num_input, num_output, eta=0.5, max_epoch=10000, batch_size=1, eps=2e-3, net_type=NetType.BinaryClassifier) net = NeuralNet(params) net.train(reader, checkpoint=1) # test print(Test(net, reader)) # visualize draw_source_data(reader, title) draw_split_line(net, reader, title) plt.show()
plt.scatter(x[i,0], x[i,1], marker='^', c='r', s=200) else: plt.scatter(x[i,0], x[i,1], marker='^', c='b', s=200) """ # 主程序 if __name__ == '__main__': # data reader = SimpleDataReader() reader.ReadData() draw_source_data(reader, show=True) # net num_input = 2 num_output = 1 params = HyperParameters(num_input, num_output, eta=0.1, max_epoch=10000, batch_size=10, eps=1e-3, net_type=NetType.BinaryClassifier) net = NeuralNet(params) net.train(reader, checkpoint=10) # show result draw_source_data(reader, show=False) draw_predicate_data(net) draw_split_line(net) plt.show()
# Copyright (c) Microsoft. All rights reserved. # Licensed under the MIT license. See LICENSE file in the project root for full license information. import numpy as np from HelperClass.NeuralNet import * from HelperClass.HyperParameters import * # 主程序 if __name__ == '__main__': # data reader = SimpleDataReader() reader.ReadData() # net num_input = 2 num_output = 1 params = HyperParameters(num_input, num_output, eta=0.1, max_epoch=100, batch_size=10, eps=1e-3, net_type=NetType.BinaryClassifier) net = NeuralNet(params) net.train(reader, checkpoint=1) # inference x_predicate = np.array([0.58, 0.92, 0.62, 0.55, 0.39, 0.29]).reshape(3, 2) a = net.inference(x_predicate) print("A=", a)
# Copyright (c) Microsoft. All rights reserved. # Licensed under the MIT license. See LICENSE file in the project root for full license information. from HelperClass.NeuralNet import * from HelperClass.HyperParameters import * if __name__ == '__main__': sdr = SimpleDataReader() sdr.ReadData() params = HyperParameters(1, 1, eta=0.1, max_epoch=100, batch_size=1, eps = 0.02) net = NeuralNet(params) net.train(sdr)
# Copyright (c) Microsoft. All rights reserved. # Licensed under the MIT license. See LICENSE file in the project root for full license information. # warning: 运行本程序将会得到失败的结果,这是by design的,是为了讲解课程内容,后面的程序中会有补救的方法 import numpy as np from HelperClass.NeuralNet import * if __name__ == '__main__': # data reader = SimpleDataReader() reader.ReadData() # net params = HyperParameters(eta=0.1, max_epoch=10, batch_size=1, eps = 1e-5) net = NeuralNet(params, 2, 1) net.train(reader, checkpoint=0.1) # inference x1 = 15 x2 = 93 x = np.array([x1,x2]).reshape(1,2) print(net.inference(x))
# get real weights def DeNormalizeWeightsBias(net, dataReader): W_real = np.zeros_like(net.W) for i in range(W_real.shape[0]): W_real[i, 0] = net.W[i, 0] / dataReader.X_norm[i, 1] #end for B_real = net.B - W_real[0, 0] * dataReader.X_norm[0, 0] - W_real[ 1, 0] * dataReader.X_norm[1, 0] return W_real, B_real if __name__ == '__main__': # data reader = SimpleDataReader() reader.ReadData() reader.NormalizeX() # net params = HyperParameters(eta=0.01, max_epoch=500, batch_size=10, eps=1e-5) net = NeuralNet(params, 2, 1) net.train(reader, checkpoint=0.1) # inference W_real, B_real = DeNormalizeWeightsBias(net, reader) print("W_real=", W_real) print("B_real=", B_real) x1 = 15 x2 = 93 x = np.array([x1, x2]).reshape(1, 2) z = np.dot(x, W_real) + B_real print("Z=", z)
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) print("A=", a) for i in range(3): if a[i,0] > 0.5: plt.scatter(x[i,0], x[i,1], marker='^', c='g', s=100) else: plt.scatter(x[i,0], x[i,1], marker='^', c='r', s=100) #end if #end for # 主程序 if __name__ == '__main__': # data reader = SimpleDataReader() reader.ReadData() # net params = HyperParameters(eta=0.1, max_epoch=10000, batch_size=10, eps=1e-3, net_type=NetType.BinaryClassifier) num_input = 2 num_output = 1 net = NeuralNet(params, num_input, num_output) net.train(reader, checkpoint=1) # show result draw_source_data(net, reader) draw_predicate_data(net) draw_split_line(net) plt.show()
# Licensed under the MIT license. See LICENSE file in the project root for full license information. import numpy as np from HelperClass.NeuralNet import * # main if __name__ == '__main__': # data reader = SimpleDataReader() reader.ReadData() reader.NormalizeX() reader.NormalizeY() # net params = HyperParameters(2, 1, eta=0.01, max_epoch=200, batch_size=10, eps=1e-5) net = NeuralNet(params) net.train(reader, checkpoint=0.1) # inference x1 = 15 x2 = 93 x = np.array([x1, x2]).reshape(1, 2) x_new = reader.NormalizePredicateData(x) z = net.inference(x_new) print("z=", z) Z_real = z * reader.Y_norm[0, 1] + reader.Y_norm[0, 0] print("Z_real=", Z_real)
# Copyright (c) Microsoft. All rights reserved. # Licensed under the MIT license. See LICENSE file in the project root for full license information. from HelperClass.NeuralNet import * from HelperClass.HyperParameters import * if __name__ == '__main__': sdr = SimpleDataReader() sdr.ReadData() params = HyperParameters(eta=0.1, max_epoch=100, batch_size=1, eps=0.02) net = NeuralNet(params, 1, 1) net.train(sdr)
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 params = HyperParameters(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(params) net.train(dataReader, checkpoint=500) ShowResult(net, dataReader, "Polynomial")
ax.scatter3D(X[:, 0], X[:, 1], Y[:, 0]) x_dr = np.linspace(0, 1, 50) y_dr = np.linspace(0, 1, 50) x_dr, y_dr = np.meshgrid(x_dr, y_dr) R = np.hstack((x_dr.ravel().reshape(2500, 1), y_dr.ravel().reshape(2500, 1))) z_dr = neural.Forward(R) z_dr = z_dr.reshape(-50, 50) ax.plot_surface(x_dr, y_dr, z_dr, cmap="rainbow") plt.show() if __name__ == '__main__': reader = DataReader(file_name) reader.ReadData() #读入数据 reader.NormalizeX() #归一化X reader.NormalizeY() #归一化Y # 具体的神经网络 hp = HyperParameters(2, 1, eta=0.01, max_epoch=100, batch_size=10, eps=1e-5) net = NeuralNet(hp) net.train(reader, checkpoint=0.1) print("W = ", net.W) print("B = ", net.B) showResult(reader, net)
from HelperClass.NeuralNet import * from HelperClass.DataReader import * file_name = os.getcwd() + '/iris.csv' if __name__ == '__main__': reader = DataReader(file_name) reader.ReadData() # print(reader.XTrainRaw) # print(reader.YTrainRaw) reader.NormalizeY(NetType.MultipleClassifier, base=1) reader.NormalizeX() reader.GenerateValidationSet() # print(reader.XTrain) # print(reader.YTrain) n_input = reader.num_feature n_hidden = 4 #四个输入类型(四个隐藏神经元) n_output = 3 #三输出 eta, batch_size, max_epoch = 0.1, 5, 10000 eps = 0.01 #学习步长 params = HyperParameters(n_input, n_hidden, n_output, eta, max_epoch, batch_size, eps, NetType.MultipleClassifier, InitialMethod.Xavier) net = NeuralNet(params, "Non-linear Classifier of Iris") net.train(reader, 10) net.ShowTrainingHistory() print("===输出===\nwb1.W = ", net.wb1.W, "\nwb1.B = ", net.wb1.B, "\nwb2.W = ", net.wb2.W, "\nwb2.B = ", net.wb2.B)
from numpy import array from HelperClass.NeuralNet import * from HelperClass.DataReader import * import os file_name = os.getcwd()+'/iris.csv' if __name__ == '__main__': #data reader = DataReader(file_name) reader.ReadData() reader.NormalizeX()#标准化特征值 reader.NormalizeY(NetType.MultipleClassifier, base=1)#将标签值转化成独热编码 reader.GenerateValidationSet()#产生验证集 n_input = reader.num_feature n_hidden = 4#(4个隐藏神经元) # n_hidden = 8 n_output = 3#三输出 eta, batch_size, max_epoch,eps = 0.1,5,10000,1e-3 hp = HyperParameters(n_input, n_hidden, n_output, eta, max_epoch, batch_size, eps, NetType.MultipleClassifier, InitialMethod.Xavier) net = NeuralNet(hp, "非线性分类") net.train(reader, 10) net.ShowTrainingHistory() print("===权重矩阵===\nwb1.W = ", net.wb1.W,"\nwb1.B = ", net.wb1.B,"\nwb2.W = ", net.wb2.W,"\nwb2.B = ", net.wb2.B)
plt.legend([p13,p23,p12], ["13","23","12"]) plt.axis([-0.1,1.1,-0.1,1.1]) DrawThreeCategoryPoints(xt[:,0], xt[:,1], yt[:], xlabel="x1", ylabel="x2", show=True, isPredicate=True) """ for i in range(xt.shape[0]): plt.scatter(xt[i,0], xt[i,1], marker='^', s=200) plt.show() """ # 主程序 if __name__ == '__main__': num_category = 3 reader = SimpleDataReader() 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(num_input, num_category, eta=0.1, max_epoch=100, batch_size=10, eps=1e-3, net_type=NetType.MultipleClassifier) net = NeuralNet(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)