【发布时间】:2019-04-25 16:17:41
【问题描述】:
我关注 this tutorial 为我的 Keras 模型创建了一个自定义生成器。这是一个 MWE,显示了我面临的问题:
import sys, keras
import numpy as np
import tensorflow as tf
import pandas as pd
from keras.models import Model
from keras.layers import Dense, Input
from keras.optimizers import Adam
from keras.losses import binary_crossentropy
class DataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, list_IDs, batch_size, shuffle=False):
'Initialization'
self.batch_size = batch_size
self.list_IDs = list_IDs
self.shuffle = shuffle
self.on_epoch_end()
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.list_IDs) / self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
# Generate indexes of the batch
#print('self.batch_size: ', self.batch_size)
print('index: ', index)
sys.exit()
def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.list_IDs))
print('self.indexes: ', self.indexes)
if self.shuffle == True:
np.random.shuffle(self.indexes)
def __data_generation(self, list_IDs_temp):
'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
X1 = np.empty((self.batch_size, 10), dtype=float)
X2 = np.empty((self.batch_size, 12), dtype=int)
#Generate data
for i, ID in enumerate(list_IDs_temp):
print('i is: ', i, 'ID is: ', ID)
#Preprocess this sample (omitted)
X1[i,] = np.repeat(1, X1.shape[1])
X2[i,] = np.repeat(2, X2.shape[1])
Y = X1[:,:-1]
return X1, X2, Y
if __name__=='__main__':
train_ids_to_use = list(np.arange(1, 321)) #1, 2, ...,320
valid_ids_to_use = list(np.arange(321, 481)) #321, 322, ..., 480
params = {'batch_size': 32}
train_generator = DataGenerator(train_ids_to_use, **params)
valid_generator = DataGenerator(valid_ids_to_use, **params)
#Build a toy model
input_1 = Input(shape=(3, 10))
input_2 = Input(shape=(3, 12))
y_input = Input(shape=(3, 10))
concat_1 = keras.layers.concatenate([input_1, input_2])
concat_2 = keras.layers.concatenate([concat_1, y_input])
dense_1 = Dense(10, activation='relu')(concat_2)
output_1 = Dense(10, activation='sigmoid')(dense_1)
model = Model([input_1, input_2, y_input], output_1)
print(model.summary())
#Compile and fit_generator
model.compile(optimizer=Adam(lr=0.001), loss=binary_crossentropy)
model.fit_generator(generator=train_generator, validation_data = valid_generator, epochs=2, verbose=2)
我不想打乱我的输入数据。我认为这已经得到处理,但是在我的代码中,当我在__get_item__ 中打印出index 时,我得到了随机数。我想要连续的数字。请注意,我正在尝试在 __getitem__ 中使用 sys.exit 来终止进程,以查看发生了什么。
我的问题:
为什么
index不连续?我该如何解决这个问题?当我在终端使用屏幕运行时,为什么它没有响应 Ctrl+C?
【问题讨论】:
-
我认为您可以通过将
shuffle=False传递给fit_generator方法来实现? -
您好,感谢您的回复。我在
__init__中将其作为默认设置,然后我测试了索引值是否被on_epoch_end中的if 语句打乱了。我发现if语句中的东西没有被执行,我认为这意味着shuffle确实是假的。 -
您希望批量索引连续生成,对吗?这就是
fit_generator的shuffle=False参数。你试过了吗? -
是的,请看上面的评论。
-
对不起,我不明白。在我的机器上,当我在
fit_generator调用中设置shuffle=False(不是在__init__方法中)时,我会得到连续的索引。