【发布时间】:2018-04-08 02:19:37
【问题描述】:
我正在使用 keras 和 hyperas library 进行超参数优化。
我正在尝试传递一个列表而不是硬编码列表,但它会引发错误。在 hyperas 中,您将模板值包装在“{{ some value }}”中,您可以将代码卡为列表。示例:
model.add(Dense({{choice([258,512,1024])}}))
错误:
File "hy.py", line 89, in
trials=Trials())
File "/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperas-0.4-py3.6.egg/hyperas/optim.py", line 67, in minimize
verbose=verbose)
File "/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperas-0.4-py3.6.egg/hyperas/optim.py", line 118, in base_minimizer
space=get_space(),
File "/home/peachy/Documents/tensorflow/temp_model.py", line 119, in get_space
NameError: name 'dropout' is not defined
使用一个基本示例,这是模型类。我正在从加载列表的 yml 文件加载,但这不起作用,所以我只是试图在模型类中创建一个列表并将其传递进去。它不会工作。
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
import common_config as cfg
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.
"""
ModelConfig = cfg.ModelConfig
dropout = [0,1]
print (dropout)
print (type(dropout))
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation(ModelConfig.activation))
model.add(Dropout({{uniform(dropout)}}))
model.add(Dense({{choice([258,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=10,
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)
值是如何从模板发送到 optim.py 文件的? 我已经检查过了,正则表达式应该能够处理列表。
我不希望任何人知道,但你将如何调试它,因为它很可能是一个库问题
编辑
解决方案 1 和 2 只会引发类型错误,这在源代码中是一个非常奇怪的检查,以查看它是否是某个字符串类型
Hyperopt 代码 pyll_util.py
def validate_label(f):
@wraps(f)
def wrapper(label, *args, **kwargs):
is_real_string = isinstance(label, basestring)
is_literal_string = (isinstance(label, Literal) and
isinstance(label.obj, basestring))
if not is_real_string and not is_literal_string:
raise TypeError('require string label')
return f(label, *args, **kwargs)
return wrapper
错误
Traceback (most recent call last):
File "hy.py", line 107, in <module>
trials=Trials())
File "/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperas-0.4-py3.6.egg/hyperas/optim.py", line 67, in minimize
verbose=verbose)
File "/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperas-0.4-py3.6.egg/hyperas/optim.py", line 136, 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 320, in fmin
rval.exhaust()
File "/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperopt/fmin.py", line 199, in exhaust
self.run(self.max_evals - n_done, block_until_done=self.async)
File "/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperopt/fmin.py", line 173, in run
self.serial_evaluate()
File "/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperopt/fmin.py", line 92, in serial_evaluate
result = self.domain.evaluate(spec, ctrl)
File "/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperopt/base.py", line 840, in evaluate
rval = self.fn(pyll_rval)
File "/home/peachy/Documents/tensorflow/temp_model.py", line 101, in keras_fmin_fnct
File "<string>", line 1, in <module>
File "/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperopt/pyll_utils.py", line 21, in wrapper
raise TypeError('require string label')
TypeError: require string label
通过合并列表的解决方案 3 会导致以下错误,该错误表明 %s 已与 dropout_1 一起基于生成的 temp_model 文件中。
Unexpected error: <class 'SyntaxError'>
Traceback (most recent call last):
File "hy.py", line 107, in <module>
trials=Trials())
File "/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperas-0.4-py3.6.egg/hyperas/optim.py", line 67, in minimize
verbose=verbose)
File "/home/peachy/Documents/tensorflow/tf/lib/python3.6/site-packages/hyperas-0.4-py3.6.egg/hyperas/optim.py", line 104, in base_minimizer
from temp_model import keras_fmin_fnct, get_space
File "/home/peachy/Documents/tensorflow/temp_model.py", line 133
'Dropout_1': hp.uniform('Dropout_1', %s),
^
【问题讨论】:
标签: python python-3.x optimization keras