繁体   English   中英

使用Keras fit_generator会产生错误的形状错误

[英]Using Keras fit_generator gives an error of wrong shape

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

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所建议的那样:数据生成器需要提供一批数据,而不是单个样本。 参见例如: https : //stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly

由于我想要随机梯度下降(批量大小为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)

暂无
暂无

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

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