一所大学向我传递了他的神经网络代码。我想训练网络以识别图像是技术图纸还是其他东西。因为测试csv文件大约为14GB,所以我除了用pandas实现逐块读取方法外别无选择。可悲的是,这样做之后,神经网络似乎不再起作用。我的python shell告诉我,该程序已启动,但未提供任何内容。我等了大约一个小时,没有任何进展。因为,在执行繁重任务的任务管理器中,我的RAM或处理器上没有任何信号,所以我建议某些事情无法正常工作。由于我没有收到错误消息,所以感觉完全迷失了下一步。 代码如下:
import numpy as np
import scipy.special
from tqdm import tqdm
import csv
import pandas as pd
class neuralNetwork:
def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
self.inode = inputnodes
self.hnode = hiddennodes
self.onode = outputnodes
# =============================================================================
# self.wih=np.random.normal(0.0,pow(self.inode,-0.5),(self.hnode,self.inode))
# self.who=np.random.normal(0.0,pow(self.hnode,-0.5),(self.onode,self.hnode))
# =============================================================================
self.wih = np.random.normal(0, pow(self.hnode, -0.5), (self.hnode, self.inode))
self.who = np.random.normal(0, pow(self.onode, -0.5), (self.onode, self.hnode))
self.lr = learningrate
self.activation_function = lambda x: scipy.special.expit(x)
pass
def train(self, inputs_list, targets_list):
inputs = np.array(inputs_list, ndmin=2).T
targets = np.array(targets_list, ndmin=2).T
hidden_inputs = np.dot(self.wih, inputs)
hidden_outputs = self.activation_function(hidden_inputs)
final_inputs = np.dot(self.who, hidden_outputs)
final_outputs = self.activation_function(final_inputs)
output_errors = targets - final_outputs
hidden_errors = np.dot(self.who.T, output_errors)
self.who += self.lr * np.dot((output_errors * final_outputs
* (1.0 - final_outputs)), np.transpose(hidden_outputs))
self.wih += self.lr * np.dot((hidden_errors * hidden_outputs
* (1.0 - hidden_outputs)), np.transpose(inputs))
pass
def test(self, inputs_list):
inputs = np.array(inputs_list, ndmin=2).T
hidden_inputs = np.dot(self.wih, inputs)
hidden_outputs = self.activation_function(hidden_inputs)
final_inputs = np.dot(self.who, hidden_outputs)
final_outputs = self.activation_function(final_inputs)
return final_outputs
input_nodes = 784
hidden_nodes = 500
output_nodes = 10
learning_rate = 0.1
epochs = 5
chunksize = 10**8
n = neuralNetwork(input_nodes,hidden_nodes,output_nodes, learning_rate)
#training_data_file = open('C:/Users/Anwender/Documents/Uni/KI/Python/train.csv', 'r')
#training_data_list = training_data_file.readlines()
#training_data_file.close()
for e in range(epochs):
for chunk in pd.read_csv('C:/Users/Anwender/Documents/Uni/KI/Python/train.csv', chunksize=chunksize):
process(chunk)
for record in tqdm(get_chunk()): # train on this record
all_values = record.split(',')
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
targets = np.zeros(output_nodes) + 0.01
targets[int(float(all_values[0]))] = 0.99
n.train(inputs, targets)
pass
pass
#test_data_file = open('C:/Users/Anwender/Documents/Uni/KI/Python/test.csv', 'r')
#test_data_list = test_data_file.readlines()
#test_data_file.close()
scorecard = []
for record in test_data_list:
for chunk in pd.read_csv('C:/Users/Anwender/Documents/Uni/KI/Python/test.csv', chunksize=chunksize):
process(chunk)
for record in tqdm(get_chunk()):
all_values = record.split(',')
correct_label = int(all_values[0])
inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
outputs = n.test(inputs)
label = np.argmax(outputs)
if (label == correct_label):
scorecard.append(1)
else:
scorecard.append(0)
pass
pass
scorecard_array = np.asarray(scorecard)
print ("Genauigkeit = ", scorecard_array.sum() / scorecard_array.size)