gpt4 book ai didi

python - 错误-使用Keras的多分类神经网络

转载 作者:行者123 更新时间:2023-12-02 10:43:15 26 4
gpt4 key购买 nike

我从我的NN代码中收到一个相当烦人的错误,并希望对Keras如何工作有更好了解的人可以向我解释为什么我会收到该错误。感谢您的帮助!
错误:

AttributeError: 'DirectoryIterator' object has no attribute 'ndim'

错误来自:
    Traceback (most recent call last):
File "C:\Users\Cameron\Desktop\AI\CubeFieldNN_Train -fix.py", line 80, in <module>
validation_steps = (validation_samples / batch_size))

码:
NN.fit(
train_set, train_labels,
batch_size = batch_size,
epochs = epochs,
validation_data = (validation_set, validation_labels),
validation_steps = (validation_samples / batch_size))

完整代码: https://pastebin.com/V1YwJW3X

完全错误:
    Traceback (most recent call last):
File "C:\Users\Cameron\Desktop\AI\CubeFieldNN_Train -fix.py", line 80, in <module>
validation_steps = (validation_samples / batch_size))
File "C:\Python\lib\site-packages\keras\models.py", line 1002, in fit
validation_steps=validation_steps)
File "C:\Python\lib\site-packages\keras\engine\training.py", line 1630, in fit
batch_size=batch_size)
File "C:\Python\lib\site-packages\keras\engine\training.py", line 1476, in _standardize_user_data
exception_prefix='input')
File "C:\Python\lib\site-packages\keras\engine\training.py", line 76, in _standardize_input_data
data = [np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data]
File "C:\Python\lib\site-packages\keras\engine\training.py", line 76, in <listcomp>
data = [np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data]
AttributeError: 'DirectoryIterator' object has no attribute 'ndim'

最佳答案

确实没有必要从previous questionfit过渡到fit_generator
flow_from_directory 返回一个生成器类型的对象,该对象返回数据和标签的元组。对于validation_set同样如此。还要注意,如果指定validation_steps,还必须指定steps_per_epoch。因此,您可以使用:

NN.fit_generator(train_set,
steps_per_epoch=steps_per_epoch,
epochs=epochs,
validation_data=validation_set,
validation_steps=validation_steps)

另外,您可以一次加载所有图像,然后将其与标签一起传递给 NN.fit()函数。

关于python - 错误-使用Keras的多分类神经网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50186035/

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