InvalidArgumentError:indices [0,0] = -1不在[0,10]中

时间:2018-03-20 06:44:41

标签: python tensorflow deep-learning keras lstm

它与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)]]

代码:

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)

0 个答案:

没有答案