question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Name 'LSTM' is not defined

See original GitHub issue

I’m making a simple LSTM model, but I’m having issues with the model function

Top of code


import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from tensorflow import keras

from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform

from keras.layers.core import Dense, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
def create_model(x_train, y_train, x_test, y_test):
    from keras.layers.core import Dense, Dropout
    from keras.layers.recurrent import LSTM
    
    model = Sequential()
    model.add(LSTM( {{choice([512, 1024, 2048])}} , input_shape=(14,1)))
    
    if {{choice(["dropout", "none"])}} == "dropout":
        model.add(Dropout({{uniform(0, 1)}}))
    
    model.add(Dense(1))
    model.compile(optimizer={{choice(['adam', 'adagrad', 'rmsprop'])}},loss='mse', metrics=['rmse'])
    x_train = x_train.reshape((x_train.shape[0],x_train.shape[1],1))
    x_test = x_test.reshape((x_test.shape[0],x_test.shape[1],1))
    
    model.fit(X_train,y_train,epochs=20,validation_data=(X_test,y_test),shuffle=False)
    score, err = model.evaluate(x_test, y_test, verbose=0)
    print('Test accuracy:', err)
    return {'loss': err, 'status': STATUS_OK, 'model': model}

But I keep getting the error: “Name ‘LSTM’ is not defined” When I try to run the optimizer. I looked at the LSTM examples and I do not seem to be doing much different from them, besides defining an input_shape. But if the input_shape is the problem, how would I be able to run my LSTM with this input? Thanks

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
maxpumperlacommented, Feb 14, 2019

one of the main reasons I advocate against notebooks btw.

0reactions
maxpumperlacommented, Feb 14, 2019

@JohnnyUrosevic so I see from your notebook cell that you import this

from keras.layers.core import Dense, Dropout
from keras.layers.recurrent import LSTM

but the hyperas output file says

try:
    from keras.layers.core import Dense, Dropout, Activation
except:
    pass

this does not align at all. Hyperas can’t magically add Activation at the end for you. This means your notebook cell execution order is off (most likely).

Read more comments on GitHub >

github_iconTop Results From Across the Web

NameError: name 'LSTM' is not defined · Issue #37137 - GitHub
It should be from tensorflow.keras.layers import LSTM. Thank you. 2
Read more >
I am using keras and I get an error message saying "No ...
python - I am using keras and I get an error message saying "No module named 'lstm'" - Stack Overflow. Stack Overflow for...
Read more >
Keras | NameError: name 'LSTM' is not defined_一只殿鹿的博客
问题描述:使用keras搭建神经网络时报错: model = tf.keras.Sequential([ LSTM(80, return_sequences=True), Dropout(0.2), LSTM(100), Dropout(0.2) ...
Read more >
LSTM seq2seq with keras - Kaggle
input/fra2eng-dataset/fra-eng")) FileNotFoundError: [Errno 2] No such file or directory: '. ... NameError: name 'num_encoder_tokens' is not defined.
Read more >
tf.keras.layers.LSTM | TensorFlow v2.11.0
When the input numpy array is not compatible with the RNN layer state, either size wise or dtype wise. Was this helpful?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found