ValueError: `y` argument is not supported when using `keras.utils.Sequence` as input.
时间: 2024-01-25 14:03:25 浏览: 57
这个错误通常出现在使用 `keras.utils.Sequence` 作为输入时,同时将 `y` 作为参数传递给 `fit` 函数。这个错误的原因是 `keras.utils.Sequence` 生成器只返回 `X` 值,而不返回 `y` 值。因此,你可以尝试将 `y` 值添加到 `keras.utils.Sequence` 的子类中,使其在生成器中返回。
以下是一个示例代码,你可以参考一下:
```python
from keras.utils import Sequence
class MyGenerator(Sequence):
def __init__(self, x_set, y_set, batch_size):
self.x, self.y = x_set, y_set
self.batch_size = batch_size
def __len__(self):
return int(np.ceil(len(self.x) / float(self.batch_size)))
def __getitem__(self, idx):
batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]
return batch_x, batch_y
```
然后,你可以使用 `MyGenerator` 类作为输入来训练你的模型,如下所示:
```python
train_generator = MyGenerator(x_train, y_train, batch_size)
model.fit(train_generator, epochs=epochs, validation_data=(x_val, y_val))
```
希望这可以解决你的问题。