Search code examples
pythontensorflowkeraslstmnamed-entity-recognition

ValueError: setting an array element with a sequence. On Keras model.fit


I'm getting "ValueError: setting an array element with a sequence" on keras model.fit

model.fit(X_tr, np.array(y_tr), batch_size=32, epochs=5, validation_split=0.1, verbose=1)



X_tr
Out[22]:
array([[4256, 1244, 4475, ..., 8766, 8766, 8766],
       [5443, 3401, 4709, ..., 8766, 8766, 8766],
       [3829,  543,  681, ..., 8766, 8766, 8766],
       ...,
       [2185, 7510, 8004, ..., 8766, 8766, 8766],
       [7562, 5842, 4742, ..., 8766, 8766, 8766],
       [2449, 6217, 2310, ..., 8766, 8766, 8766]], dtype=int32)

X_tr.shape
(2699, 75)

np.array(y_tr)
Out[37]:
array([array([[0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    0.],
   [0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
    0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.,
    0.],
   ...,
   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.,
    0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.,
    0.],
   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.,
    0.]], dtype=float32)], dtype=object)

np.array(y_tr).shape
(2699,)

I am so confused, anyone able to help me out? Thanks in advance!

In case you need code: https://github.com/sunsuntianyi/question/blob/master/LSTM.ipynb


Solution

  • The error definitely originates from you passing in an object array as your predictive variable. Your y_tr should be of shape (2699,17) as far as I can tell from your snippet. Maybe some of your rows in y_tr is not 17 long or maybe you have specifically used an object array to generate the data. If the latter, you can try to convert it back like this:

    y_tr = np.asarray([np.asarray(row, dtype=float) for row in y_tr], dtype=float)
    

    Replace float with whatever type suits your needs. This should give an error if the rows are of different size as well.