# TFIDF vector into LSTM model

## Issue

I am trying to feed my TFIDF vector into an LSTM model.
`TfidfVectorizer(ngram_range=(1,2), use_idf=True, analyzer='word', max_features = 5000)`

Here’s the Vector shapes
`train_vector.shape` = (22895, 5000)
`test_vector.shape` = (5724, 5000)

I am defining a model like below:

``````model = models.Sequential()

``````

Other Parameters

``````model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
``````
``````model.fit(train_vector, y_train, validation_data=(test_vector, y_test), epochs=10, batch_size=1024)
``````

Tensorflow is being used here.

I am getting this error

ValueError: Input 0 of layer sequential_2 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 5000)

I am trying to reshape the arrays, but the errors are still showing. I know LSTM needs 3D array. So how can I shape my arrays in a way that can be fed into LSTM???

## Solution

To add a new dimension to your training and test data, you can try:

``````train_vector = train_vector[..., None] # or tf.newaxis instead of None
test_vector = test_vector[..., None] # or tf.newaxis instead of None
``````

or

``````train_vector = tf.expand_dims(train_vector, axis=-1)
test_vector = tf.expand_dims(test_vector, axis=-1)
``````

Also, note that if you have one node in your output layer and you are using a sigmoid activation function, you usually combine it with the `binary_crossentropy` loss function instead of `sparse_categorical_crossentropy`, which is usually used for more than 2 classes.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7456106,"math_prob":0.8271356,"size":1820,"snap":"2022-27-2022-33","text_gpt3_token_len":461,"char_repetition_ratio":0.12665199,"word_repetition_ratio":0.025974026,"special_character_ratio":0.2725275,"punctuation_ratio":0.20891365,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9985882,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-18T02:10:19Z\",\"WARC-Record-ID\":\"<urn:uuid:b3ca5c98-30de-4c9d-90a3-f5f4468d5ca1>\",\"Content-Length\":\"42267\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5fa33a40-bfc9-499d-b6b9-4dca4c396f9d>\",\"WARC-Concurrent-To\":\"<urn:uuid:e08f2fff-d3ff-4803-a322-3b762a10bd8c>\",\"WARC-IP-Address\":\"104.21.9.154\",\"WARC-Target-URI\":\"https://errorsfixing.com/tfidf-vector-into-lstm-model/\",\"WARC-Payload-Digest\":\"sha1:QBBSFNBWYHOGX75ZCBAK7GHXIFMOGD3N\",\"WARC-Block-Digest\":\"sha1:WVF7QNHP6O7BF4TDATNPDUFIDTK3OATM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882573145.32_warc_CC-MAIN-20220818003501-20220818033501-00648.warc.gz\"}"}