InvalidArgumentError:索引[0,0] = -1 不在 [0, 10) 中

2024-03-15

它与 MLP 一起进行二元分类效果很好。然而,在 LSTM 和卷积中,它给出了InvalidArgumentError.

我发现 y 需要重塑,我就这么做了。 我尝试了 x 的所有正值,并且模型运行良好。 那么负值有什么问题呢? 数据在代码中给出。有任何想法吗?

错误是这样的:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/usr/local/src/conda3_runtime/home/envs/DSX-Python35-Spark/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1326     try:
-> 1327       return fn(*args)
   1328     except errors.OpError as e:
....
File "/usr/local/src/conda3_runtime/home/envs/DSX-Python35-Spark/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): indices[0,0] = -1 is not in [0, 10)
     [[Node: embedding_11/Gather = Gather[Tindices=DT_INT32, Tparams=DT_FLOAT, validate_indices=true, _device="/job:localhost/replica:0/task:0/cpu:0"](embedding_11/embeddings/read, _arg_embedding_11_input_0_3)]]

Code:

from keras.models import Sequential
from keras.layers import Dense, Embedding
from keras.layers import LSTM

max_features=10
maxlen=4
embedding_size=4
lstm_output_size = 1
batch_size = 2
epochs = 2

x=([[ 0,  6,  1,  0],
       [-1,  0,  0,  1],
       [ 4, -4,  3,  0],
       [-1,  9,  1, -5],
       [-2,  0,  0,  0],
       [ 2,  0,  4,  1],
       [ 4, 10, -4, -4],
       [ 0, 10, -1, -4],
       [ 3, -2,  2,  1],
       [ 0, -1,  0,  0]])

y=([[0, 1, 0, 0, 1, 0, 0, 0, 0, 1]])

x, y = np.array(x), np.array(y)
y = y.reshape((10, 1))

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)


print('Build model...')
model = Sequential()
model.add(Embedding(max_features, 64, input_length = maxlen ))
model.add(LSTM(64, activation="sigmoid", return_sequences=True, recurrent_activation="hard_sigmoid"))
model.add(Dropout(0.25))
model.add(LSTM(lstm_output_size))
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=epochs,
          validation_data=(x_test, y_test))
score, acc = model.evaluate(x_test, y_test,
                            batch_size=batch_size)

print('Test score:', score)
print('Test accuracy:', acc)

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

InvalidArgumentError:索引[0,0] = -1 不在 [0, 10) 中 的相关文章

随机推荐