maxpumperla / hyperas

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

TypeError with 3d array #117

Open nbucklin opened 7 years ago

nbucklin commented 7 years ago

Hi @maxpumperla, thanks for all your help with this. I'm having an issue with using a 3d array for my X values. My training X values are in an array as float64 with the dimensions (926, 100, 1). These values are feed into a LSTM model. The 3d array is to fulfill the Keras format of n=926, timesteps=100, features=1. My model works when I do not use it with Hyperas. When I try to run it with Hyperas, I get this error:

File "/anaconda/lib/python3.6/inspect.py", line 636, in getfile
    'function, traceback, frame, or code object'.format(object))
TypeError: **(outputs entire array)**
is not a module, class, method, function, traceback, frame, or code object

Below is my model for reference:

def model(train_x,train_y,test_x,test_y):
    model = Sequential()

    model.add(LSTM(
        input_dim=1,
        output_dim=50,
        return_sequences=True))
    model.add(Dropout({{uniform(0,.5)}}))

    model.add(LSTM(
        100,
        return_sequences=False))
    model.add(Dropout({{uniform(0,.5)}}))

    model.add(Dense(
            output_dim=1))
    model.add(Activation("sigmoid"))

    start = time.time()
    model.compile(loss="binary_crossentropy", optimizer="rmsprop",metrics=['accuracy'],class_mode="binary")
    print("> Compilation Time : ", time.time() - start)

# Fitting model
    model.fit(
            train_x,
            train_y,
            batch_size={{choice([16,32,64,128,256,512])}},
            nb_epoch=5,
            validation_split=.05,
            show_accuracy=True)
    Score, Acc = model.evaluate(test_x,test_y)
    print('Accuracy', Acc)
    return {'loss':-Acc,'status':STATUS_OK,'model':model}

if __name__ == '__main__':
    best_run, best_model = optim.minimize(model=model,
                                          data=data(),
                                          algo=tpe.suggest,
                                          max_evals=5,
                                          trials=Trials())
    train_x, train_y, test_x, test_y = data()
    print("Evalutation of best performing model:")
    print(best_model.evaluate(test_x,test_y))
    print('Best performing hyper-paramaters:')
    print(best_run)

Any ideas? Thanks!

maxpumperla commented 7 years ago

@nbucklin hmm, can you maybe provide your full experiment as a gist along with dummy data for me to reproduce this?

nbucklin commented 7 years ago

Sure thing. Below is the gist link. The code I've provided generates dummy data and compiles it into X and Y training and test sets (X_train, X_test, y_train, y_test). Each of these are float64 3d arrays. For example, X_train is (1511,100,2) for 1511 samples, 100 timesteps, and two features. The model itself is a LSTM regression model that predicts two continuous variables. I am able to reproduce the same error I mentioned above using this code.

Please let me know if you need anything else.

https://gist.github.com/nbucklin/912505e7c4eca355472dcf53647e402f

sdzharkov commented 6 years ago

Hey, I've been receiving the same exact error. I attached my output in the gist below. Could it be the format of the data that is being passed it? It seems like it wants an ndarray, but is is something else?

https://gist.github.com/sdzharkov/069453c9092a8eae76551ecb6dbe67e8

Slyfest commented 6 years ago

Got same error. Any tips or workarounds so far?

loewenm commented 6 years ago

Have the same error as well... Just tagging-in in the hopes we find an answer...

The error is being thrown in the getfile() def in the inspect.py file.

def getfile(object):
    """Work out which source or compiled file an object was defined in."""
    if ismodule(object):
        if hasattr(object, '__file__'):
            return object.__file__
        raise TypeError('{!r} is a built-in module'.format(object))
    if isclass(object):
        if hasattr(object, '__module__'):
            object = sys.modules.get(object.__module__)
            if hasattr(object, '__file__'):
                return object.__file__
        raise TypeError('{!r} is a built-in class'.format(object))
    if ismethod(object):
        object = object.__func__
    if isfunction(object):
        object = object.__code__
    if istraceback(object):
        object = object.tb_frame
    if isframe(object):
        object = object.f_code
    if iscode(object):
        return object.co_filename
    raise TypeError('{!r} is not a module, class, method, '
                    'function, traceback, frame, or code object'.format(object))
loewenm commented 6 years ago

Hello all - I've figured out the issue here; at least from my standpoint...

I thought the wrapper was a bit more flexible in terms of its parameter inputs when you call optim.minimize. It's not flexible. I believe the majority of you are likely either trying to pass in your X_train, y_train, X_test, y_test, and/or the model directly. Simply put, you can't use this wrapper in this fashion. You must pass in a "module, class, method, function, traceback, frame, or code object", no exceptions.

So, when Max suggests using the following code:

    best_run, best_model = optim.minimize(model = create_model, 
                                          data = data, 
                                          algo = tpe.suggest, 
                                          max_evals = 5, 
                                          trials = Trials(), 
                                          notebook_name = None)

You literally need to pass in the parameters as such. You must make a data() function that returns the four data series and you must make a self-contained function that creates and returns the model i.e. create_model().

I suppose this is written explicitly in the instructions/setup and the error thrown also suggests this is the case as well... I'm a little ashamed that I didn't read it thoroughly enough. This feels like a Zoolander moment ("the files are in the computer"). lol!

Thank you Max and team for putting hyperas together. It's much less frustrating once you read the instructions... ;)

maxpumperla commented 6 years ago

@loewenm thanks for your time and efforts, nothing to be ashamed for. A lot of people seem to struggle with this, indicating that I did a poor job documenting the project.

If any of you have ideas how to make clear how exactly to use this project (wiki, more examples, more concise readme etc.), please let me know. Also, feel free to send PRs for that matter. For various reasons I have close to 0 time to do such rather non-critical tasks, which I hope is understandable.

Thanks!

kesarianubhav commented 6 years ago

@loewenm Corrct Dude !! I think this hyperas documentation needs some more explaination on how to pass arguments !!

JonnoFTW commented 5 years ago

Is there any way to have a parameterised data function?

Edit fixed this in #254

anasouzac commented 4 years ago

Hey, guys! I'm having the same problem... I'm running my code on a Jupyter Notebook. Is that a problem? Here's the error:


TypeError Traceback (most recent call last)

in 9 max_evals=5, 10 trials=Trials(), ---> 11 notebook_name='teste') 12 x_train, x_train, x_val, y_val = data() 13 print("Evalutation of best performing model:") ~\AppData\Local\Continuum\anaconda3\lib\site-packages\hyperas\optim.py in minimize(model, data, algo, max_evals, trials, functions, rseed, notebook_name, verbose, eval_space, return_space, keep_temp) 67 notebook_name=notebook_name, 68 verbose=verbose, ---> 69 keep_temp=keep_temp) 70 71 best_model = None ~\AppData\Local\Continuum\anaconda3\lib\site-packages\hyperas\optim.py in base_minimizer(model, data, functions, algo, max_evals, trials, rseed, full_model_string, notebook_name, verbose, stack, keep_temp) 96 model_str = full_model_string 97 else: ---> 98 model_str = get_hyperopt_model_string(model, data, functions, notebook_name, verbose, stack) 99 temp_file = './temp_model.py' 100 write_temp_files(model_str, temp_file) ~\AppData\Local\Continuum\anaconda3\lib\site-packages\hyperas\optim.py in get_hyperopt_model_string(model, data, functions, notebook_name, verbose, stack) 196 197 functions_string = retrieve_function_string(functions, verbose) --> 198 data_string = retrieve_data_string(data, verbose) 199 model = hyperopt_keras_model(model_string, parts, aug_parts, verbose) 200 ~\AppData\Local\Continuum\anaconda3\lib\site-packages\hyperas\optim.py in retrieve_data_string(data, verbose) 217 218 def retrieve_data_string(data, verbose=True): --> 219 data_string = inspect.getsource(data) 220 first_line = data_string.split("\n")[0] 221 indent_length = len(determine_indent(data_string)) ~\AppData\Local\Continuum\anaconda3\lib\inspect.py in getsource(object) 971 or code object. The source code is returned as a single string. An 972 OSError is raised if the source code cannot be retrieved.""" --> 973 lines, lnum = getsourcelines(object) 974 return ''.join(lines) 975 ~\AppData\Local\Continuum\anaconda3\lib\inspect.py in getsourcelines(object) 953 raised if the source code cannot be retrieved.""" 954 object = unwrap(object) --> 955 lines, lnum = findsource(object) 956 957 if istraceback(object): ~\AppData\Local\Continuum\anaconda3\lib\inspect.py in findsource(object) 766 is raised if the source code cannot be retrieved.""" 767 --> 768 file = getsourcefile(object) 769 if file: 770 # Invalidate cache if needed. ~\AppData\Local\Continuum\anaconda3\lib\inspect.py in getsourcefile(object) 682 Return None if no way can be identified to get the source. 683 """ --> 684 filename = getfile(object) 685 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:] 686 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:] ~\AppData\Local\Continuum\anaconda3\lib\inspect.py in getfile(object) 664 raise TypeError('module, class, method, function, traceback, frame, or ' 665 'code object was expected, got {}'.format( --> 666 type(object).__name__)) 667 668 def getmodulename(path): TypeError: module, class, method, function, traceback, frame, or code object was expected, got tuple ------------------------------------------------------------------------------------------------------------------------------------------- And my final code is: from hyperopt import Trials, STATUS_OK, tpe from hyperas import optim from hyperas.distributions import choice, uniform if __name__ == '__main__': best_run, best_model = optim.minimize(model=model, data=data(), algo=tpe.suggest, max_evals=5, trials=Trials(), notebook_name='teste') x_train, x_train, x_val, y_val = data() print("Evalutation of best performing model:") print(best_model.evaluate(X_test, Y_test)) print("Best performing model chosen hyper-parameters:") print(best_run)