keras fit generator形状差异

时间:2018-10-11 15:48:44

标签: python tensorflow keras lstm

我正在尝试使用拟合生成器拟合LSTM,因为我的数据是稀疏矩阵的数组,因此我需要向网络提供非稀疏矩阵。

我的数据形状为(835027,) 每个实例都是大小为17321的稀疏矩阵。

这是我的代码

def fit_by_batch(X, y, batch_size):
    n_batches_for_epoch = X.shape[0]//batch_size
    for i in range(n_batches_for_epoch):
        index_batch = range(X.shape[0])[batch_size*i:batch_size*(i+1)]
        X_batch =X[index_batch][0].toarray()[0] #from sparse to array
        X_batch=X_batch.reshape(1,X_batch.shape[0],1 ) # to 3d array
        y_batch = y[index_batch,][0]
        yield(numpy.array(X_batch),y_batch)



model.fit_generator(generator=fit_by_batch(train_X, train_y, 10),steps_per_epoch=835027,nb_epoch=4)


model.add(LSTM(neurons, batch_input_shape=(10,17321,835027), stateful=True, return_sequences=True))

这给出了以下错误

ValueError: Error when checking input: expected lstm_1_input to have shape (17321, 835027) but got array with shape (17321, 1)

我猜是因为我的生成器返回了形状数组(17321,1) 我尝试将输入形状中的835027更改为1,并将批次也更改为1,它可以正常工作,但我想处理更大的批次

我在做什么错?我真的无法弄清楚我的错误

0 个答案:

没有答案
相关问题