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

from __future__ imports must occur at the beginning of the file #157

Open simonljus opened 6 years ago

simonljus commented 6 years ago

I am using python 2.7 and when importing other packages from future than print_function, the temp_model.py file wraps the imports in a try and except block. The first lines in temp_model.py:

#coding=utf-8

from __future__ import print_function

try:
    from __future__ import unicode_literals
except:
    pass
SimSmith commented 6 years ago

I used Python 3.6 and got the same problem and I believe the problem had something to do with Jupyter Notebook. Did you use a Notebook as well?

simonljus commented 6 years ago

The problem arose when I imported future packages to the same file as where the Hyperas package is imported. I solved it by moving all functions that were dependent on the future packages to a different file and imported them. Did you have any future packages in your code when using Python 3.6?

AllardJM commented 6 years ago

Can you explain this fix a bit more @simonljus ?

simonljus commented 6 years ago

I made one separate python file for Hyperas. The Hyperas file contained three functions:

By doing it this way, no future packages were needed to be imported into the Hyperas file.

Before: file_a.py

from __future__ import unicode_literals
import hyperas....
def build_my_model(data):
    ....
def train_my_model(data):
    ....
def evaluate_my_model(data):
     ....
def some_function_that_needed_unicode_literals():
    ....
def read_training_sets():
    some_function_that_needed_unicode_literals()
    ....

def my_data_function():
    data = readTrainingSets()
    .....
    return data

def my_model_function(data):
    hyperas_parameters = ...
    build_my_model(hyper_parameters)
    train_my_model(data)
    loss =  evaluate_my_model(data)
    ....
    return {"loss"=loss.....}
def myHyperasFunction():
    optim.minimize(model=my_model_function,data=my_data_function....)
    ....

after: Made call_hyperas() that calls the Hyperas function that will start the hyperoptimization Moved all hyperas related functions to file_hyperas.py file_a.py

from __future__ import unicode_literals
def build_my_model(data):
     ....
def train_my_model(data):
    ....
def evaluate_my_model(data):
     ....
def some_function_that_needed_unicode_literals():
    .....

def read_training_sets():
    some_function_that_needed_unicode_literals()
def call_hyperas():
    from file_hyperas import my_hyperas_function() #the files are dependent on each other, made the import here as a solutioon
    my_hyperas_function()

file_hyperas.py

from file_a.py import read_training_sets, build_my_model train_my_model, evaluate_my_model
import hyperas....

def my_data_function():
    data = read_training_sets()
    .....
    return data
def my_model_function(data)
    hyperas_parameters = ....
    build_my_model(hyper_parameters)
    train_my_model(data)
    loss =  evaluate_my_model(data)
    ....
   return {"loss"=loss.....}
def my_hyperas_function():
    optim.minimize(model=my_model_function,data=my_data_function....)
    ....
AllardJM commented 6 years ago

Thanks! I will review this and see if it works for me.

zhangyi6 commented 6 years ago

I got the same problem, did you gays solve this with the seperated python file?

mbfair commented 6 years ago

I'm also having this issue

simonljus commented 6 years ago

Yes, I solved it by moving all Hyperas code into one separate file. This is just an example how it could look like. If it is unclear, specify what the problem is or how your code looks like :)

In the Hyperas file, the Hyperas functions do not need any extra future packages to work

#hyperasfile.py 
from my_python_file_with_an_important function import thisImportantFunction
from conflicting_file import anotherFunctionIReallyNeed
'''
write all the hyperas functions here 
'''
def myModelFunction():
    anotherFunctionIReallyNeed
'''
see my previous comment in the thread
'''
def myDatafunction():
    thisImportantfunction()
'''
se my previous comment in the thread
'''
def myHyperasFunction(max_evals=3,another_argument=None):
    optim.minimize(model=myModelFunction,data=myDataFunction,max_evals=max_evals)

In the other files you can import as much __future__ you want :) In the conflicting file which calls the functions in hyperas_file just do like this…

#conflicting_file.py
from __future__ import something_important_from_the_future 
def anotherFunctionIReallyNeed():
    something_important_from_the_future()

def hereICallHyperas():
    from hyperasfile import my_hyperas_function #this is the sneaky solution when the files are dependent on each other
    myHyperasFunction(max_evals=10)
mbfair commented 6 years ago

Ok, I have implemented this and am avoiding the future problem now. Thank you very much!

RayDean commented 5 years ago

I got this problem too, I use Ipython notebook to run the demo file simple_notebook.ipynb, and it runs OK , But I copy the whole codes from simple_notebook.ipynb to a new notebook file named "my_file.ipynb", and runs Error like this, It seems Hyperas create a temp_model.py file from "my_file.ipynb", and set the beginning of temp_model.py to "#coding=utf-8", and the script claims from future imports must occur at the beginning of the file, so the error happens. Even there is no "from future imports " in "my_file.ipynb", the temp_model.py has "from future imports ... " sentences..

uncodera commented 5 years ago

i solved this error using small trick but not good idea

open the hyperas/optim.py or copy optim.py to new filename optim_new.py

when ever you call the optim.minimize function that will create a temp_model.py in working directory. SyntaxError: from future imports print_function must occur at the beginning of the file

as per the SyntaxError have the from future import print_function model in first line so edited the optim_new.py add below function (write_print_function) in optim_new.py or optim.py and in base_minimizer function after write_temp_files(model_str, temp_file) statement call write_print_function(temp_file) it will look like

write_temp_files(model_str, temp_file) write_print_function(temp_file)

def write_print_function(temp_file): f = open(temp_file,"r+") d = f.readlines() f.seek(0) f.write("from future import print_function") for i in d: if "from future import print_function" not in i: f.write(i) f.truncate() f.close() return

maxpumperla commented 5 years ago

@uncodera the idea is correct, though. we can definitely add a little helper function to optim.py that strips dangling future imports

BingBlog commented 5 years ago

I used Python 3.6 and got the same problem and I believe the problem had something to do with Jupyter Notebook. Did you use a Notebook as well?

@simonljus Same problem, how did you fixed?

simonljus commented 5 years ago

@BingBlog I did not use any notebook and solved it by separating the code involving hyperas into one file(for example, hyperas_functions.py, no use of future imports here) and all the rest to the other file (for example other_functions.py, calling the function(s) in the hyperas_functions.py). If this does not work for you, try to use hyperopt directly instead.

token-cjg commented 5 years ago

Code in a jupyter notebook such as:

from __future__ import absolute_import, division, print_function

can be rewritten as:

exec('from __future__ import absolute_import, division, print_function')

sum781 commented 5 years ago

token-cjg It worked>>>>>>>>>>> thanks

berlwein commented 5 years ago

I used Python 3.6 and got the same problem and I believe the problem had something to do with Jupyter Notebook. Did you use a Notebook as well?

I just had the same problem while using jupyter notebook. Using the same code, I switched to an IDE (Spyder), and the problem went away. So, it does seem to be a problem related to notebook, or perhaps ipython.

SAyaka0122 commented 5 years ago

@token-cjg It didn't work for me...Could you please show me your code?

RenaissanceEngineering commented 4 years ago

This didn't work for me either moving back to Hyperopt

aamirkhanz commented 4 years ago

Code in a jupyter notebook such as:

from __future__ import absolute_import, division, print_function

can be rewritten as:

exec('from __future__ import absolute_import, division, print_function')

exactly

nunocesarsa commented 4 years ago

Based on all the suggestions here (Thank you! but it was nightmarish) I came up with yet another variation for sorting this out if you are using a google colab notebook (or similar i reckon).

So, load the package hyperas as normal.

Before importing the function you go into the folder where optim.py is e.g. /usr/local/lib/python3.6/dist-packages/hyperas/

I've adapted from @token-cjg and @uncodera suggestions.

The function will basically just substitute the string on the temporary file that hyperas creates.

Step 1: Open optim.py and add the following function:

def replace_text(temp_file): with open(temp_file,'r') as file: filedata=file.read() filedata=filedata.replace('from future import print_function',"exec('from future import absolute_import, division, print_function')") with open(temp_file, 'w') as file: file.write(filedata) return

Step 2: add the function replace_text(temp_file) immediately after the write_temp_files(model_str, temp_file) within the base_optimizer function.

Then it should basically correct the command for a colab jupiter notebook. One note: In case you have already loaded the optim from hyperas then you must reload it. Yoou can do it by importing importlib and them issuing the command:

importlib.reload(optim).