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

invalid syntax from unknown file #98

Open simahgh opened 7 years ago

simahgh commented 7 years ago

Hi, I have just installed the package but when I am trying to run mycode in jupyter python, it I gives an error regarding optim.minimize saying invalid argument. I've added the notebook name as well, but still it is saying SyntaxError: invalid syntax from unknown file. I am wondering if you could help me. Thanks

`

def model(train_X, test_X, train_y, test_y):
    timesteps = 32
    input_dim = 124
    num_classes = 6 
    model = Sequential()
    model.add(LSTM({{choice([256, 120, 80])}}, input_shape=(timesteps, input_dim), return_sequences=True,  activation='tanh'))
    model.add(Dropout({{uniform(0, 1)}}))
    model.add(LSTM(96,activation='tanh',dropout=0.2))
    model.add(Dense(num_classes, activation="softmax"))
    model.compile(loss='categorical_crossentropy', optimizer={{choice(['rmsprop', 'adam', 'sgd'])}})
    model.fit(train_X, train_y,batch_size={{choice([64, 128])}},nb_epoch={{choice([10, 20])}},show_accuracy=True,verbose=0,validation_data=(X_test, Y_test))
    score, acc = model.evaluate(test_X, test_y, show_accuracy=True, verbose=0)
    print('Test accuracy:', acc)
    return {'loss': -acc, 'status': STATUS_OK, 'model': model}
if __name__ == '__main__':

    train_X, test_X, train_y, test_y = data()
    best_run, best_model = optim.minimize(model=model,data=data,algo=tpe.suggest,max_evals=5,trials=Trials(),notebook_name='simple_notebook2')
    print("Evalutation of best performing model:")
    print(best_model.evaluate(test_X, test_y))
`

File "", line 33 mat = loadmat('C:/Users/simah/Downloads/Kaneshiro_etAl_objectCategoryEEG/Kaneshiro_etAl_objectCategoryEEG/S1.mat') ^ SyntaxError: invalid syntax

maxpumperla commented 7 years ago

Can you try to remove the comment in the very line you load the data?

simahgh commented 7 years ago

Thanks for your reply. I did but it still gives some syntax error like: SyntaxError: from future imports must occur at the beginning of the file

I appreciate your help.

maxpumperla commented 7 years ago

yeah, I think in notebooks you can call imports at the beginning of each cell, but

from __future__ import print_function

etc. have to be at the beginning of the file in a script, which hyperas translates to. Can you try to move the resp. statements up? If that does not work, the output your command line produces of the hyperas model would be very helpful to see.

simahgh commented 7 years ago

Thanks a lot for your reply. I tried to run the code as you said (put from future import print_function first line of first code)but still get the same error:

Imports:

coding=utf-8

from future import print_function

try: from hyperopt import Trials, STATUS_OK, tpe except: pass

try: from hyperas import optim except: pass

try: from hyperas.distributions import choice, uniform, conditional except: pass

try: import scipy.io except: pass

try: import numpy as np except: pass

try: from scipy.io import loadmat except: pass

try: import tensorflow as tf except: pass

try: from sklearn.model_selection import train_test_split except: pass

try: from sklearn import preprocessing except: pass

try: from keras.models import Sequential except: pass

try: from keras.layers import Dense except: pass

try: from keras.layers import LSTM except: pass

try: from keras.layers.core import Dense, Dropout, Activation except: pass

try: from keras.layers.embeddings import Embedding except: pass

try: from keras.preprocessing import sequence except: pass

try: from sklearn import preprocessing except: pass

try: from sklearn.preprocessing import OneHotEncoder except: pass

try: from sklearn.model_selection import train_test_split except: pass

try: import os.path as op except: pass

from future import print_function

try: from hyperopt import Trials, STATUS_OK, tpe except: pass

try: from hyperas import optim except: pass

try: from hyperas.distributions import choice, uniform, conditional except: pass

from future import print_function

try: from hyperopt import Trials, STATUS_OK, tpe except: pass

try: from hyperas import optim except: pass

try: from hyperas.distributions import choice, uniform, conditional except: pass

Hyperas search space:

def get_space(): return { 'LSTM': hp.choice('LSTM', [256, 120, 80]), 'Dropout': hp.uniform('Dropout', 0, 1), 'optimizer': hp.choice('optimizer', ['rmsprop', 'adam', 'sgd']), 'batch_size': hp.choice('batch_size', [64, 128]), 'nb_epoch': hp.choice('nb_epoch', [10, 20]), }

Data 1: 2: 3: train_X, test_X, train_y, test_y=train_test_split(Xx,Y, test_size=0.1) 4: 5: 6: Resulting replaced keras model:

1: def keras_fmin_fnct(space): 2: 3: 4:
5: timesteps = 32 6: input_dim = 124 7: num_classes = 6 8: model = Sequential() 9: model.add(LSTM(space['LSTM'], input_shape=(timesteps, input_dim), return_sequences=True, activation='tanh')) 10: model.add(Dropout(space['Dropout'])) 11: model.add(LSTM(96,activation='tanh',dropout=0.2)) 12: model.add(Dense(num_classes, activation="softmax")) 13: model.compile(loss='categorical_crossentropy', optimizer=space['optimizer']) 14: model.fit(train_X, train_y,batch_size=space['batch_size'],nb_epoch=space['nb_epoch'],show_accuracy=True,verbose=0,validation_data=(X_test, Y_test)) 15: score, acc = model.evaluate(test_X, test_y, show_accuracy=True, verbose=0) 16: print('Test accuracy:', acc) 17: return {'loss': -acc, 'status': STATUS_OK, 'model': model} 18: Unexpected error: <class 'SyntaxError'>

File "C:\Users\simah\temp_model.py", line 100 from future import print_function ^ SyntaxError: from future imports must occur at the beginning of the file

Thanks a lot for your help

maxpumperla commented 7 years ago

hey @simahgh, it seems the future import is correctly put at the beginning of the file, but then we see it again imported later in the file. Can you please share your script, ideally as a gist?

simahgh commented 7 years ago

https://gist.github.com/simahgh/15da34b92dffec3d614a5145e0f5fcad Thanks a lot:)

maxpumperla commented 7 years ago

@simahgh it seems hyperas parses the imports in reversed order. How exactly do you invoke python? Can you run examples/simple.py? It has the same import structure as yours and it passes our unit tests.

It's recommended to use python or ipython from command line.

fkolokathi commented 6 years ago

Did you find a solution for this error.I use Jupyter and I have the same error.