0

I'm trying to do a sentiment analysis classifier based on the rotten tomatoes dataset. I'm getting this error:

AttributeError: 'RNN' object has no attribute '_output_tensor_cache'

Unfortunately, there isn't help anywhere. I have no idea where to even look.

import pandas as pd
from sklearn.model_selection import train_test_split
from tensorflow import keras
from keras import Model
from tensorflow.keras.layers import LSTM, Embedding, Dense
import os
os.chdir('/home/nicolas/Documents/datasets')

df = pd.read_csv('rotten_tomatoes_reviews.csv', nrows=50_000)

df = df.loc[df.Review.str.len() >= 3]

array = df.Review.values
target = df.Freshness.values

tokenizer = keras.preprocessing.text.Tokenizer(num_words=3_000)

tokenizer.fit_on_texts(array)
vector = tokenizer.texts_to_sequences(array)

padded = keras.preprocessing.sequence.pad_sequences(vector, maxlen=40)

X_train, X_test, y_train, y_test = train_test_split(padded, target, test_size=2e-1)


class RNN(Model):
    def __init__(self):
        super(RNN, self).__init__()
        self.rnn1 = LSTM(8, return_sequences=True, return_state=True)
        self.rnn2 = LSTM(8)
        self.emb1 = Embedding(input_dim=3_000, output_dim=50, input_length=40)
        self.flc1 = Dense(2)

    def __call__(self, inputs, training=None, mask=None):
        x = self.emb1(inputs)
        x = self.rnn1(x)
        x = self.rnn2(x)
        out = self.flc1(x)
        print(out.shape)
        return out


def main():
    model = RNN()

    model.compile(optimizer=keras.optimizers.Adam(0.001),
                  loss=keras.losses.BinaryCrossentropy(from_logits=True),
                  metrics=['accuracy'])

    model.fit(X_train, y_train, batch_size=16, epochs=10,
              validation_data=[X_test, y_test], verbose=1)

    scores = model.evaluate(X_test, y_test, batch_size=16, verbose=1)
    print("Final test loss and accuracy :", scores)


if __name__ == '__main__':
    main()

1 Answer 1

1

That's because you are mixing native Keras implementation and TensorFlow implementation of Keras (i.e. tf.keras):

from tensorflow import keras
from keras import Model     # Wrong! DON'T mix keras and tf.keras!
from tensorflow.keras.layers import LSTM, Embedding, Dense

You should never do that. Fix the import for Model class by using: from tensorflow.keras import Model

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.