def recognize(sett): '''Обучение и тестирование нейронной сети''' # Инициализация экземпляра нейроннйо сети nn = NN(sett.input_nodes, sett.hidden_nodes, sett.output_nodes, sett.rate) # Загрузка тренировочных данных training_data = open("dataset/mnist_train_100.csv", 'r') training_list = training_data.readlines() training_data.close() # Обучение for _ in range(5): for record in training_list: all_values = record.split(',') inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01 targets = np.zeros(sett.output_nodes) + 0.01 targets[int(all_values[0])] = 0.99 nn.train(inputs, targets) # Загрзука тестовых данных test_data = open("dataset/mnist_test_10.csv", 'r') test_list = test_data.readlines() test_data.close() score_card = [] # Тестирование сети for record in test_list: all_values = record.split(',') correct_val = int(all_values[0]) inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01 outputs = nn.query(inputs) network_val = np.argmax(outputs) score_card.append(1 if correct_val == network_val else 0) # Загрузка входного изображения и преобразование его в массив img_array = smc.imread("numbs/new.png", flatten=True) img_data = 255.0 - img_array.reshape(784) img_data = (img_data / 255.0 * 0.99) + 0.01 # Выходные сигналы outputs_new = nn.query(img_data) # Значение цифры, которое имеет наибольший процент сходства с изображением network_val = np.argmax(outputs_new) return network_val
#define neural network model-1 parameters hidden_layers = [ 5, 5 ] # hidden layer size is passed as a list ith position value determines number of neuron in ith layer l_rate = 0.6 # learning rate epochs = 1000 # number of training epochs #check the timing of model-1 t0 = time.time() #start time #define model-1 model1 = NN(input_layer_size=m, output_layer_size=n_class, hidden_layer_size=hidden_layers) model1.train(X_train, Y_train, l_rate=l_rate, n_epochs=epochs) #end time t1 = time.time() total_time = t1 - t0 #prediction of our model on test and training data Y_train_predict = model1.predict(X_train) Y_test_predict = model1.predict(X_test) accuracy_train = 100 * np.sum(Y_train == Y_train_predict) / len(Y_train) accuracy_test = 100 * np.sum(Y_test == Y_test_predict) / len(Y_test) print("total time for training model-1:", total_time) print("accuracy of model-1 on training data", accuracy_train) print("accuracy of model-1 on test data", accuracy_test) #define neural network model-2 parameters hidden_layers = [