Code Monkey home page Code Monkey logo

hyperas's Introduction

Hyperas Build Status PyPI version

Hyperas brings fast experimentation with Keras and hyperparameter optimization with Hyperopt together. It lets you use the power of hyperopt without having to learn the syntax of it. Instead, just define your keras model as you are used to, but use a simple template notation to define hyper-parameter ranges to tune.

Installation

pip install hyperas

Quick start

Assume you have data generated as such

def data():
    x_train = np.zeros(100)
    x_test = np.zeros(100)
    y_train = np.zeros(100)
    y_test = np.zeros(100)
    return x_train, y_train, x_test, y_test

and an existing keras model like the following

def create_model(x_train, y_train, x_test, y_test):
    model = Sequential()
    model.add(Dense(512, input_shape=(784,)))
    model.add(Activation('relu'))
    model.add(Dropout(0.2))
    model.add(Dense(512))
    model.add(Activation('relu'))
    model.add(Dropout(0.2))
    model.add(Dense(10))
    model.add(Activation('softmax'))

    # ... model fitting

    return model

To do hyper-parameter optimization on this model, just wrap the parameters you want to optimize into double curly brackets and choose a distribution over which to run the algorithm.

In the above example, let's say we want to optimize for the best dropout probability in both dropout layers. Choosing a uniform distribution over the interval [0,1], this translates into the following definition. Note that before returning the model, to optimize, we also have to define which evaluation metric of the model is important to us. For example, in the following, we optimize for accuracy.

Note: In the following code we use 'loss': -accuracy, i.e. the negative of accuracy. That's because under the hood hyperopt will always minimize whatever metric you provide. If instead you want to actually want to minimize a metric, say MSE or another loss function, you keep a positive sign (e.g. 'loss': mse).

from hyperas.distributions import uniform

def create_model(x_train, y_train, x_test, y_test):
    model = Sequential()
    model.add(Dense(512, input_shape=(784,)))
    model.add(Activation('relu'))
    model.add(Dropout({{uniform(0, 1)}}))
    model.add(Dense(512))
    model.add(Activation('relu'))
    model.add(Dropout({{uniform(0, 1)}}))
    model.add(Dense(10))
    model.add(Activation('softmax'))

    # ... model fitting

    score = model.evaluate(x_test, y_test, verbose=0)
    accuracy = score[1]
    return {'loss': -accuracy, 'status': STATUS_OK, 'model': model}

The last step is to actually run the optimization, which is done as follows:

best_run = optim.minimize(model=create_model,
                          data=data,
                          algo=tpe.suggest,
                          max_evals=10,
                          trials=Trials())

In this example we use at most 10 evaluation runs and the TPE algorithm from hyperopt for optimization.

Check the "complete example" below for more details.

Complete example

Note: It is important to wrap your data and model into functions as shown below, and then pass them as parameters to the minimizer. data() returns the data the create_model() needs. An extended version of the above example in one script reads as follows. This example shows many potential use cases of hyperas, including:

  • Varying dropout probabilities, sampling from a uniform distribution
  • Different layer output sizes
  • Different optimization algorithms to use
  • Varying choices of activation functions
  • Conditionally adding layers depending on a choice
  • Swapping whole sets of layers
from __future__ import print_function
import numpy as np

from hyperopt import Trials, STATUS_OK, tpe
from keras.datasets import mnist
from keras.layers.core import Dense, Dropout, Activation
from keras.models import Sequential
from keras.utils import np_utils

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


def data():
    """
    Data providing function:

    This function is separated from create_model() so that hyperopt
    won't reload data for each evaluation run.
    """
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train = x_train.reshape(60000, 784)
    x_test = x_test.reshape(10000, 784)
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255
    nb_classes = 10
    y_train = np_utils.to_categorical(y_train, nb_classes)
    y_test = np_utils.to_categorical(y_test, nb_classes)
    return x_train, y_train, x_test, y_test


def create_model(x_train, y_train, x_test, y_test):
    """
    Model providing function:

    Create Keras model with double curly brackets dropped-in as needed.
    Return value has to be a valid python dictionary with two customary keys:
        - loss: Specify a numeric evaluation metric to be minimized
        - status: Just use STATUS_OK and see hyperopt documentation if not feasible
    The last one is optional, though recommended, namely:
        - model: specify the model just created so that we can later use it again.
    """
    model = Sequential()
    model.add(Dense(512, input_shape=(784,)))
    model.add(Activation('relu'))
    model.add(Dropout({{uniform(0, 1)}}))
    model.add(Dense({{choice([256, 512, 1024])}}))
    model.add(Activation({{choice(['relu', 'sigmoid'])}}))
    model.add(Dropout({{uniform(0, 1)}}))

    # If we choose 'four', add an additional fourth layer
    if {{choice(['three', 'four'])}} == 'four':
        model.add(Dense(100))

        # We can also choose between complete sets of layers

        model.add({{choice([Dropout(0.5), Activation('linear')])}})
        model.add(Activation('relu'))

    model.add(Dense(10))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy', metrics=['accuracy'],
                  optimizer={{choice(['rmsprop', 'adam', 'sgd'])}})

    result = model.fit(x_train, y_train,
              batch_size={{choice([64, 128])}},
              epochs=2,
              verbose=2,
              validation_split=0.1)
    #get the highest validation accuracy of the training epochs
    validation_acc = np.amax(result.history['val_acc']) 
    print('Best validation acc of epoch:', validation_acc)
    return {'loss': -validation_acc, 'status': STATUS_OK, 'model': model}


if __name__ == '__main__':
    best_run, best_model = optim.minimize(model=create_model,
                                          data=data,
                                          algo=tpe.suggest,
                                          max_evals=5,
                                          trials=Trials())
    X_train, Y_train, X_test, Y_test = 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)

FAQ

Here is a list of a few popular errors

TypeError: require string label

You're probably trying to execute the model creation code, with the templates, directly in python. That fails simply because python cannot run the templating in the braces, e.g. {{uniform..}}. The def create_model(...) function is in fact not a valid python function anymore.

You need to wrap your code in a def create_model(...): ... function, and then call it from optim.minimize(model=create_model,... like in the example.

The reason for this is that hyperas works by doing template replacement of everything in the {{...}} into a separate temporary file, and then running the model with the replaced braces (think jinja templating).

This is the basis of how hyperas simplifies usage of hyperopt by being a "very simple wrapper".

TypeError: 'generator' object is not subscriptable

This is currently a known issue.

Just pip install networkx==1.11

NameError: global name 'X_train' is not defined

Maybe you forgot to return the x_train argument in the def create_model(x_train...) call from the def data(): ... function.

You are not restricted to the same list of arguments as in the example. Any arguments you return from data() will be passed to create_model()

notebook adjustment

If you find error like "No such file or directory" or OSError, Err22, you may need add notebook_name='simple_notebook'(assume your current notebook name is simple_notebook) in optim.minimize function like this:

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

How does hyperas work?

All we do is parse the data and model templates and translate them into proper hyperopt by reconstructing the space object that's then passed to fmin. Most of the relevant code is found in optim.py and utils.py.

How to read the output of a hyperas model?

Hyperas translates your script into hyperopt compliant code, see here for some guidance on how to interpret the result.

How to pass arguments to data?

Suppose you want your data function take an argument, specify it like this using positional arguments only (not keyword arguments):

import pickle
def data(fname):
    with open(fname,'rb') as fh:
        return pickle.load(fh)

Note that your arguments must be implemented such that repr can show them in their entirety (such as strings and numbers). If you want more complex objects, use the passed arguments to build them inside the data function.

And when you run your trials, pass a tuple of arguments to be substituted in as data_args:

best_run, best_model = optim.minimize(
    model=model,
    data=data,
    algo=tpe.suggest,
    max_evals=64,
    trials=Trials(),
    data_args=('my_file.pkl',)
)

What if I need more flexibility loading data and adapting my model?

Hyperas is a convenience wrapper around Hyperopt that has some limitations. If it's not convenient to use in your situation, simply don't use it -- and choose Hyperopt instead. All you can do with Hyperas you can also do with Hyperopt, it's just a different way of defining your model. If you want to squeeze some flexibility out of Hyperas anyway, take a look here.

Running hyperas in parallel?

You can use hyperas to run multiple models in parallel with the use of mongodb (which you'll need to install and setup users for). Here's a short example using MNIST:

  1. Copy and modify examples/mnist_distributed.py (bump up max_evals if you like):

  2. Run python mnist_distributed.py. It will create a temp_model.py file. Copy this file to any machines that will be evaluating models. It will then begin waiting for evaluation results

  3. On your other machines (make sure they have a python installed with all your dependencies, ideally with the same versions) run:

    export PYTHONPATH=/path/to/temp_model.py
    hyperopt-mongo-worker --exp-key='mnist_test' --mongo='mongo://username:[email protected]:27017/jobs'
  4. Once max_evals have been completed, you should get an output with your best model. You can also look through your mongodb and examine the results, to get the best model out and run it, do:

    from pymongo import MongoClient
    from keras.models import load_model
    import tempfile
    c = MongoClient('mongodb://username:[email protected]:27017/jobs')
    best_model = c['jobs']['jobs'].find_one({'exp_key': 'mnist_test'}, sort=[('result.loss', -1)])
    temp_name = tempfile.gettempdir()+'/'+next(tempfile._get_candidate_names()) + '.h5'
    with open(temp_name, 'wb') as outfile:
        outfile.write(best_model['result']['model_serial'])
    model = load_model(temp_name)

hyperas's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hyperas's Issues

The "show_accuracy" argument is deprecated

C:\Program Files\Anaconda2\lib\site-packages\keras\models.py:635: UserWarning:
he "show_accuracy" argument is deprecated, instead you should pass the "accurac
" metric to the model at compile time:
model.compile(optimizer, loss, metrics=["accuracy"])
warnings.warn('The "show_accuracy" argument is deprecated, '

In your example application:

model.fit(X_train, Y_train,
              batch_size={{choice([64, 128])}},
              nb_epoch=1,
              show_accuracy=True,
              verbose=2,
              validation_data=(X_test, Y_test))

use hyperas to tune learning rate

Hi, first of all, this library is awesome!!! Thanks for your contributions.
I am just wondering if there is a way to use hyperas tuning learning rate since I have not found related code in the examples.

Thanks!

Support Python 3 and IPython and add more examples

Hi Max,
I open this issue to officially and kindly ask you to add python 3 and IPython support for both hyperapt and hyperas since you can maintain hyperapt now.

I tried many other packages and wrappers to tune keras, but your package is significantly more efficient than others.
Thanks in advance.

import java.lang.System

Pyhon2.7 Windows64
Running the homepage's "complete example".
A file called temp_model.py is created and in line32 reads, "import java.lang.System"
which causes an error:

ImportError: No module named java.lang.System

import error with bson

After I installed hyperas using'pip install hyperas', I run the example code of lstm, but it raise an error: 'ImportError: No module named bson', I then pip to intall bson, 'pip install bson', but it raise an error:
' Command "python setup.py egg_info" failed with error code 1 in c:\users\admi
ni~1\appdata\local\temp\pip-build-jx55yy\bson', can someone help me, thanks in advance.

Memory explosion

I was tuning a mid-size network. I observed that the memory usage kept rising and with enough trials (around 50) eventually it caused the server to kill it for taking too much memory. As I remember in the README that the data was loaded only once, I wonder if it is because hyperas saves the parameters (i mean actual weights) of all the trials? If so, I feel it makes more sense to release them since we only care about hyper-parameters.

AttributeError: 'NoneType' object has no attribute 'raise_exception_on_not_ok_status

Passing the None type as a default argument for the Voting Model and base minimizer produces an error when running some of the examples (i.e. mnist ensemble example)

Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x7fd8d08742e8>> Traceback (most recent call last): File "/opt/conda/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 522, in __del__ File "/opt/conda/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 518, in close AttributeError: 'NoneType' object has no attribute 'raise_exception_on_not_ok_status'

https://github.com/maxpumperla/hyperas/blob/master/hyperas/optim.py#L88
https://github.com/maxpumperla/hyperas/blob/master/hyperas/ensemble.py#L7

Bug when there are multiple hyper-parameter to tune in one layer

Thank you for the great work in combing hyperopt with keras!

I think I spotted a bug when you have more than one hyper-parameter to tune in one layer:

For example:

    model = Sequential()
    model.add(Convolution2D({{choice([32,64])}}, 1, {{choice([7,11,15])}}, border_mode='same', input_shape=(4, 1, 101),activation='relu'))
    model.add(MaxPooling2D(pool_size=(1, 2),strides=(1,2)))
    model.add(Convolution2D({{choice([64,128])}},1,3, border_mode='same',activation='relu'))
    model.add(MaxPooling2D(pool_size=(1, 2),strides=(1,2)))
    model.add(Flatten())
    model.add(Dense({{choice([32,64,128])}},activation='relu'))
    model.add(Dropout({{choice([0.1,0.5,0.7])}}))
    model.add(Dense(2,activation='softmax'))

Running on this model, it will output:

>>> Hyperas search space:

def get_space():
    return {
        'Convolution2D': hp.choice('Convolution2D', [32,64]),
        'Convolution2D_1': hp.choice('Convolution2D_1', [7,11,15]),
        'Dense': hp.choice('Dense', [64,128]),
        'Dropout': hp.choice('Dropout', [32,64,128]),
    }

which shows that the mapping from the parameter name to the value choices is messed up.

I assume this is a bug?

More parameters ?

Hello Max, are you planning or is it even possible to run a keras model through hyperas with multiple choice of activation function or multiple optimizer ? Thanks!

Graph Disconnected Error

Hi, I am using hyperas to optimize my model. There seems to be some issue it works fine for few epochs after that it gives following error:
Traceback (most recent call last):
File "parameter_tuning.py", line 240, in
main(*sys.argv)
File "parameter_tuning.py", line 180, in main
best_run, best_model = optim.minimize(model=model,data=load_data, algo=tpe.suggest, max_evals=10, trials= Trials())
File "/usr/local/lib/python2.7/dist-packages/hyperas/optim.py", line 31, in minimize
best_run = base_minimizer(model, data, algo, max_evals, trials, rseed)
File "/usr/local/lib/python2.7/dist-packages/hyperas/optim.py", line 111, in base_minimizer
rseed=rseed)
File "/usr/local/lib/python2.7/dist-packages/hyperopt/fmin.py", line 334, in fmin
rval.exhaust()
File "/usr/local/lib/python2.7/dist-packages/hyperopt/fmin.py", line 294, in exhaust
self.run(self.max_evals - n_done, block_until_done=self.async)
File "/usr/local/lib/python2.7/dist-packages/hyperopt/fmin.py", line 268, in run
self.serial_evaluate()
File "/usr/local/lib/python2.7/dist-packages/hyperopt/fmin.py", line 187, in serial_evaluate
result = self.domain.evaluate(spec, ctrl)
File "/usr/local/lib/python2.7/dist-packages/hyperopt/fmin.py", line 114, in evaluate
rval = self.fn(pyll_rval)
File "/home/khanma0b/keras-master/deepgo-master/temp_model.py", line 121, in keras_fmin_fnct
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 1683, in init
str(layers_with_complete_input))
Exception: Graph disconnected: cannot obtain value for tensor input_1 at layer "input_1". The following previous layers were accessed without issue: []

When data can't fit into memory at once

The example given in README demonstrates the usage of hyperas when you have a rather small dataset that you can load into memory at once. I wonder if there is any chance that it can take a "generator" as input, like fit_generator in Keras?

Questions about nb_epoch and some configurations of `optim.minimize`

Hi @maxpumperla !

I saw that all of the examples provided are with nb_epoch=1. I am wondering that:

  1. Will I get more reasonable results (more optimal hyper-parameters) if I set nb_epoch larger than 1 since it will do the optimization by over many times of epochs?
  2. In the method optim.minimize there are some parameters I would like to discuss:
  • algo: what are the options of this parameter (like tpe.suggest and rand.suggest)? And what are your suggestions about when should we choose which, especially for RNN/LSTM model?
  • max_evals: this is the maximum number of execution of evaluate. I am wondering the difference between the following two settings: (1)nb_epoch=10 and max_evals=1; (2) nb_epoch=1 and max_evals=10.
  • trials: I have seen Trials(). What is this parameter used for?

Thanks.

How would you suggest to using hyperas to get the best pairs of hyper-param?

Hi! maxpumperla,
Recently, I have tried hyperas on training LSTM for character-level text generation, but it seems that what hypers get is not the best of hyper-param pairs ,so how would you suggest on using it? For, example, should we group which pairs to optimize and others, you know, I mean a whole process. Thanks in advance!

Hyperas in Keras (Older Version)

I am using an older version of keras - last cloned around January/February.
I am trying to integrate hyperas with keras, and get an error as follows when building my keras model with dropout.

model.add(Dropout({{uniform(0, 1)}}))

File "/Library/Python/2.7/site-packages/hyperopt/pyll_utils.py", line 48, in hp_uniform
raise TypeError('require string label')
TypeError: require string label

Can someone please help me as to why I get an error like this? I am simply initially following the examples as in the hyperas examples.

Passing data-manipulation script inside data() function

Hi Max,

Thanks for incorporating hyperopt + keras in your wonderful implementation.

I am curious about a function retrieve_data_string() in optim.py.
Basically in data() function, user is expected to pass X_test, X_train, Y_test, Y_train but one can not write their data read and preprocess snippets because of line.strip().

For eg.

`

# remove constant columns, which have std = 0
cols_to_remove = []
for col in df.columns:
    if (df[col].std() == 0):
        colsToRemove.append(col)`

This would fail if written in data(). Since this is just a string manipulation this can be easily taken care of.

So, just wondering if you wanted this API to be restricted in order to get data from a different module or pre-processing can be allowed in data() as well ?

hyperas score

I am trying to see different score after the final step of hyperas, is there a way to return a y_pred from the function to use in the main() ?

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, roc_curve, roc_auc_score
from matplotlib import pyplot as plt
%matplotlib inline
from future import print_function
from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform

def keras_model():
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import LSTM
from keras.datasets import imdb
from keras.callbacks import EarlyStopping, ModelCheckpoint

max_features = 20000
maxlen = 100

print('Loading data...')
(X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features, test_split=0.2)
print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')

print("Pad sequences (samples x time)")
X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)

print('Build model...')
model = Sequential()
model.add(Embedding(max_features, 128, input_length=maxlen))
model.add(LSTM(128))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              class_mode="binary")

early_stopping = EarlyStopping(monitor='val_loss', patience=4)
checkpointer = ModelCheckpoint(filepath='keras_weights.hdf5',
                               verbose=1,
                               save_best_only=True)

hist = model.fit(X_train, y_train,
                 batch_size={{choice([32, 64, 128])}},
                 # batch_size=128,
                 nb_epoch=1,
                 validation_split=0.08,
                 show_accuracy=True,
                 callbacks=[early_stopping, checkpointer])

score, acc = model.evaluate(X_test, y_test, show_accuracy=True, verbose=0)

y_pred = model.predict_classes(X_test)

print ('Accuracy:', accuracy_score(y_test, y_pred))
print ("Precision", precision_score(y_test, y_pred))
print ("Recall", recall_score(y_test, y_pred))
print ("f1_score", f1_score(y_test, y_pred))
print ("confusion_matrix")
print (confusion_matrix(y_test, y_pred))
fpr, tpr, tresholds = roc_curve(y_test, y_pred)
auc = roc_auc_score(y_test, y_pred)

#Courbe
plt.figure(figsize=(6, 4))
plt.plot(fpr, tpr, label='ROC Curve. AUC = {0:.2f}'.format(auc))
plt.plot([0, 1], [0, 1], 'r--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('FPR')
plt.ylabel('TPR')
plt.title('ROC Curve')
plt.legend(loc='lower right')
#print('Test accuracy:', acc)

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

if name == 'main':
best_run = optim.minimize(keras_model,
algo=tpe.suggest,
max_evals=1,
trials=Trials())
print(best_run)

Have an issue on pure pycharm file

@maxpumperla I am still confused how I should implement my code in pure pycharm?
it's my pure pycharm file:

`from future import print_function
from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import RMSprop

from keras.datasets import mnist
from keras.utils import np_utils

def data():
"""
Data providing function:

This function is separated from model() so that hyperopt
won't reload data for each evaluation run.
"""
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
nb_classes = 10
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
return X_train, Y_train, X_test, Y_test

def model(X_train, Y_train, X_test, Y_test):
"""
Model providing function:

Create Keras model with double curly brackets dropped-in as needed.
Return value has to be a valid python dictionary with two customary keys:
    - loss: Specify a numeric evaluation metric to be minimized
    - status: Just use STATUS_OK and see hyperopt documentation if not feasible
The last one is optional, though recommended, namely:
    - model: specify the model just created so that we can later use it again.
"""
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation('relu'))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense({{choice([256, 512, 1024])}}))
model.add(Activation('relu'))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense(10))
model.add(Activation('softmax'))

rms = RMSprop()
model.compile(loss='categorical_crossentropy', optimizer=rms, metrics=['accuracy'])

model.fit(X_train, Y_train,
          batch_size={{choice([64, 128])}},
          nb_epoch=1,
          verbose=2,
          validation_data=(X_test, Y_test))
score, acc = model.evaluate(X_test, Y_test, verbose=0)
print('Test accuracy:', acc)
return {'loss': -acc, 'status': STATUS_OK, 'model': model}

X_train, Y_train, X_test, Y_test = data()

best_run, best_model = optim.minimize(model=model,
data=data,
algo=tpe.suggest,
max_evals=5,
trials=Trials(), notebook_name="simple_notebook")
best_model.save("best_model.h5")
print("Evalutation of best performing model:")
print(best_model.evaluate(X_test, Y_test))
`
Should I crate any notebook file?

  1. I create and empty it. then run my code and get the error. "NameError: name 'mnist' is not defined"
  2. then I copy-paste my code into notebook, and run my code. again get the error "NameError: name 'mnist' is not defined".
  3. I run the notebook directly, everything is OK. then run pycharm file and it is OK as well.
  4. empty the notebook, and run pycharm code.. it is still surprisingly working...

I would be grateful if you could let me know how to utilize hyperas in pure pycharm code...

Can data and model function take argument?

It would be nice if they can so that I could pass argument (such as file path) from outside of the function, rather than manually changing the file every time I run it on some new data. Thx!

Is Python 3 supported?

I got the following error when installing in Python 3.4, hence the question: do you support Python 3? If not yet, are there any plans to do it in the near future? I see that you currently run test only for 2.7.

Traceback (most recent call last):
  File "<string>", line 17, in <module>
  File "/Users/<NAME>/Projects/virtual_envs/testing123/build/hyperopt/setup.py", line 119, in <module>
    if package_data is None: package_data = find_package_data(packages)
  File "/Users/<NAME>/Projects/virtual_envs/testing123/build/hyperopt/setup.py", line 102, in find_package_data
    for subdir in find_subdirectories(package):
  File "/Users/Niko/Projects/virtual_envs/testing123/build/hyperopt/setup.py", line 73, in find_subdirectories
    subdirectories = os.walk(package_to_path(package)).next()[1]
AttributeError: 'generator' object has no attribute 'next'

Errors while running example files

Hi @maxpumperla, I just install the latest 0.2 release of hyperas and tried to run the example files. I run the simple.py on both IPyhon and python console, both failed but different error messages. I first ran it on an IPython console, and here is the result:

Unexpected error: <type 'exceptions.NameError'>
Traceback (most recent call last):

File "", line 1, in
runfile('C:/Anaconda2/Lib/site-packages/hyperas-0.2/examples/simple.py', wdir='C:/Anaconda2/Lib/site-packages/hyperas-0.2/examples')

File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)

File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Anaconda2/Lib/site-packages/hyperas-0.2/examples/simple.py", line 75, in
trials=Trials())

File "C:\Anaconda2\lib\site-packages\hyperas-0.2-py2.7.egg\hyperas\optim.py", line 31, in minimize
best_run = base_minimizer(model, data, algo, max_evals, trials, rseed)

File "C:\Anaconda2\lib\site-packages\hyperas-0.2-py2.7.egg\hyperas\optim.py", line 96, in base_minimizer
from temp_model import keras_fmin_fnct, get_space

File "temp_model.py", line 23, in
(X_train, y_train), (X_test, y_test) = mnist.load_data()

NameError: name 'mnist' is not defined

While I ran it on an normal python console, the output is:

runfile('C:/Anaconda2/Lib/site-packages/hyperas-0.2/examples/simple.py', wdir='C:/Anaconda2/Lib/site-packages/hyperas-0.2/examples')
Using Theano backend.
Traceback (most recent call last):
File "", line 1, in
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Anaconda2/Lib/site-packages/hyperas-0.2/examples/simple.py", line 75, in
trials=Trials())
File "C:\Anaconda2\lib\site-packages\hyperas-0.2-py2.7.egg\hyperas\optim.py", line 31, in minimize
best_run = base_minimizer(model, data, algo, max_evals, trials, rseed)
File "C:\Anaconda2\lib\site-packages\hyperas-0.2-py2.7.egg\hyperas\optim.py", line 92, in base_minimizer
model_str = get_hyperopt_model_string(model, data)
File "C:\Anaconda2\lib\site-packages\hyperas-0.2-py2.7.egg\hyperas\optim.py", line 66, in get_hyperopt_model_string
with open(calling_script_file, 'r') as f:
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\Anaconda2\Lib\site-packages\hyperas-0.2\examples<stdin>'

How to solve this? I think I've installed all the dependencies, including the hyperopt. I also tried other examples like the lstm.py and mnist_ensemble.py and the same error occurs.

best parameters

Greetings.
I can't figure out how to view the parameters of the best performing model after a run.
Ideally, I'd like to be able to rank the models by performance (w/the params displayed).
Are either of these possible?

Forgive me if I've missed something in the docs or something obvious in the code, I'm not very python savvy.

Reload data with every run

In the example given, all the data has to be loaded and rebuilt before every trial which is quite a time-consuming task, especially for big datasets.

Is it possible to do it once in the beginning and reuse in all the runs?

IndentationError: unindent does not match any outer indentation level

I'm coding up a script in Ipython, and I'm always running into this error:
IndentationError: unindent does not match any outer indentation level

The problem is that I don't have any indentation errors in my script. If I remove the hyperas routines, it runs fine.

A somewhat related issue is that I've also noticed for some reason old parts of the code are being "retained" even when they are deleted. For example, the output before that error message would point to a certain line it was trying to execute. I would delete that line, run it again, and it would give the same error message even though that part of the code has been deleted.

I find that to fix the bug, I cannot just go to Kernel -> Restart & Run All, that would give the old error (as if the program is seeing the old code that was deleted). Instead, I have to manually save/checkpoint, and re-run the notebook before it runs the new code.

object of type 'NoneType' has no len()

First time try with Hyperas. I get an error: object of type 'NoneType' has no len() at trials=Trials() in the optim.minimize(...) call. Not sure what this means. I am trying to optimize a network that takes three inputs in the form of a list X_train[], so X_train[0] has a set of input data, X_train[1] and X_train_[2] also. Not sure if that could be the cause for this error? Don't know what Hyperas can handle.

Here's some code:

def data(): 
    # load VGG16 output data, X_train and X_valid are lists with data channels
    X_train, Y_train, X_valid, Y_valid = load_VGG16_output(data_type='train', num_channels=3)
    return X_train, Y_train, X_valid, Y_valid
def model(X_train, Y_train, X_valid, Y_valid):

    in_0 = Input(shape=X_train[0].shape[1:])    
    x = Dense(256, activation='relu')(in_0)
    x = Dropout(0.5)(x)

    in_1 = Input(shape=X_train[1].shape[1:])
    y = Dense(64, activation='relu')(in_1) #, W_regularizer=l2(0.01))(in_1)
    y = Dropout(0.5)(y)

    in_2 = Input(X_train[2].shape[1:])
    z = Dense(32, activation='relu')(in_2) #, W_regularizer=l2(0.01))(in_2)
    z = Dropout(0.5)(z)

    x = merge([x, y, z], mode='concat', concat_axis=1)
    
    predict = Dense(1, activation='sigmoid')(x)
    model = Model(input=[in_0, in_1, in_2], output=predict)
   
    optimizer=Adam(lr=1e-4, decay=0.003)

    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])

    # load the class weight to balance the input positive / negative scores
    class_weight = load_class_weight()

    model.fit(X_train, Y_train,
              batch_size={{choice([32, 64, 128])}}, # optimize batch size
              nb_epoch=1, 
              verbose=2, 
              class_weight=class_weight,
              validation_data=(X_valid, Y_valid))

    score, acc = model.evaluate(X_valid, Y_valid, verbose=0)
    print('Valid accuracy: ', acc)
    return {'loss': -acc, 'status': STATUS_OK, 'model': model}
if __name__ == '__main__':
    
    X_train, Y_train, X_valid, Y_valid = data()

    best_run, best_model = optim.minimize(model=model,
                                          data=data,
                                          algo=tpe.suggest,
                                          max_evals=5,
                                          trials=Trials())
       
    print('Evalutation of best performing model: ')
    print(best_model.evaluate(X_valid, Y_valid))

autoload_pylab_o problem with hyperopt

I have recently installed hyperas, but when I run the example code, it raise an error, I have trying to fix it, but not work yet, anyone can help me ? Below is the error info:

Unexpected error: <type 'exceptions.NameError'>
Using Theano backend.
Traceback (most recent call last):

  File "<ipython-input-1-570b570a6029>", line 1, in <module>
    runfile('C:/SciSoft/my_code/test_hyperas.py', wdir='C:/SciSoft/my_code')

  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)

  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/SciSoft/my_code/test_hyperas.py", line 211, in <module>
    trials=Trials())

  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\optim.py", line 31, in minimize
    best_run = base_minimizer(model, data, algo, max_evals, trials, rseed)

  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\optim.py", line 96, in base_minimizer
    from temp_model import keras_fmin_fnct, get_space

  File "temp_model.py", line 8, in <module>
    spy_cfg.IPKernelApp.pylab_import_all = autoload_pylab_o

NameError: name 'autoload_pylab_o' is not defined

hyperas on LSTM

Hi,

I am using keras 0.3.2 and trying a simple LSTM model with hyperas.
But I get the following error. Anything wrong ?

Exception: Layer is not connected. Did you forget to set "input_shape"?

model = Sequential()
model.add(LSTM(input_dim=388, output_dim=300, return_sequences=False))
model.add(Dropout({{uniform(0, 1)}}))
model.add(Dense(output_dim=1))
model.add(Activation("linear"))
model.compile(loss="mean_squared_error", optimizer="rmsprop")

Error in Running Complete Example: SyntaxError: invalid syntax Unexpected error: <class 'SyntaxError'>

Hi,
While I am running your complete example, I am getting the following error, and I do not know why?
I am on pycharm and default interpreter of its console is IPython.

Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/IPython/core/interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 66, in
trials=Trials())
File "/usr/local/lib/python3.4/dist-packages/hyperas/optim.py", line 31, in minimize
best_run = base_minimizer(model, data, algo, max_evals, trials, rseed)
File "/usr/local/lib/python3.4/dist-packages/hyperas/optim.py", line 96, in base_minimizer
from temp_model import keras_fmin_fnct, get_space
File "/home/ermia/JetBrains/pycharm-2016.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/ermia/PycharmProjects/DeepLearning/temp_model.py", line 3, in
from _pydevd_bundle.pydevconsole_code_for_ironpython import InteractiveConsole
File "/home/ermia/JetBrains/pycharm-2016.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "/home/ermia/JetBrains/pycharm-2016.2/helpers/pydev/_pydevd_bundle/pydevconsole_code_for_ironpython.py", line 105
except SyntaxError, err:
^
SyntaxError: invalid syntax
Unexpected error: <class 'SyntaxError'>

IndentationError: unexpected indent error in your "complete example"

@maxpumperla Hi Max,
I think there is a serious issue whenever we utilize loops in hyperas. for example in your complete example in the first page of your GitHub repo, I get the following error:

File "", line 39
if conditional({{choice(['three', 'four'])}}) == 'four':
^
IndentationError: unexpected indent

I put the very complete/modified example of your code here:
`
from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform, conditional
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU, SReLU, PReLU, ThresholdedReLU

def data():
"""
Data providing function:

This function is separated from model() so that hyperopt
won't reload data for each evaluation run.
"""
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
nb_classes = 10
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
return X_train, Y_train, X_test, Y_test

def model(X_train, Y_train, X_test, Y_test):
"""
Model providing function:

Create Keras model with double curly brackets dropped-in as needed.
Return value has to be a valid python dictionary with two customary keys:
    - loss: Specify a numeric evaluation metric to be minimized
    - status: Just use STATUS_OK and see hyperopt documentation if not feasible
The last one is optional, though recommended, namely:
    - model: specify the model just created so that we can later use it again.
"""
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(BatchNormalization())
model.add(Activation({{choice(['relu', 'softplus', 'softsign'])}}))
model.add(Dropout({{uniform(0, 0.5)}}))

model.add(Dense({{choice([32, 64, 128, 256, 512, 768, 1024])}}))
model.add(BatchNormalization())
model.add(Activation({{choice(['relu', 'sigmoid', 'hard_sigmoid', 'softmax', 'tanh', 'softplus', 'softsign'])}}))
model.add(Dropout({{uniform(0, 0.5)}}))

# If we choose 'four', add an additional fourth layer
# if conditional({{choice(['three', 'four'])}}) == 'four':
for _ in range(1, conditional({{choice([1, 4, 8, 16])}})):
    # model.add(Dense(100))
    model.add(Dense({{choice([32, 64, 128, 256, 512, 768, 1024])}}))
    model.add(BatchNormalization())
    # We can also choose between complete sets of layers
    model.add({{choice([Dropout(0.5), Dropout(0.25),
                        Activation('linear'), Activation('relu'), Activation('hard_sigmoid'), Activation('softmax')])}})
    model.add(Activation('relu'))

model.add(Dense(10))
model.add(BatchNormalization())
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', metrics=['accuracy'],
              optimizer={{choice(['rmsprop', 'adam', 'sgd', 'nadam', 'adadelta', 'adamax', 'adagrad'])}})

model.fit(X_train, Y_train,
          batch_size={{choice([64, 128, 256, 512, 768, 1024])}},
          nb_epoch=1,
          verbose=2,
          validation_data=(X_test, Y_test))
score, acc = model.evaluate(X_test, Y_test, verbose=0)
print('Test accuracy:', acc)
return {'loss': -acc, 'status': STATUS_OK, 'model': model}

best_run, best_model = optim.minimize(model=model,
data=data,
algo=tpe.suggest,
max_evals=100,
trials=Trials(), notebook_name="simple_notebook")
best_model.save("best_model.h5")
X_train, Y_train, X_test, Y_test = data()
print("Evaluation of best performing model:")
print(best_model.evaluate(X_test, Y_test))
print(best_model.summary())
`

If you run this code, you will get.

File "", line 57
for _ in range(1, conditional({{choice([1, 4, 8, 16])}})):
^
IndentationError: unexpected indent

so, it is serious issue, and I think it's because you might forget special note there... the code work properly in previous versions :(

nb_epoch > 1 ?

I am wondering why keras does not allow to search for hyperparameters through more than 1 epoch in every evaluation ? Can you explain a little the behaviour of the TPE algorithm in keras ? Thanks!

cannot import name 'conditional'

I encountered a problem when installing hyperas package by python 3.5. I posted this problem on (StackOverflow)[http://stackoverflow.com/questions/35978549/pip-install-hyperopt-and-hyperas-fail] and the problem was solved by pip install git+https://github.com/hyperopt/hyperopt.git. However, when I ran from hyperas.distributions import choice, uniform, conditional, it told me that ImportError: cannot import name 'conditional'. So I opened the distributions.py file and I found no conditional function.

Ensemble example - TypeError: 'map' object is not subscriptable

I'm running the examples with Python 3. I've managed to get most of them running so far.

The mnist ensemble example breaks with Python3 and I would appreciate any recommendations on how to fix it.

Traceback (most recent call last): File "hypetest/mnist_ensemble.py", line 68, in <module> preds =ensemble_model.predict(X_test) File "/opt/conda/lib/python3.5/site-packages/hyperas/ensemble.py", line 36, in predict nb_classes = predictions[0].shape[1] TypeError: 'map' object is not subscriptable

Ignoring comments

So in the model function I often experiment with different configurations commenting in/out different parameters and layers of the model. The thing is, even if you comment out the line with {{uniform[0,1]}} it is still gonna be picked up by the parsing script and added into parameter space.

E.g. I get this:

    def get_space(): 
        return {
            'GRU': hp.choice('GRU', [256, 512, 1024]),
            'Dropout': hp.uniform('Dropout', 0.0, 0.7),
            'Dense': hp.choice('Dense', [512, 1024]),
            'Dropout_1': hp.uniform('Dropout_1', 0.0, 0.3)
        }

For this code:

    model = Sequential()
    model.add(GRU(
        {{choice([256, 512, 1024])}},
        input_dim=EMBEDDING_SIZE,
        input_length=SAMPLE_LENGTH,
    ))
    model.add(BatchNormalization())
    model.add(Dropout({{uniform(0, 0.7)}}))

    #model.add(Dense({{choice([512, 1024])}}, activation='relu'))

    #model.add(BatchNormalization())
    #model.add(Dropout({{uniform(0, 0.3)}}))

    model.add(Dense(OUTPUT_LAYER))

Would it be possible to filter them out in the parser?

hyperopt OsError

I have this error that i can't get rid of "OSError: [Errno 2] No such file or directory: './temp.pyc". My code is the same as the CNN_LSTM example from keras. But i am running from a virtualenv.

Error when run as a module like "python -m test.test"

Unexpected error: <type 'exceptions.SyntaxError'>
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"main", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/hyhy/dicom/opt/test.py", line 117, in
voting='hard')
File "/usr/local/lib/python2.7/dist-packages/hyperas/optim.py", line 46, in best_ensemble
data=data, algo=algo, max_evals=max_evals, trials=trials)
File "/usr/local/lib/python2.7/dist-packages/hyperas/optim.py", line 51, in best_models
base_minimizer(model, data, algo, max_evals, trials)
File "/usr/local/lib/python2.7/dist-packages/hyperas/optim.py", line 96, in base_minimizer
from temp_model import keras_fmin_fnct, get_space
File "temp_model.py", line 1
importers when locating support scripts as well as when importing modules.
^
SyntaxError: invalid syntax

cannot import conditional ?

Hi i've just copied your example from manual for some test, it seems the 'conditional' cant be imported?

from hyperas.distributions import choice, uniform, conditional

ImportError: cannot import name conditional

temp_model.py invalid syntax error

Hello @maxpumperla !
I am using ipython notebooks
I have a syntax error by running your example code on my data. It is because of generated temp-model.py file. Especially because of first line of this file, which contains some comments and placed without any spec. simbols, like # or '''

There are first lines of temp_model.py file.

importers when locating support scripts as well as when importing modules.
import sys
import importlib.machinery # importlib first so we can test #15386 via -m
import importlib.util
import types
from pkgutil import read_code, get_importer

There is a syntax error in my main .py file

File "PATH_TO/temp_model.py", line 1
importers when locating support scripts as well as when importing modules.
^
SyntaxError: invalid syntax

Whats going wrong? Thanks for helping!

Make hyperas run inside iPython notebook

This is a suggestion to enhance hyperas so that it can be used within iPython notebook. This enhancement is also related to issue: #28

In current implementation, hyperas tries to read the "import" lines from its calling script and write them to an external temporary file. This does not work with an iPython notebook because those "import" lines are modified by the notebook. I could not figure out a quick-and-easy solution to it. It may require some redesign of the running mechanisms of hyperas. @maxpumperla

inspect.getsource(model) not working in optim.py , getting IOError: could not get source code,

Hi @maxpumperla , I was just trying to run the codes in example folder but all executions failing at inspect.getsource(model) in optim.py .
Could you please advise.

Below is the error dump.

File "C:\Users\Singh\Anaconda2new\lib\site-packages\hyperas\optim.py", line 114, in get_hyperopt_model_string
model_string = inspect.getsource(model)
File "C:\Users\Singh\Anaconda2new\lib\inspect.py", line 701, in getsource
lines, lnum = getsourcelines(object)
File "C:\Users\Singh\Anaconda2new\lib\inspect.py", line 690, in getsourcelines
lines, lnum = findsource(object)
File "C:\Users\Singh\Anaconda2new\lib\inspect.py", line 538, in findsource
raise IOError('could not get source code')
IOError: could not get source code

Error with example

I run into this error when trying out the example provided in README. What can I do to fix it?

Traceback (most recent call last):
  File "grid_search.py", line 80, in <module>
    print(best_model.evaluate(X_test, Y_test))
AttributeError: 'NoneType' object has no attribute 'evaluate'

temp_model.py generation chops off commands from def data()

Running hyperas to a modified piece of keras data results in the following error:

Unexpected error: <type 'exceptions.SyntaxError'> Traceback (most recent call last): File "lstm-hyperas.py", line 165, in <module> trials=Trials()) File "C:\Users\stuart\Anaconda2\lib\site-packages\hyperas\optim.py", line 31, in minimize best_run = base_minimizer(model, data, algo, max_evals, trials, rseed) File "C:\Users\stuart\Anaconda2\lib\site-packages\hyperas\optim.py", line 96, in base_minimizer from temp_model import keras_fmin_fnct, get_space File "C:\Users\stuart\Documents\Developer\Python\ForexPredictionV1\LSTM\temp_model.py", line 28 h open(file_name, 'rU') as f: #opens PW file ^ SyntaxError: invalid syntax

Which makes sense when you look at the temp_model.py file that was created:

from keras.callbacks import ReduceLROnPlateau
from keras.regularizers import l2, activity_l2, l1, l1l2
from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
import cPickle as pickle
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
from hyperas.distributions import conditional

S_OUT = 15
e_name = 'output-final-150000.csv'
_data = []
h open(file_name, 'rU') as f:  #opens PW file
ader = csv.reader(f)
w_data = list(list(rec) for rec in csv.reader(f, delimiter=',')) #reads csv into a list of lists
close() #close the csv

onvert string values to floats using a buffer
fer = []
 x in range(len(raw_data)):
ffer.append([float(list_item) for list_item in raw_data[x]])

_data = buffer

ler = MinMaxScaler(feature_range=(0, 1))
_data = scaler.fit_transform(raw_data)

in_size = int(len(raw_data) * 0.67)
t_size = int(len(raw_data) * 0.33)

lace the data the day before in X and the day after (the one to predict) in Y
 raw_data[:-DAYS_OUT]

y_prices = []
 data in raw_data:
ly_prices.append(data[5])

 []

Hyperas chops off my commands. What? Code in keras_fmin_fnct(space) is unaltered.

Error when choosing between sets of advanced activations layers

Hi, I am optimizing the number of neurons and the activation function of the layers of a two hidden-layer network for the mnist dataset, but when I tried choosing from a set of advanced activations from keras I get the following error:

ValueError: Incompatible shapes for broadcasting: (?, 128) and (8,)

Here is my model's definition:

model = Sequential()

model.add(Dense({{choice([8, 128])}}, input_dim=784))
model.add({{choice([advanced_activations.ThresholdedReLU(), advanced_activations.SReLU()])}})

model.add(Dense({{choice([8, 128])}}))
model.add({{choice([advanced_activations.ThresholdedReLU(), advanced_activations.SReLU()])}})

model.add(Dense(10))
model.add(Activation('softmax'))

I think it is a issue on how hyperas manages the space of the parameters to tune.

How to run multiple optimisations in a loop?

Hi,

I want to find optimal paramaters for a time series regression task at different steps, eg 1 step, 3 steps, 6 steps etc. I can't seem to get this to work though as no matter what step variable I get into the temp_model.py it always uses the first time step (even though it shows the model.py with the new timestep value).

def step_data():
    EPS = 1e-6
    all_data = load_data('/path/to/data.csv', EPS)
    return all_data
def do_model(all_data):
    _steps = steps
    print("steps:", _steps)
    features = all_data[:-_steps]
    labels = all_data[_steps:, 4:]
    tts = train_test_split(features, labels, test_size=0.4)
    .......
    return {'loss': rmse_val, 'status': STATUS_OK, 'metrics': metrics}

if __name__ == "__main__":
    results = []
    for i in [1, 3, 6, 9, 12]:
        trials = Trials()
        best_run, best_model = optim.minimize(
            model=do_model,
            data=step_data,
            algo=tpe.suggest,
            max_evals=1,
            trials=trials,
            extra={'steps':i}
        )
        results.extend(pluck.pluck(trials.results, 'metrics'))
     print(tabulate.tabulate(sorted(results, key=lambda x: (x['steps'], x['rmse'])), headers='keys'))

When I do this, I get the output (extra modelling stuff has been snipped for brevity):

Using Theano backend.
PARTS:
hidden_neurons
batch_size
dropout
>>> Hyperas search space:

def get_space():
    return {
        'hidden_neurons': hp.choice('hidden_neurons', [212, 230, 256]),
        'batch_size': hp.choice('batch_size', [128, 148, 156, 164, 196]),
        'dropout': hp.uniform('dropout', 0, 1),
    }

>>> Data

EPS = 1e-6
all_data = load_data('/path/to/data.csv', EPS)

>>> Resulting replaced keras model:

def keras_fmin_fnct(space):

    steps = 1
    _steps = steps
    print("steps:", _steps)
    ...
    return {'loss': rmse_val, 'status': STATUS_OK, 'metrics': metrics}

steps: 1

>>> Resulting replaced keras model:

def keras_fmin_fnct(space):

    steps = 3
    _steps = steps
    print("steps:", _steps)
    ...
    return {'loss': rmse_val, 'status': STATUS_OK, 'metrics': metrics}

steps: 1
...
>>> Resulting replaced keras model:

def keras_fmin_fnct(space):

    steps = 6
    _steps = steps
    print("steps:", _steps)
    ...

steps: 1

I hope I'm not missing some annoying python scoping behaviour here...

In order to put the extra data in, I modified optim.py to include an extra parameter which basically calls this function:

def get_extras_string(extras=None, indent=4):
    out = []
    if type(extras) is dict:
        for k, v in extras.items():
            out.append((" "*indent)+("{} = {}\n".format(k, v)))
    return ''.join(out)

So now get_hyperopt_model_string looks like:

def get_hyperopt_model_string(model, data, extra=None):
    model_string = inspect.getsource(model)
    lines = model_string.split("\n")
    lines = [line for line in lines if not line.strip().startswith('#')]

    calling_script_file = os.path.abspath(inspect.stack()[-1][1])
    with open(calling_script_file, 'r') as f:
        calling_lines = f.read().split('\n')
        raw_imports = [line.strip() + "\n" for line in calling_lines if "import" in line]
        imports = ''.join(raw_imports)
    model_string = [line + "\n" for line in lines if "import" not in line]

    model_string.insert(1, get_extras_string(extra) )
    model_string = ''.join(model_string)

    parts = hyperparameter_names(model_string)
    aug_parts = augmented_names(parts)

    hyperopt_params = get_hyperparameters(model_string)
    space = get_hyperopt_space(parts, hyperopt_params)

    data_string = retrieve_data_string(data, extra)
    model = hyperopt_keras_model(model_string, parts, aug_parts)

    temp_str = temp_string(imports, model, data_string, space)
    return temp_str

My full script is here if you want to look: https://github.com/JonnoFTW/traffic-prediction/blob/master/main.py

The results of `lstm` example: batch_size=1.

I have run this example and the results I got is:

{'Dropout': 0.055013664364538806, 'batch_size': 1}

However, the batch_size=1 is not even in the options: batch_size={{choice([32, 64, 128])}}. Do you think this is reasonable?

IndexError parsing strings when I using

Hi,

When I try to run hyperas on my keras code, I seem to run into an IndexError somehow related to parsing strings.

Code:


from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform,loguniform

def data():
    import math
    import platform
    import random
    import sys
    import time
    import numpy as np
    import scipy.io
    import platform
    import keras
    from keras.models import Sequential
    from keras.layers import Dense, Dropout, Activation
    from keras.layers import Embedding
    from keras.layers import LSTM,GRU
    import numpy as np
    from hyperopt import Trials, STATUS_OK, tpe
    from hyperas import optim
    from hyperas.distributions import choice, uniform,loguniform
    #from tqdm import *
    WINDOW_SIZE=1
    BATCH_SIZE=500
    make_windows=lambda data, window_size: np.stack([ data[row:row+WINDOW_SIZE,:] for row in range(WINDOW_SIZE,data.shape[0]-WINDOW_SIZE) if row%WINDOW_SIZE==0])
    one_hot_row = lambda place,leng: np.array([1 if i==place else 0 for i in range(leng)])
    one_hot_df = lambda df, leng: np.array([one_hot_row(row, leng) for row in df])
    precision = lambda c_m: c_m.astype(float).diagonal() / np.sum(c_m, 1)
    recall = lambda c_m: c_m.astype(float).diagonal() / np.sum(c_m, 0)
    f1scores = lambda c_m: 2 * precision(c_m) * recall(c_m) / (precision(c_m) + recall(c_m))
    meanf1 = lambda c_m: np.mean(np.nan_to_num(f1scores(c_m)))
    OPP_mat_file = scipy.io.loadmat('opp2IMUsOnly.mat')
    opp_training_data = OPP_mat_file['trainingData'].transpose()
    opp_training_labels = OPP_mat_file['trainingLabels']#.transpose()
    opp_val_data = OPP_mat_file['testingData'].transpose()
    opp_val_labels = OPP_mat_file['testingLabels']#.transpose()
    target_data = make_windows(opp_training_data, WINDOW_SIZE)#pamap_2_data# pamap_2_data[0:(pamap_2_data.shape[0]/2),:]
    target_labels = make_windows(one_hot_df(opp_training_labels-1,18), WINDOW_SIZE) #pamap_2_labels-2  # pamap_2_labels[0:(pamap_2_data.shape[0]/2),:]
    val_data = make_windows(opp_val_data, WINDOW_SIZE) #pamap_2_data# pamap_2_data[(pamap_2_data.shape[0]/2):,:]
    val_labels = make_windows(one_hot_df(opp_val_labels-1,18), WINDOW_SIZE)
    return target_data, target_labels, val_data, val_labels

def model(target_data, target_labels, val_data, val_labels):
    WINDOW_SIZE=1
    BATCH_SIZE=500
    DROPOUT=0.5
    LAYER_SIZE=1,#{{choice([1,2,3])}}
    #LEARNING_RATE=
    #LEARNING_RATE_DECAY =
    RHO=0.9
    N_LAYERS=2
    model = Sequential()
    model.add(LSTM(300, batch_input_shape=(BATCH_SIZE, WINDOW_SIZE,79), stateful=True,init='glorot_uniform', dropout_W=DROPOUT))
    '''for i in range(N_LAYERS-1):
        model.add(LSTM(150, stateful=True,init='glorot_uniform', dropout_W=0.5,W_regularizer = keras.regularizers.l1(1) ))#model.add())
    '''#model.add(LSTM(output_dim=18, activation='sigmoid', inner_activation='hard_sigmoid', input_shape=(79,)))
    model.add(Dropout(DROPOUT))
    model.add(Dense(18))
    model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy',
                  optimizer=keras.optimizers.RMSprop(lr={{loguniform(-7,-2)}},rho=RHO, decay={{loguniform(-7,-2)}}, epsilon=1e-05),#lr=0.010494504630565, rho=0.9,
                  metrics=['categorical_accuracy'])
    model.fit(target_data[:-(target_data.shape[0]%BATCH_SIZE),:], target_labels[:-(target_labels.shape[0]%BATCH_SIZE),0,:], batch_size=BATCH_SIZE, nb_epoch=50,verbose=2)
    predictions = model.predict(batch_size=BATCH_SIZE, x=val_data[:-(val_data.shape[0]%BATCH_SIZE),:])
    actuals = np.argmax(one_hot_df(opp_val_labels[1:-1-(val_data.shape[0]%BATCH_SIZE)]-1, 18), axis=1)
    test_conf_matrix = sklearn.metrics.confusion_matrix(
                                y_pred=np.argmax(predictions, axis=1),
                                y_true=actuals, labels=list(range(0, 18)))
    trial_mean_f1_score = meanf1(test_conf_matrix)
    outputDict = {'loss': -trial_mean_f1_score, 'status': STATUS_OK, 'model': model, 'eval_time': time.time()}
    return outputDict

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


Error:


Using TensorFlow backend.
Traceback (most recent call last):
  File "/Users/shanework/Dropbox/tfreconstlstmexps/9sept modified slightly and still works/using_hyperas.py", line 82, in <module>
    trials=Trials())
  File "/Users/shanework/miniconda2/lib/python2.7/site-packages/hyperas/optim.py", line 31, in minimize
    best_run = base_minimizer(model, data, algo, max_evals, trials, rseed)
  File "/Users/shanework/miniconda2/lib/python2.7/site-packages/hyperas/optim.py", line 92, in base_minimizer
    model_str = get_hyperopt_model_string(model, data)
  File "/Users/shanework/miniconda2/lib/python2.7/site-packages/hyperas/optim.py", line 74, in get_hyperopt_model_string
    parts = hyperparameter_names(model_string)
  File "/Users/shanework/miniconda2/lib/python2.7/site-packages/hyperas/optim.py", line 154, in hyperparameter_names
    parts.append(parts[-1])
IndexError: list index out of range

The packages were installed from pip:
hyperas 0.2
Keras 1.0.7
hyperopt 0.0.2

Thank-you.

ValueError: Incompatible shapes for broadcasting: (512,) and (?, 1024)

when training the following model

def model(Xtrain, Xtest, ytrain, ytest, nb_classes):
dims = Xtrain.shape[1]
#neurons = 2056
print('Building model...')
#neurons_all_layers = {{choice([256, 512, 1024, 2056])}}
#activation = {{choice([LeakyReLU(), LeakyReLU(), PReLU()])}}
model = Sequential()
model.add(Dense(2056, input_shape=(dims,)))
model.add({{choice([LeakyReLU(), LeakyReLU(), PReLU()])}})
model.add(BatchNormalization())
model.add(Dropout({{uniform(0, 1)}}))

model.add(Dense({{choice([256, 512, 1024, 2056])}}))
model.add({{choice([LeakyReLU(), LeakyReLU(), PReLU()])}})
model.add(BatchNormalization())
model.add(Dropout({{uniform(0, 1)}}))

I get:
"ValueError: Incompatible shapes for broadcasting: (512,) and (?, 1024)"

Any recommendations on how to fix this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.