2017-12-21 6 views
0

keras를 사용하여 문장을 분류하는 RNN 모델을 작성하고 싶습니다. keras.preprocessing.text에서 Tokenizer를 사용하는 동안 메모리가 부족합니다.

나는 다음과 같은 코드를 시도 :

docs = [] 
with open('all_dga.txt', 'r') as f: 
    for line in f.readlines(): 
     dga_domain, _ = line.split(' ') 
     docs.append(dga_domain) 

t = Tokenizer() 
t.fit_on_texts(docs) 
encoded_docs = t.texts_to_matrix(docs, mode='count') 
print(encoded_docs) 

을하지만 MemoryError의를 얻었다. 모든 데이터를 메모리에로드 할 수없는 것 같았습니다. 이 출력은 다음과 같습니다.

Traceback (most recent call last): 
    File "test.py", line 11, in <module> 
    encoded_docs = t.texts_to_matrix(docs, mode='count') 
    File "/home/yurzho/anaconda3/envs/deepdga/lib/python3.6/site-packages/keras/preprocessing/text.py", line 273, in texts_to_matrix 
    return self.sequences_to_matrix(sequences, mode=mode) 
    File "/home/yurzho/anaconda3/envs/deepdga/lib/python3.6/site-packages/keras/preprocessing/text.py", line 303, in sequences_to_matrix 
    x = np.zeros((len(sequences), num_words)) 
MemoryError 

keras에 익숙한 사람은 데이터 세트를 사전 처리하는 방법을 알려주십시오.

미리 감사드립니다.

답변

0

t.texts_to_matrix(docs, mode='count')에 오류가 발생하여 t.fit_on_texts(docs)에서 어휘를 작성하는 데 문제가없는 것처럼 보입니다.

그래서 당신은 배치

from keras.preprocessing.text import Tokenizer 

t = Tokenizer() 

with open('/Users/liling.tan/test.txt') as fin: 
    for line in fin:  
     t.fit_on_texts(line.split()) # Fitting the tokenizer line-by-line. 

M = [] 

with open('/Users/liling.tan/test.txt') as fin: 
    for line in fin: 
     # Converting the lines into matrix, line-by-line. 
     m = t.texts_to_matrix([line], mode='count')[0] 
     M.append(m) 

의 문서를 변환 할 수 있습니다하지만 당신은 당신의 컴퓨터가 메모리에 데이터의 양을 처리 할 수없는 경우 나중에 어떤 점에서 MemoryError로 실행 볼 수 있습니다.

관련 문제