用于大数据格式化程序的Python垃圾收集器

时间:2017-08-31 19:20:39

标签: python arrays excel garbage-collection large-files

我编写了一个程序来读取excel文件的文件夹并将每个文件加载到程序中。然后它获取数据并创建一个大小为零的数组(3001,2001),它将被迭代通过,并且来自excel的相应坐标值将更改为1。然后将阵列重新整形为(1,6005001)的大小。我使用tensorflow重塑数组,因为程序认为它是一个元组,但最终的值存储在一个numpy数组中。我最终将最终格式化的数组存储到名为" filename_Array.csv"的csv文件中。然后程序转到下一个要格式化的excel文件。我在安装了tensorflow的Eclipse上运行Python

我遇到的问题是某些值被缓存在内存中,但我无法弄清楚它是什么。我已经尝试显式删除将重新初始化的大变量,并使用gc.collect()来清理存储的非活动内存。我仍然看到内存使用量稳步增加,直到大约25个文件格式化,然后计算机开始冻结,因为我的电脑上的所有RAM(12GB)正在使用。我知道python会自动为程序完全无法访问的值清除内存,因此我不确定这是否是RAM或其他内容碎片的问题。 对不起文字的墙,我只是想尽可能多地提供问题的信息。

在我因为计算机冻结而不得不终止程序之前,通过大约24个文件运行程序时,我的performance tab屏幕截图的链接。

这是我的代码:

from __future__ import print_function
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
import numpy as np
import csv

import gc

path = r'C:\Users\jeremy.desforges\Desktop\Eclipse\NN_MNIST\VAM SLIJ-II 4.500'

def create_array(g,h,trainingdata,filename):
    # Multiplying by factors of 10 to keep precision of data
    g = g*1000
    h = h*1
    max_g = 3000
    max_h = 2000

    # Initializes an array with zeros to represent a blank graph
    image = np.zeros((max_g+1,max_h+1),dtype=np.int)
    shape = ((max_g+1)*(max_h+1))

    # Fills the blank graph with the input data points
    for i in range(len(h)):
        image[g[i].astype('int'),h[i].astype('int')] = 1

    trainingdata.close()
    image = tf.reshape(image,[-1,shape])

    # Converts tensor objects to numpy arrays to feed into network
    sess = tf.InteractiveSession()
    image = sess.run(image)

    np.savetxt((filename + "_Array.csv"), np.flip(image,1).astype(int), fmt = '%i' ,delimiter=",")

    print(filename, "appended")
    print("size",image.shape)
    print(image,"=  output array")
    del image,shape,g,h,filename,sess
    return

# Initializing variables

image = []
shape = 1
g = 1.0
h = 1.0
f = 1
specials = '.csv'
folder = os.listdir(path)

for filename in folder:

    trainingdata = open(filename, "r+")
    filename = str(filename.replace(specials, ''))
    data_read = csv.reader(trainingdata)

    for row in data_read:
        in1 = float(row[0])
        in2 = float(row[1])    
        if (f==0):
            z_ = np.array([in1])
            g = np.hstack((g,z_))
            q = np.array([in2])
            h = np.hstack((h,q))
        if (f == 1):
            g = np.array([in1])
            h = np.array([in2])
            f = 0

    create_array(g,h,trainingdata,filename)

    gc.collect()
    image = []
    shape = 1
    g = 1.0
    h = 1.0
    f = 1

0 个答案:

没有答案
相关问题