我正在培训有关电子商务数据的lstm。在训练期间,我遇到以下错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: index [3,7] = -1不在[0,20000]中[[节点:embedding_1 / GatherV2 = GatherV2 [出租车= DT_INT32,丁迪克斯= DT_INT32,Tparams = DT_FLOAT,_class = [“ loc:@ training / Adam / gradients / embedding_1 / GatherV2_grad / Reshape”], _device =“ / job:localhost /副本0 / task:0 / device:CPU:0”](embedding_1 / embeddings / read, embedding_1 / Cast,lstm_1 / TensorArrayUnstack / range / start)]]
任何想法如何解决此问题? 我将模型定义如下:
model = Sequential()
model.add(Embedding(max_features, 128))
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
print('Train...')
model.fit(x_train, y_train, batch_size=batch_size, epochs=2, validation_data=(x_test, y_test))
score, acc = model.evaluate(x_test, y_test, batch_size=batch_size)
答案 0 :(得分:0)
我对代码进行了以下更改,并且有效:
我为此使用了python.cat.codes
for i in range(len(columns)):
df[columns[i]] = df[columns[i]].astype('category')
df[columns[i]+"_cat"] = df[columns[i]].cat.codes
astype将数据列转换为类别类型,从而解决了问题。