2016-12-06 11 views
11

tensorflow MNIST tutorialmnist.train.next_batch(100) फ़ंक्शन बहुत आसान है। अब मैं एक साधारण वर्गीकरण को लागू करने की कोशिश कर रहा हूं। मेरे पास एक संख्यात्मक सरणी में मेरा प्रशिक्षण डेटा है। मुझे अपना बैच देने के लिए अपने डेटा के लिए एक समान फ़ंक्शन कैसे कार्यान्वित किया जा सकता है?अपने डेटा के लिए tensorflow के अगले_बैच को कैसे कार्यान्वित करें

sess = tf.InteractiveSession() 
tf.global_variables_initializer().run() 
Xtr, Ytr = loadData() 
for it in range(1000): 
    batch_x = Xtr.next_batch(100) 
    batch_y = Ytr.next_batch(100) 

उत्तर

10

लिंक आप पोस्ट का कहना है: "हमारे प्रशिक्षण सेट से एक सौ यादृच्छिक डेटा बिंदुओं के" बैच "हम एक मिल"। मेरे उदाहरण में मैं एक वैश्विक फ़ंक्शन का उपयोग करता हूं (आपके उदाहरण में एक विधि नहीं) इसलिए वाक्यविन्यास में एक अंतर होगा।

मेरे फ़ंक्शन में आपको नमूने की इच्छित संख्या और डेटा सरणी पारित करने की आवश्यकता होगी।

import numpy as np 

def next_batch(num, data, labels): 
    ''' 
    Return a total of `num` random samples and labels. 
    ''' 
    idx = np.arange(0 , len(data)) 
    np.random.shuffle(idx) 
    idx = idx[:num] 
    data_shuffle = [data[ i] for i in idx] 
    labels_shuffle = [labels[ i] for i in idx] 

    return np.asarray(data_shuffle), np.asarray(labels_shuffle) 

Xtr, Ytr = np.arange(0, 10), np.arange(0, 100).reshape(10, 10) 
print(Xtr) 
print(Ytr) 

Xtr, Ytr = next_batch(5, Xtr, Ytr) 
print('\n5 random samples') 
print(Xtr) 
print(Ytr) 

और एक डेमो रन:

यहाँ सही कोड है, जो यह सुनिश्चित करता है नमूने सही लेबल है

[0 1 2 3 4 5 6 7 8 9] 
[[ 0 1 2 3 4 5 6 7 8 9] 
[10 11 12 13 14 15 16 17 18 19] 
[20 21 22 23 24 25 26 27 28 29] 
[30 31 32 33 34 35 36 37 38 39] 
[40 41 42 43 44 45 46 47 48 49] 
[50 51 52 53 54 55 56 57 58 59] 
[60 61 62 63 64 65 66 67 68 69] 
[70 71 72 73 74 75 76 77 78 79] 
[80 81 82 83 84 85 86 87 88 89] 
[90 91 92 93 94 95 96 97 98 99]] 

5 random samples 
[9 1 5 6 7] 
[[90 91 92 93 94 95 96 97 98 99] 
[10 11 12 13 14 15 16 17 18 19] 
[50 51 52 53 54 55 56 57 58 59] 
[60 61 62 63 64 65 66 67 68 69] 
[70 71 72 73 74 75 76 77 78 79]] 
+2

मुझे इस पर विश्वास नहीं उपयोगकर्ता के रूप में काम करता है। इनपुट एक्सआरआर और आउटपुट वाईआरटी के बीच 1: 1 सहसंबंध है। प्रत्येक व्यक्तिगत रूप से यादृच्छिकरण हो रहा है। इसके बजाय, यादृच्छिक मानों का एक सेट चुना जाना चाहिए और फिर दोनों सेटों पर लागू किया जाना चाहिए। –

+1

धन्यवाद, मैंने अपनी पोस्ट अपडेट की है। – edo

+2

@edo 'id [i] idx में i के लिए] 'आप' डेटा [idx]' कर सकते हैं ताकि आप एनडारों से सूचियों तक वापस न जाएं और फिर से फिर से एनडारों तक न जाएं। –

6

क्रम शफ़ल करने के लिए और प्रत्येक मिनी बैच के नमूने में, राज्य है कि क्या वर्तमान युग के अंदर एक नमूना भी चुना जाना चाहिए। यहां एक कार्यान्वयन है जो उपर्युक्त उत्तर में डेटा का उपयोग करता है।

import numpy as np 

class Dataset: 

def __init__(self,data): 
    self._index_in_epoch = 0 
    self._epochs_completed = 0 
    self._data = data 
    self._num_examples = data.shape[0] 
    pass 


@property 
def data(self): 
    return self._data 

def next_batch(self,batch_size,shuffle = True): 
    start = self._index_in_epoch 
    if start == 0 and self._epochs_completed == 0: 
     idx = np.arange(0, self._num_examples) # get all possible indexes 
     np.random.shuffle(idx) # shuffle indexe 
     self._data = self.data[idx] # get list of `num` random samples 

    # go to the next batch 
    if start + batch_size > self._num_examples: 
     self._epochs_completed += 1 
     rest_num_examples = self._num_examples - start 
     data_rest_part = self.data[start:self._num_examples] 
     idx0 = np.arange(0, self._num_examples) # get all possible indexes 
     np.random.shuffle(idx0) # shuffle indexes 
     self._data = self.data[idx0] # get list of `num` random samples 

     start = 0 
     self._index_in_epoch = batch_size - rest_num_examples #avoid the case where the #sample != integar times of batch_size 
     end = self._index_in_epoch 
     data_new_part = self._data[start:end] 
     return np.concatenate((data_rest_part, data_new_part), axis=0) 
    else: 
     self._index_in_epoch += batch_size 
     end = self._index_in_epoch 
     return self._data[start:end] 

dataset = Dataset(np.arange(0, 10)) 
for i in range(10): 
    print(dataset.next_batch(5)) 

उत्पादन होता है:

[2 8 6 3 4] 
[1 5 9 0 7] 
[1 7 3 0 8] 
[2 6 5 9 4] 
[1 0 4 8 3] 
[7 6 2 9 5] 
[9 5 4 6 2] 
[0 1 8 7 3] 
[9 7 8 1 6] 
[3 5 2 4 0] 

पहले और दूसरे (3 और 4, ...) मिनी बैच एक पूरे युग के अनुरूप ..

1

मैं एनाकोंडा और Jupyter का उपयोग । Jupyter में यदि आप ?mnist चलाने आपको मिलेगा: File: c:\programdata\anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py Docstring: Datasets(train, validation, test)

फ़ोल्डर datesets आप mnist.py पाते हैं जाएगा जो next_batch सहित सभी तरीकों में शामिल हैं।

1

आप अपने tensorflow सत्र रन में आकार बेमेल त्रुटि प्राप्त करने के लिए तो ऊपर पहले समाधान में प्रदान की जाती समारोह के बजाय नीचे फ़ंक्शन का उपयोग नहीं करना चाहते हैं, तो (https://stackoverflow.com/a/40995666/7748451) -

def next_batch(num, data, labels): 

    ''' 
    Return a total of `num` random samples and labels. 
    ''' 
    idx = np.arange(0 , len(data)) 
    np.random.shuffle(idx) 
    idx = idx[:num] 
    data_shuffle = data[idx] 
    labels_shuffle = labels[idx] 
    labels_shuffle = np.asarray(labels_shuffle.values.reshape(len(labels_shuffle), 1)) 

    return data_shuffle, labels_shuffle 
संबंधित मुद्दे