繁体   English   中英

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

[英]Keras Sequential Model: fit_generator with batch input

我一直在尝试使用稀疏矩阵训练序列Keras模型。 尽管我已在代码中指定了批处理大小,但正在使用batch_size = 1(即一次一行)进行训练。

这是代码:

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()

谁能帮助我为顺序模型生成批输入? 另外,当模型以batch_size = 32而不是batch_size = 1进行训练时,精度有多大?

谢谢

您得到的不是此生成器输出的1批大小。 让我在这里给你一个例子:

如果总共有1000个样本,并且将其分成10个批次,则使用fit_generator只能得到10个批次,这意味着10个样本中的每个都有10个批次。

因此,关键是fit_generator显示的是批次数量而不是样本总数。

暂无
暂无

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

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