Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optmizing CNN - TypeError: module, class, method, function, traceback, frame, or code object was expected, got tuple #280

Open
anasouzac opened this issue Jul 14, 2020 · 2 comments

Comments

@anasouzac
Copy link

anasouzac commented Jul 14, 2020

Hey, guys! I'm having the same problem described in other issues, but I am training a 2D CNN. I'm running my code on a Jupyter Notebook. Is that a problem?

Here's the error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-79a7f74171c2> in <module>
     10                                           max_evals=5,
     11                                           trials=Trials(),
---> 12                                           notebook_name='teste_hyperas')
     13     x_train, x_train, x_val, y_val = data()
     14     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 code is:


def data():
    
    import pandas as pd
    import numpy as np
    from sklearn import preprocessing
    from keras.layers import Conv2D, Dense, Dropout, MaxPooling2D, Flatten
    from keras.models import Sequential
    from keras.optimizers import SGD
    from keras.callbacks import EarlyStopping
    
    x_windows_train = np.load('D:\\TEP - Python\\x_windows_09.npy')
    y_windows_train = np.load('D:\\TEP - Python\\y_targets_09.npy')
    y_windows_train_ohe = np.load('D:\\TEP - Python\\y_windows_ohe_09.npy')

    x_windows_test = np.load('D:\\TEP - Python\\x_windows_13.npy')
    y_windows_test = np.load('D:\\TEP - Python\\y_targets_13.npy')
    y_windows_test_ohe = np.load('D:\\TEP - Python\\y_windows_ohe_13.npy')

    nlinhas = x_windows_train.shape[1]
    ncolunas = x_windows_train.shape[1]
    
    print("Data loaded!")
    
    split_size = 0.80

    train_size = int(len(x_windows_train)*split_size)
    x_train = x_windows_train[0:train_size, :]
    y_train = y_windows_train_ohe[0:train_size, :]

    x_val = x_windows_train[train_size:,:]
    y_val = y_windows_train_ohe[train_size:,:]

    x_test = x_windows_test
    y_test = y_windows_test_ohe

    print("TRAIN")
    print("X: ", np.shape(x_train))
    print("Y: ", np.shape(y_train))
    print("Status:", np.unique(y_windows_train[0:train_size]))

    print("\nVALIDATION")
    print("X: ", np.shape(x_val))
    print("Y: ", np.shape(y_val))
    print("Status:", np.unique(y_windows_train[train_size:]))

    print("\nTEST")
    print("X: ", np.shape(x_test))
    print("Y: ", np.shape(y_test))
    print("Status:", np.unique(y_windows_test))
    
    return x_train, y_train, x_val, y_val

def model(x_train, y_train, x_val, y_val):
    
    model = Sequential()

    model.add(Conv2D(filters={{choice([10, 20, 30])}}, 
                     kernel_size={{choice([(3,3), (5,5)])}}, 
                     strides=(1,1), 
                     padding={{choice(['valid', 'same'])}}, 
                     data_format='channels_last', activation='relu', use_bias=True, input_shape=(nlinhas,ncolunas,1))) 
    
    model.add(MaxPooling2D(pool_size=(2,2), strides=None, padding="valid", data_format=None))
    
    model.add(Flatten()) 
    
    model.add(Dense(units=21, activation='softmax', use_bias=True, kernel_regularizer=None,
                bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None))

    sgd = SGD(lr={{choice([0.1, 0.2, 0.3])}})
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['acc'])
    es = EarlyStopping(monitor='loss', mode='auto', verbose=0, patience=5)

    history = model.fit(x_train, y_train, epochs=700, batch_size={{choice([1000, 5000, 10000])}}, verbose=1, callbacks=[es],
                       validation_data=(x_val, y_val))
    history_list.append(history)

    scores = model.evaluate(x_val, y_val, verbose=0)
    scores_list.append(scores)

    return {'loss': -acc, 'status': STATUS_OK, 'model': model}

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)

@ncuxomun
Copy link

Hi, can you try running in a script?
I tried running in a notebook before, it gave me some errors. Just add a line to save the best model, and reload to test your data.
Hope that helps!

@anasouzac
Copy link
Author

Hi, can you try running in a script?
I tried running in a notebook before, it gave me some errors. Just add a line to save the best model, and reload to test your data.
Hope that helps!

Hi, @ncuxomun! Tks for the tip, but it also didn't work... I just updated the issue to include my entire code. I wonder if the problem is the input data shape, which is a 4D tensor (because i'm training a 2dConv)...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants