2017-08-31 97 views
1

我在Keras中使用MLP進行一組表格數據的二進制分類。每個數據點有66個特徵,我有數百萬個數據點。 爲了在閱讀我的大型訓練集時提高記憶效率,我開始使用fit_generator。我把一個簡單的測試代碼在這裏:Keras Progress Bar在使用fit_generator時生成隨機批號

batch_size = 1 
input_dim = 66 
train_size = 18240 
train_steps_per_epoch = int(train_size/batch_size) 
model.fit_generator(generate_data_from_file('train.csv', feature_size=input_dim, batch_size=batch_size), 
          steps_per_epoch=train_steps_per_epoch, nb_epoch=epochs, verbose=1) 

這裏是我的發電機:

def generate_data_from_file(filename, feature_size, batch_size, usecols=None, delimiter=',', skiprows=0, dtype=np.float32): 
    while 1: 
     batch_counter = 0 
     if usecols is None: 
      usecols = range(1, feature_size+1) 
      x_batch = np.zeros([batch_size, feature_size]) 
      y_batch = np.zeros([batch_size, 1]) 
     else: 
      x_batch = np.zeros([batch_size, len(usecols)]) 
      y_batch = np.zeros([batch_size, 1]) 
     with open(filename, 'r') as train_file: 
      for line in train_file: 
        batch_counter += 1 
        line = line.rstrip().split(delimiter) 
        y = np.array([dtype(line[0])]) # Extracting labels from the first colomn 
        x = [dtype(line[k]) for k in usecols] # Extracting features 
        x = np.reshape(x, (-1, len(x))) 
        # stacking the data in batches 
        x_batch[batch_counter - 1] = x 
        y_batch[batch_counter - 1] = y 
        # Yield when having one batch ready. 
        if batch_counter == batch_size: 
         batch_counter = 0 
         yield (x_batch, y_batch) 

在我的訓練數據的第一colomn是標籤,其餘的功能。 如果我已經正確理解了fit_generator,我必須批量堆疊數據並生成它們。 培訓沒有問題,但進度條顯示隨機進度,令我困惑。這裏我使用batch_size = 1來簡化。結果是這樣的:

1/18240 [..............................] - ETA: 1089s - loss: 0.7444 - binary_accuracy: 0.0000e+00 
    38/18240 [..............................] - ETA: 52s - loss: 0.6888 - binary_accuracy: 0.4211  
    72/18240 [..............................] - ETA: 42s - loss: 0.6757 - binary_accuracy: 0.6806 
    110/18240 [..............................] - ETA: 36s - loss: 0.6355 - binary_accuracy: 0.7455 
    148/18240 [..............................] - ETA: 33s - loss: 0.5971 - binary_accuracy: 0.7500 
    185/18240 [..............................] - ETA: 32s - loss: 0.4890 - binary_accuracy: 0.8000 
    217/18240 [..............................] - ETA: 31s - loss: 0.4816 - binary_accuracy: 0.8295 
    249/18240 [..............................] - ETA: 31s - loss: 0.4513 - binary_accuracy: 0.8474 
    285/18240 [..............................] - ETA: 30s - loss: 0.4042 - binary_accuracy: 0.8561 
    315/18240 [..............................] - ETA: 30s - loss: 0.3957 - binary_accuracy: 0.8381 

我不知道爲什麼突然跳到從一萬八千二百四十零分之一到一萬八千二百四十零分之三十八再到一萬八千二百四十零分之七十二等。當我使用更大批量時,它具有相同的行爲。 我的發生器或者它的keras進度條行爲如何?

回答

0

正如你可以在this Keras看到實際檢查多少自去年progbar更新時間的流逝,它只有當它比秒一組量(這是默認設置here)更大的打印它。如果您的批量計算持續時間少於此保證金 - 則progbar不會更新,這就是爲什麼您的批次數不同。

+0

謝謝!這就解釋了爲什麼當我使用調試器時,我正確地看到它們(較慢的計算...)。 – user3428338