maxpumperla / hyperas

Keras + Hyperopt: A very simple wrapper for convenient hyperparameter optimization
http://maxpumperla.com/hyperas/
MIT License
2.17k stars 316 forks source link

Name 'LSTM' is not defined #199

Open JohnnyUrosevic opened 5 years ago

JohnnyUrosevic commented 5 years ago

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

maxpumperla commented 5 years ago

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

JohnnyUrosevic commented 5 years ago

It's at both the top and in the method

maxpumperla commented 5 years ago

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

JohnnyUrosevic commented 5 years ago

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

rashikaanand commented 5 years ago

i am getting a similar issue. Any updates?

maxpumperla commented 5 years ago

@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 commented 5 years ago

one of the main reasons I advocate against notebooks btw.