'generator' object is not subscriptable
See original GitHub issueJust installed in a virtual enviorment with Python 3.6 and I am unable to run the basic example on the readme or any of the example in the example folder and they all give similar errors.
Code
`from future import print_function
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, conditional
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({{choice([‘relu’, ‘sigmoid’])}})) model.add(Dropout({{uniform(0, 1)}}))
# If we choose 'four', add an additional fourth layer
if conditional({{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'])}})
model.fit(x_train, y_train,
batch_size={{choice([64, 128])}},
epochs=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}
if name == ‘main’: best_run, best_model = optim.minimize(model=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)`
Results in
`Using TensorFlow backend.
Imports: #coding=utf-8
from future import print_function
try: from hyperopt import Trials, STATUS_OK, tpe except: pass
try: from keras.datasets import mnist except: pass
try: from keras.layers.core import Dense, Dropout, Activation except: pass
try: from keras.models import Sequential except: pass
try: from keras.utils import np_utils 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 { ‘Dropout’: hp.uniform(‘Dropout’, 0, 1), ‘Dense’: hp.choice(‘Dense’, [256, 512, 1024]), ‘Activation’: hp.choice(‘Activation’, [‘relu’, ‘sigmoid’]), ‘Dropout_1’: hp.uniform(‘Dropout_1’, 0, 1), ‘conditional’: hp.choice(‘conditional’, [‘three’, ‘four’]), ‘add’: hp.choice(‘add’, [Dropout(0.5), Activation(‘linear’)]), ‘optimizer’: hp.choice(‘optimizer’, [‘rmsprop’, ‘adam’, ‘sgd’]), ‘batch_size’: hp.choice(‘batch_size’, [64, 128]), }
Data 1: 2: “”" 3: Data providing function: 4: This function is separated from model() so that hyperopt 5: won’t reload data for each evaluation run. 6: “”" 7: (x_train, y_train), (x_test, y_test) = mnist.load_data() 8: x_train = x_train.reshape(60000, 784) 9: x_test = x_test.reshape(10000, 784) 10: x_train = x_train.astype(‘float32’) 11: x_test = x_test.astype(‘float32’) 12: x_train /= 255 13: x_test /= 255 14: nb_classes = 10 15: y_train = np_utils.to_categorical(y_train, nb_classes) 16: y_test = np_utils.to_categorical(y_test, nb_classes) 17: 18: 19: Resulting replaced keras model:
1: def keras_fmin_fnct(space): 2: 3: “”" 4: Model providing function: 5: Create Keras model with double curly brackets dropped-in as needed. 6: Return value has to be a valid python dictionary with two customary keys: 7: - loss: Specify a numeric evaluation metric to be minimized 8: - status: Just use STATUS_OK and see hyperopt documentation if not feasible 9: The last one is optional, though recommended, namely: 10: - model: specify the model just created so that we can later use it again. 11: “”" 12: model = Sequential() 13: model.add(Dense(512, input_shape=(784,))) 14: model.add(Activation(‘relu’)) 15: model.add(Dropout(space[‘Dropout’])) 16: model.add(Dense(space[‘Dense’])) 17: model.add(Activation(space[‘Activation’])) 18: model.add(Dropout(space[‘Dropout_1’])) 19: 20: # If we choose ‘four’, add an additional fourth layer 21: if conditional(space[‘conditional’]) == ‘four’: 22: model.add(Dense(100)) 23: 24: # We can also choose between complete sets of layers 25: 26: model.add(space[‘add’]) 27: model.add(Activation(‘relu’)) 28: 29: model.add(Dense(10)) 30: model.add(Activation(‘softmax’)) 31: 32: model.compile(loss=‘categorical_crossentropy’, metrics=[‘accuracy’], 33: optimizer=space[‘optimizer’]) 34: 35: model.fit(x_train, y_train, 36: batch_size=space[‘batch_size’], 37: epochs=1, 38: verbose=2, 39: validation_data=(x_test, y_test)) 40: score, acc = model.evaluate(x_test, y_test, verbose=0) 41: print(‘Test accuracy:’, acc) 42: return {‘loss’: -acc, ‘status’: STATUS_OK, ‘model’: model} 43: Traceback (most recent call last): File “hy.py”, line 80, in <module> trials=Trials()) File “/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperas/optim.py”, line 67, in minimize verbose=verbose) File “/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperas/optim.py”, line 133, in base_minimizer return_argmin=True), File “/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperopt/fmin.py”, line 307, in fmin return_argmin=return_argmin, File “/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperopt/base.py”, line 635, in fmin return_argmin=return_argmin) File “/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperopt/fmin.py”, line 314, in fmin pass_expr_memo_ctrl=pass_expr_memo_ctrl) File “/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperopt/base.py”, line 786, in init pyll.toposort(self.expr) File “/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperopt/pyll/base.py”, line 715, in toposort assert order[-1] == expr TypeError: ‘generator’ object is not subscriptable ` Any help would be appreciated
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
Found out it’s an issue with running on Linux or Mac Os. By default both these use networkx version 2.0 which has issues with hyperopt
If you downgrade to version 1.11 the issues go away but this is a Hyperopt issue not a Hyperas issue. This should only be done is virtual environment as a short term solution
pip3 install networkx==1.11
@aprasla0922 @prashanthdumpuri and others, please note that this is an issue with hyperopt that has been fixed on master, but has not been released yet. If you install hyperopt from master, you should be fine as well.