Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Keras fit_生成器会产生错误形状的错误_Python_Python 3.x_Tensorflow_Keras_Keras Layer - Fatal编程技术网

Python 使用Keras fit_生成器会产生错误形状的错误

Python 使用Keras fit_生成器会产生错误形状的错误,python,python-3.x,tensorflow,keras,keras-layer,Python,Python 3.x,Tensorflow,Keras,Keras Layer,我在fit_生成器上遇到错误。我的生成器返回以下内容: yield(row.values, label) 例如,使用它: myg = generate_array() for i in myg: print((i[0].shape)) print(i) break (9008,) (array([0.116516, 0.22419 , 0.03373 , ..., 0. , 0. , 0. ]), 0) 但下面抛出了一个异常: mode

我在fit_生成器上遇到错误。我的生成器返回以下内容:

yield(row.values, label)
例如,使用它:

myg = generate_array()
for i in myg:
    print((i[0].shape))
    print(i)
    break

(9008,)
(array([0.116516, 0.22419 , 0.03373 , ..., 0.      , 0.      , 0.      ]), 0)
但下面抛出了一个异常:

model = Sequential()
model.add(Dense(84, activation='relu', input_dim=9008))

ValueError: Error when checking input: expected dense_1_input to have shape 
(9008,) but got array with shape (1,)

有什么想法吗?

正如Kota Mori所建议的:数据生成器需要提供一批数据,而不是一个样本。例如,见:

由于我需要随机梯度下降(批量大小为1),以下代码修复了该问题:

def generate_array():
   while True:
    X = np.empty((1, 9008))
    y = np.empty((1), dtype=int)
    # Some processing
    X[0] = row
    y[0] = label
    yield(X,y)

您应该制作一个显示问题的自包含示例,因为使用您显示的代码,我们无法猜测问题是什么。data generator需要提供一批数据,而不是一个样本。参见示例:@Kota Mori-谢谢!这就成功了。