hyperas icon indicating copy to clipboard operation
hyperas copied to clipboard

Name 'LSTM' is not defined

Open JohnnyUrosevic opened this issue 6 years ago • 7 comments

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

JohnnyUrosevic avatar Oct 18 '18 19:10 JohnnyUrosevic

@JohnnyUrosevic move the two keras layer import statements to the top

maxpumperla avatar Oct 30 '18 07:10 maxpumperla

It's at both the top and in the method

JohnnyUrosevic avatar Oct 30 '18 07:10 JohnnyUrosevic

can you try to remove it from the method? If that doesn't work, please provide your full example for me to debug. thanks

maxpumperla avatar Oct 30 '18 08:10 maxpumperla

Removed it from the method and got the same error

Here's my notebook with my code

https://colab.research.google.com/drive/1lCKRpvbNJ3NNt56RpW4HaF-DU-2d6sir?authuser=1#scrollTo=1T37v8ww5Kbe

JohnnyUrosevic avatar Oct 31 '18 22:10 JohnnyUrosevic

i am getting a similar issue. Any updates?

rashikaanand avatar Feb 13 '19 11:02 rashikaanand

@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).

maxpumperla avatar Feb 14 '19 08:02 maxpumperla

one of the main reasons I advocate against notebooks btw.

maxpumperla avatar Feb 14 '19 08:02 maxpumperla