Reputation: 51
I am about the train a modell with LSTM, but I am receiving this error message:
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.recurrent.LSTM object at 0x00000272F295E508>
I've seen that some other people had the same problem but none of their solutions worked for me. I need quick help because my deadline is almost over.
Please help!
Here my imports:
import pandas as pd
import os
from os import walk
from os.path import join
import numpy as np
import re
from numpy import array
from numpy import asarray
from numpy import zeros
import nltk
from nltk.stem import PorterStemmer
from nltk.stem import SnowballStemmer
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from tensorflow.keras import models
from keras.preprocessing.text import one_hot
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers.core import Activation, Dropout, Dense
from keras.layers import Flatten
from keras.layers import GlobalMaxPooling1D
from keras.layers.embeddings import Embedding
from sklearn.model_selection import train_test_split
from keras.preprocessing.text import Tokenizer
import matplotlib.pyplot as plt
And here my code:
model = Sequential()
embedding_layer = Embedding(vocab_size, 100, weights=[embedding_matrix], input_length=maxlen , trainable=False)
model.add(embedding_layer)
model.add(LSTM(128))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])
The error-message:
TypeError Traceback (most recent call last)
<ipython-input-501-6005bb036887> in <module>
2 embedding_layer = Embedding(vocab_size, 100, weights=[embedding_matrix], input_length=maxlen , trainable=False)
3 model.add(embedding_layer)
----> 4 model.add(LSTM(128))
5
6 model.add(Dense(1, activation='sigmoid'))
~\Anaconda3\lib\site-packages\keras\engine\sequential.py in add(self, layer)
131 raise TypeError('The added layer must be '
132 'an instance of class Layer. '
--> 133 'Found: ' + str(layer))
134 self.built = False
135 if not self._layers:
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.recurrent.LSTM object at 0x00000272F295E508>
Please help me!
Upvotes: 0
Views: 2549
Reputation: 91
similary problem is solved by following:
from tensorflow.keras.layers import LSTM
Upvotes: -1
Reputation: 1377
You are importing other layers
and your Sequential
model from keras directly, but from the error, it is understood that your LSTM
layer is imported from tensorflow.keras.layers
instead of keras.layers
. Import all layers from one, either keras or tensorflow and the error would be gone.
Upvotes: 2