2
$\begingroup$

I am getting a syntax error while using hyperas and am not sure why.

My code:

import hyperas, hyperopt

from tensorflow.keras.layers import Dense, Dropout, Activation, Input
from tensorflow.keras import regularizers
from tensorflow.keras.models import Model

from hyperas import optim
from hyperas.distributions import choice, uniform

from hyperopt import Trials, STATUS_OK, tpe 
#trials - database to store all intermediate calculations / point evaluations of the search
#status_ok returns ok if search operation is successfully completed and returns fail if the fucntion is not defined.
#tpe - bayesian hyperparameter tuning.
def data_(X_train,X_val):
    return( X_train,X_train,X_val,X_val)

def model_(X_train, X_val):
    input_dim = X_train.shape[1]
    encoding_dim=[{{choice([6,8])}}]
    input_layer = Input(shape=(input_dim, ))
    encoder = Dense(encoding_dim, activation={{choice(['sigmoid','tanh'])}}, 
                    activity_regularizer=regularizers.l1({{choice([10e-5,10e-4,10e-3])}}))(input_layer)
    encoder = Dense(int(encoding_dim / 2), activation={{choice(['leaky_relu','relu'])}})(encoder)
    decoder = Dense(int(encoding_dim / 2), activation=({{choice(['sigmoid','tanh'])}}))(encoder)
    decoder = Dense(input_dim, activation=({{choice(['leaky_relu','relu'])}}))(decoder)
    autoencoder = Model(inputs=input_layer, outputs=decoder)
    nb_epoch={{choice([100,150,200])}}
    batch_size={{choice([512,1024])}}
    #adam=keras.optimizers.Adam(lr={{choice([10**-3,10**-2,10**-1])}})
    #rmsprop=keras.optimizers.RMSprop(lr={{choice([10**-3,10**-2,10**-1])}})
    #sgd=keras.optimizers.SGD(lr={{choice([10**-3,10**-2,10**-1])}})
    autoencoder.compile(optimizer='sgd', 
                    loss='mean_squared_error', 
                    metrics=['accuracy'])
    history = autoencoder.fit(X_train, X_train,
                    epochs=nb_epoch,
                    batch_size=batch_size,
                    shuffle=True,
                    validation_data=(X_val, X_val),
                    verbose=1).history
    print('acc',history['accuracy'])
    print('val_acc',history['val_accuracy'])
    return({'acc':history['accuracy'],'val_acc':history['val_accuracy'],'status':STATUS_OK,'model':history})

best_run,best_model=optim.minimize(model=model_,data=data_(X_train,X_val),algo=tpe.suggest,max_evals=5,trials=Trials(),notebook_name='autoencoder')

I got the following error:

Traceback (most recent call last):

      File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 3291, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)

      File "<ipython-input-148-fcddad2ebef6>", line 2, in <module>
        notebook_name='autoencoder')

      File "/usr/local/lib/python3.5/dist-packages/hyperas/optim.py", line 69, in minimize
        keep_temp=keep_temp)

      File "/usr/local/lib/python3.5/dist-packages/hyperas/optim.py", line 98, in base_minimizer
        model_str = get_hyperopt_model_string(model, data, functions, notebook_name, verbose, stack)

      File "/usr/local/lib/python3.5/dist-packages/hyperas/optim.py", line 189, in get_hyperopt_model_string
        imports = extract_imports(cleaned_source, verbose)

      File "/usr/local/lib/python3.5/dist-packages/hyperas/utils.py", line 40, in extract_imports
        tree = ast.parse(source)

      File "/usr/lib/python3.5/ast.py", line 35, in parse
        return compile(source, filename, mode, PyCF_ONLY_AST)

      File "<unknown>", line 520
        kmeans=
               ^
    SyntaxError: invalid syntax
$\endgroup$
2
  • 1
    $\begingroup$ It would be much easier to answer your question if you formatted your code in markdown as opposed to as pictures. $\endgroup$
    – Ethan
    Jun 25 '19 at 18:27
  • 1
    $\begingroup$ I edited it, thanks. $\endgroup$ Jun 25 '19 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.