2016-11-17 25 views
14

मैं tensorflow का उपयोग कर टेक्स्ट वर्गीकरण पर जंगली ब्लॉग का अनुसरण कर रहा हूं। मैं कोड बयान में max_document_length का उद्देश्य समझने में सक्षम नहीं हूँ:टेन्सफोर्लो vocabularyprocessor

vocab_processor = learn.preprocessing.VocabularyProcessor(max_document_length) 

इसके अलावा मैं कैसे vocab_processor

+1

मैं एक ही ट्यूटोरियल का पालन करने की कोशिश कर रहा हूं लेकिन कुछ चीजें हैं जिन्हें मैं समझ नहीं पा रहा हूं। हो सकता है कि आप [मेरे प्रश्न पर नज़र डालें] (http://stackoverflow.com/questions/41665109/trying-to-understand-cnns-for-nlp-tutorial-using-tensorflow) और मेरी मदद करें? – displayname

उत्तर

24

मैं पता लगा है कि कैसे vocabularyprocessor वस्तु से शब्दावली को निकालने के लिए स्थानीय लोगों की शब्दावली निकाल सकते हैं। यह मेरे लिए पूरी तरह से काम किया।

import numpy as np 
from tensorflow.contrib import learn 

x_text = ['This is a cat','This must be boy', 'This is a a dog'] 
max_document_length = max([len(x.split(" ")) for x in x_text]) 

## Create the vocabularyprocessor object, setting the max lengh of the documents. 
vocab_processor = learn.preprocessing.VocabularyProcessor(max_document_length) 

## Transform the documents using the vocabulary. 
x = np.array(list(vocab_processor.fit_transform(x_text)))  

## Extract word:id mapping from the object. 
vocab_dict = vocab_processor.vocabulary_._mapping 

## Sort the vocabulary dictionary on the basis of values(id). 
## Both statements perform same task. 
#sorted_vocab = sorted(vocab_dict.items(), key=operator.itemgetter(1)) 
sorted_vocab = sorted(vocab_dict.items(), key = lambda x : x[1]) 

## Treat the id's as index into list and create a list of words in the ascending order of id's 
## word with id i goes at index i of the list. 
vocabulary = list(list(zip(*sorted_vocab))[0]) 

print(vocabulary) 
print(x) 
+0

यदि आप vocab_dict देखते हैं, तो आप देख सकते हैं कि "यह" 1 के रूप में अनुक्रमित है, "है" 2 के रूप में और इसी तरह। मैं अपनी खुद की अनुक्रमणिका पास करना चाहता हूं। उदाहरण के लिए, आवृत्ति आधारित। क्या आप जानते हैं कि यह काम कैसे करना है? – user1930402

1

max_document_length

VocabularyProcessor का उद्देश्य समझने में सक्षम नहीं वैक्टर में अपने पाठ दस्तावेजों के नक्शे, और आप इन वैक्टर की जरूरत है एक सुसंगत लंबाई का माना जाता है।

आपका इनपुट डेटा रिकॉर्ड सभी समान लंबाई (या शायद नहीं) हो सकता है। उदाहरण के लिए यदि आप भावनात्मक विश्लेषण के लिए वाक्य के साथ काम कर रहे हैं तो वे विभिन्न लंबाई के होंगे।

आप यह पैरामीटर VocabularyProcessor पर प्रदान करते हैं ताकि यह आउटपुट वैक्टर की लंबाई समायोजित कर सके। the documentation को,

max_document_length अनुसार: दस्तावेज़ों की अधिकतम लंबाई। यदि दस्तावेज़ अधिक हैं, तो छोटे-छोटे पैड किए जाने पर उन्हें छंटनी की जाएगी।

source code देखें।

def transform(self, raw_documents): 
    """Transform documents to word-id matrix. 
    Convert words to ids with vocabulary fitted with fit or the one 
    provided in the constructor. 
    Args: 
     raw_documents: An iterable which yield either str or unicode. 
    Yields: 
     x: iterable, [n_samples, max_document_length]. Word-id matrix. 
    """ 
    for tokens in self._tokenizer(raw_documents): 
     word_ids = np.zeros(self.max_document_length, np.int64) 
     for idx, token in enumerate(tokens): 
     if idx >= self.max_document_length: 
      break 
     word_ids[idx] = self.vocabulary_.get(token) 
     yield word_ids 

लाइन word_ids = np.zeros(self.max_document_length) पर ध्यान दें।

raw_documents वैरिएबल में प्रत्येक पंक्ति max_document_length की वेक्टर के लिए मैप की जाएगी।

संबंधित मुद्दे