gpt4 book ai didi

python - “tuple”对象不是迭代器

转载 作者:行者123 更新时间:2023-12-01 01:08:59 26 4
gpt4 key购买 nike

以下代码是我的词嵌入神经网络的入口点:

negative_ratio, n_positive = 1, 10
t = Trainer()
epoch = t.generate_batch(n_positive, negative_ratio=negative_ratio)
model = t.model()
h = model.fit_generator(
epoch,
epochs=15,
steps_per_epoch=negative_ratio,
verbose=2
)
上面的

epoch 是来自生成器的数据,它生成以下格式的(编码)训练数据:

 [[list([57, 41, 49, 50, 55, 19, 26, 38, 5, 14, 51])
list([50, 0, 0, 0, 0, 49, 0, 0, 0, 0, 26, 0, 0, 0, 0, 41, 55, 19, 38, 5, 51, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0, 57, 0, 0, 0, 0, 0, 0, 0])
1]
[list([35, 50, 12, 15, 21, 19, 26, 34, 13, 52])
list([50, 12, 0, 0, 0, 0, 0, 0, 0, 0, 26, 34, 0, 0, 0, 21, 19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 0, 0, 0, 0, 0, 0, 0, 0, 15, 13, 0, 0, 0, 0, 0, 0])
1]
[list([20, 28, 41, 56, 2, 1, 51, 23, 22])
list([28, 0, 0, 22, 0, 0, 0, 0, 0, 0, 2, 23, 0, 0, 0, 41, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 20, 56, 0, 0, 0, 0, 0, 0, 0])
1]
[list([30, 20, 9, 12, 15, 19, 34, 5, 52, 51, 22])
list([12, 0, 0, 22, 0, 0, 0, 9, 0, 0, 34, 0, 0, 0, 0, 19, 5, 51, 0, 0, 0, 0, 0, 0, 0, 0, 30, 0, 0, 52, 0, 0, 0, 0, 0, 0, 0, 20, 15, 0, 0, 0, 0, 0, 0, 0])
1]]

Keras 一直告诉我,生成器无效:

类型错误:“元组”对象不是迭代器

我做错了什么?

最佳答案

正如错误所示,您传递的是元组而不是生成器对象。 fit_generator() 需要一个生成器对象。在内部,它会在生成器对象上调用 next() 来获取一批数据。

如果我这样做,next() 在一个元组上,我会得到同样的错误:

>>> next((1,2))
Traceback (most recent call last):
File "<input>", line 1, in <module>
next((1,2))
TypeError: 'tuple' object is not an iterator

>>> sample_generator = ((1,2) for i in range(3))
>>> x,y = next(sample_generator)
>>> x,y
(1, 2)

关于python - “tuple”对象不是迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55034765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com