def improve_prediction_model(self, epochs=5): # Load Data Set print("Loading data set.") X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_practice_dataset( ) # Show some images (optional of course : ) ) # show_some_images(X_train_orig, X_test_orig) test_model = DeepNeuralNetwork(X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes) for i in range(epochs): parameters, accuracies = test_model.train(num_epochs=epochs, print_cost=True) new_model = PredictionModel(parameters, accuracies) if new_model > self: print( "\n\tNew model is better... Displaying accuracies and updating files.. " ) self = new_model print(self) self.save_model() else: print("Previous model is superior or equivalent.") print(self)
def main(): # Load Data Set print("Loading practice data set.") X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_practice_dataset() # Show some images (optional of course : ) ) # show_some_images(X_train_orig, X_test_orig) test_model = DeepNeuralNetwork(X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes) for i in range(5): parameters, accuracies = test_model.train(num_epochs = 1500, print_cost = True) new_model = PredictionModel(parameters, accuracies) parameters, accuracies = load_model() previous_model = PredictionModel(parameters, accuracies) if new_model > previous_model : print("\n\tNew model is better... Displaying accuracies and updating files.. ") print(new_model) save_model(new_model.parameters, new_model.accuracies) else: print("Previous model is superior.") parameters, accuracies = load_model() best_model = PredictionModel(parameters, accuracies) print(best_model)
def test_query(): layers = [28 * 28, 100, 10] learning_rate = 0.2 dnn = DeepNeuralNetwork(layers, learning_rate) training_data_list = dataset.get_data_list( "mnist_dataset/mnist_train_100.csv") for record in training_data_list[0:4]: label, inputs = dataset.get_scaled_data(record) targets = dataset.get_targets_data(layers[-1], label) outputs = dnn.layer_query(inputs, targets)
def test_dnn(): layers = [28 * 28, 200, 100, 50, 10] learning_rate = 0.2 dnn = DeepNeuralNetwork(layers, learning_rate) # 开始测试训练 print("start to train") # 训练方法1:用数据训练,采用较小的训练数据集 training_data_list = dataset.get_data_list( "mnist_dataset/mnist_train.csv")[:] # 测试全部数据 size = len(training_data_list) # 用于打印进度 for index, record in enumerate(training_data_list): label, inputs = dataset.get_scaled_data(record) targets = dataset.get_targets_data(layers[-1], label) dnn.layer_train(inputs, targets) # 打印进度 print_process(index, size) # 开始测试 print("start to test") test_data_list = dataset.get_data_list( "mnist_dataset/mnist_test.csv") scorecard = [] # 记分牌,保存每个测试数据的测试结果 right = 0 # 正确总数 size = len(test_data_list) # 用于打印进度 for index, record in enumerate(test_data_list): label, inputs = dataset.get_scaled_data(record) result = dnn.layer_query_result(inputs) # 对比神经网络预测结果和标签 if label == result: scorecard.append(1) right += 1 else: scorecard.append(0) # 打印进度 print_process(index, size) # 打印正确率 print("right rate=", right / len(test_data_list) * 100, "%") pass
def test_kdd_CICIDS(): # 设置初始化参数,采用的是mnist数据集,为28*28的手写数字图像,隐含层100,输出层10代表0~9的数字,学习率初始设为0.2 layers = [78, 40, 2] learning_rate = 0.5 n = DeepNeuralNetwork(layers, learning_rate) # labels_count = [0, 0, 0, 0, 0] # 第一步:开始训练 print("start to train") train = True if train is True: # 训练方法1:用数据训练,采用较小的训练数据集 _, training_data_list = dataset.get_kdd_CICIDS_data( "kdd/Friday-WorkingHours-Afternoon-DDos.pcap_ISCX.csv") training_data_list = random.sample(training_data_list, int(0.1 * len(training_data_list))) # training_data_list = dataset.get_data_list( # "kdd/KDDTest-21-normalization.txt.csv") size = len(training_data_list) # 用于打印进度 for index, record in enumerate(training_data_list): label, inputs = get_kdd_data_CICIDS(record) targets = numpy.zeros(layers[-1]) + 0.01 targets[label] = 0.99 # targets = numpy.zeros(output_nodes) # targets[label] = 1 n.layer_train(inputs, targets) # 打印进度 print_process(index, size) # 统计标签数 # labels_count[label] += 1 else: print("load data done") # 训练方法2:直接导入训练的结果(适用于已经有训练结果,即权值矩阵) n.load("w_input_hidden_kdd.txt", "w_hidden_output_kdd.txt") # 第二步:开始测试训练后的神经网络 print("start to test") _, test_data_list = dataset.get_kdd_CICIDS_data( "kdd/Monday-WorkingHours.pcap_ISCX.csv") test_data_list = random.sample(test_data_list, int(0.1 * len(test_data_list))) # test_data_list = dataset.get_data_list( # "kdd/KDDTrain+_22Percent-normalization.txt.csv") scorecard = [] # 记分牌,保存每个测试数据的测试结果 right = 0 # 正确总数 count = 0 # 用于打印进度 size = len(test_data_list) # 用于打印进度 for index, record in enumerate(test_data_list): label, inputs = get_kdd_data_CICIDS(record) result = n.layer_query_result(inputs) # 对比神经网络预测结果和标签 if label == result: scorecard.append(1) right += 1 else: scorecard.append(0) # 打印进度 print_process(index, size) # print(labels_count) # 打印正确率 print("right rate=", right / len(test_data_list) * 100, "%")