简体   繁体   English

Keras顺序模型:具有批输入的fit_generator

[英]Keras Sequential Model: fit_generator with batch input

I have been trying to train a Sequential Keras model using a sparse matrix. 我一直在尝试使用稀疏矩阵训练序列Keras模型。 Although I have specified the batch size in the code, it is being trained at batch_size = 1 (ie one row at a time). 尽管我已在代码中指定了批处理大小,但正在使用batch_size = 1(即一次一行)进行训练。

Here's the code: 这是代码:

def batch_generator(X_toGen, y_toGen = None, batch_size = 32):
    counter = 0
    sample_index = np.arange(X_toGen.shape[0])
    np.random.shuffle(sample_index)
    while True:
        batch_index = sample_index[batch_size*counter:min(batch_size*(counter+1), X_toGen.shape[0])]
        counter += 1
        X_batch = X_toGen[batch_index,:]
        if y_toGen is not None:
            y_batch = y_toGen[batch_index]
            yield X_batch.toarray(), y_batch
        else:
            yield X_batch.toarray()

Can anyone please help me in generating a batch input for the Sequential model? 谁能帮助我为顺序模型生成批输入? Also, how different would the accuracy be when the model is being trained at batch_size = 32, rather than batch_size = 1? 另外,当模型以batch_size = 32而不是batch_size = 1进行训练时,精度有多大?

Thanks 谢谢

What you are getting is not 1 batch size output from this generator. 您得到的不是此生成器输出的1批大小。 Let me give you an example here: 让我在这里给你一个例子:

If you have total 1000 samples and you are dividing this in batches 10. Then using fit_generator you will get only 10 batches which means you have 10 batches each of 10 samples. 如果总共有1000个样本,并且将其分成10个批次,则使用fit_generator只能得到10个批次,这意味着10个样本中的每个都有10个批次。

So, the crux is fit_generator shows number of batches instead of total number of samples. 因此,关键是fit_generator显示的是批次数量而不是样本总数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM