批处理csv文件的神经网络中的内存错误

时间:2019-07-09 08:22:50

标签: python pandas numpy scipy

获得了用于神经网络的python代码。我想训练网络以了解图片是否是技术图纸。因此,我得到了大约45个文件的一组训练图像和大约2000个文件的一组测试图像。我将所有文件重塑为28x28的大型灰度图像,并将它们转换为火车和测试的csv文件。然后,我调整了输出节点并更改了两个csvfiles的路径。我使用pandas添加了一个批处理过程,以逐块读取csv文件。我这样做是为了防止出现内存错误,因为测试数据的csv文件高于14GB。

这是神经网络的代码:

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 = 100000
learning_rate = 0.1
epochs = 5
chunksize = 5

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 chunk in pd.read_csv(training_data_file, chunksize=chunksize):
    process(chunk)
for e in range(epochs):
    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 chunk in pd.read_csv(test_data_file, chunksize=chunksize):
    process(chunk)
for record in test_data_list:

    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)

仍然,当我尝试运行网络时出现内存错误。完整的错误消息是:

回溯(最近通话最近):   文件“ C:\ Users \ Anwender \ Documents \ Uni \ KI \ Python \ code_nn__tech_draw.py”,第58行     对于pd.read_csv(training_data_file,chunksize = chunksize)中的块:    next 中的文件“ C:\ Users \ Anwender \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ pandas \ io \ parsers.py”,行1115     返回self.get_chunk()   在get_chunk中的文件1117行中的文件“ C:\ Users \ Anwender \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ pandas \ io \ parsers.py”     返回self.read(nrows = size)   已读取文件“ C:\ Users \ Anwender \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ pandas \ io \ parsers.py”,行1139     ret = self._engine.read(行)   读取文件“ C:\ Users \ Anwender \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ pandas \ io \ parsers.py”,行2039     索引=确保_索引_从_序列(数组)   文件“ C:\ Users \ Anwender \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ pandas \ core \ indexes \ base.py”,第5315行,在sure_index_from_sequences中     返回MultiIndex.from_arrays(序列,名称=名称)   文件“ C:\ Users \ Anwender \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ pandas \ core \ indexes \ multi.py”,行340,在from_arrays中     代码,级别= _factorize_from_iterables(数组)   文件“ C:\ Users \ Anwender \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ pandas \ core \ arrays \ categorical.py”,行2708,在_factorize_from_iterables中     返回地图(列表,lzip( [_ factorize_from_iterable(it)for iterables]))   文件“ C:\ Users \ Anwender \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ pandas \ core \ arrays \ categorical.py”,第2708行,在     返回地图(列表,lzip( [_ factorize_from_iterable(it)for iterables])) MemoryError

0 个答案:

没有答案