hyperas
hyperas copied to clipboard
Strange instance error between Str and Float
Aim is to optimize hyperparameters for a Keras LSTM via Hyperas. While implying the structure of the example like this:
def LSTM_Hyperas_Data():
X_train_augment = pd.read_excel("EscapeKanji_X.xlsx")
y_train_augment = pd.read_excel("EscapeKanji_y.xlsx")
X_train_augment = X_train_augment.drop(columns="Del")
y_train_augment = y_train_augment.drop(columns="Del")
Augmentor=60
Train_Num=1396
X_train_augment_R=np.array(X_train_augment).reshape(Augmentor+1,Train_Num,1)
y_train_augment_R=np.array(y_train_augment).reshape(Augmentor+1,Train_Num,1)
X_train_Hyperas, X_test_Hyperas, y_train_Hyperas, y_test_Hyperas = train_test_split(X_train_augment_R, y_train_augment_R, test_size=0.045, random_state=618)
x_train, y_train, x_test, y_test=X_train_Hyperas, y_train_Hyperas,X_test_Hyperas, y_test_Hyperas
return x_train, y_train, x_test, y_test
def createLSTMModel(x_train, y_train, x_test, y_test):#learning_rate={{choice([0.001,0.01,0.1,0.0001])}},activation={{choice(['relu', 'linear','sigmoid','hard_sigmoid', 'tanh'])}}, loss={{choice(['logcosh', 'mae', 'mse', 'hinge','squared_hinge'])}}, n_jobs=1):
# X_train_R_Grid=X_train_smallLSTM.reshape(4,Train_Num,1)
# y_train_R_Grid=y_train_smallLSTM.reshape(4,Train_Num,1)
#K.clear_session()
model = Sequential()
#model.add(Reshape((2,Train_Num,), input_shape=(1,2*Train_Num,)))
model.add(Bidirectional(LSTM(250, return_sequences=True,activation='relu'),input_shape=(Train_Num,1)))
model.add(Dropout({{uniform(0.05,0.7)}}))
model.add(Bidirectional(LSTM(170, return_sequences=True,activation='relu')))
model.add(Dropout({{uniform(0.05,0.7)}}))
model.add(Bidirectional(LSTM(25, return_sequences=True, activation='relu')))
model.add(Dropout({{uniform(0.05,0.7)}}))
#model.add(Flatten())
model.add(Dense(1))
model.compile(optimizer='Nadam',loss='logcosh')
model.fit(x_train, y_train,
batch_size={{choice([64,128,200,256])}},
epochs=15,
show_accuracy=True,
verbose=2,
validation_split=0.1)
validation_acc = np.amax(result.history['mse'])
print('Best validation acc of epoch:', validation_acc)
return {'loss': mse, 'status': STATUS_OK, 'model': model}
if __name__ == '__main__':
best_run, best_model = optim.minimize(model=createLSTMModel,
data=LSTM_Hyperas_Data,
algo=tpe.suggest,
max_evals=5,
trials=Trials(),
notebook_name='EscapeKanji')
#encoding='utf-8
x_train, y_train, x_test, y_test = LSTM_Hyperas_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)
Number of hyperparameters to optimize is reduced to Batch_Size only to reduce possible error sources, as following error persists:
>>> Imports:
#coding=utf-8
from __future__ import print_function
try:
import tensorflow as tf
except:
pass
try:
import numpy as np
except:
pass
try:
import seaborn as sns
except:
pass
try:
import scipy.io as sio
except:
pass
try:
import joblib
except:
pass
try:
import pandas as pd
except:
pass
try:
import matplotlib.pyplot as plt
except:
pass
try:
import pyexcel as pe
except:
pass
try:
from hyperopt import Trials, STATUS_OK, tpe
except:
pass
try:
from hyperas import optim
except:
pass
try:
from hyperas.distributions import choice, uniform
except:
pass
try:
import sys
except:
pass
try:
import os
except:
pass
try:
import transforms3d as TF3d
except:
pass
try:
import statsmodels
except:
pass
try:
import random
except:
pass
try:
import bezier
except:
pass
try:
from imblearn.over_sampling import SMOTE
except:
pass
try:
from operator import itemgetter
except:
pass
try:
from statsmodels import robust
except:
pass
try:
from openpyxl import Workbook
except:
pass
try:
from os.path import dirname, join
except:
pass
try:
from pylab import rcParams
except:
pass
try:
from time import time
except:
pass
try:
from numba import cuda
except:
pass
try:
import plotly_express as px
except:
pass
try:
import holoviews as hv
except:
pass
try:
import bokeh.io
except:
pass
try:
import bokeh.models
except:
pass
try:
import bokeh.palettes
except:
pass
try:
import bokeh.plotting
except:
pass
try:
from ipywidgets import interact
except:
pass
try:
from math import sqrt
except:
pass
try:
from bokeh.palettes import Spectral4, Spectral6
except:
pass
try:
from bokeh.io import output_file, show, push_notebook, curdoc
except:
pass
try:
from bokeh.plotting import figure, output_file, show
except:
pass
try:
from bokeh.models import ColumnDataSource, HoverTool, LinearColorMapper, ColorBar
except:
pass
try:
from bokeh.transform import transform
except:
pass
try:
from bokeh.layouts import widgetbox
except:
pass
try:
from bokeh.models.widgets import Dropdown, CheckboxGroup, Select, Slider, TextInput
except:
pass
try:
from sklearn import preprocessing
except:
pass
try:
from sklearn.ensemble import BaggingRegressor
except:
pass
try:
from sklearn import model_selection
except:
pass
try:
from sklearn.preprocessing import StandardScaler, MinMaxScaler, Normalizer
except:
pass
try:
from sklearn.svm import SVR
except:
pass
try:
from sklearn.linear_model import LinearRegression
except:
pass
try:
from sklearn.model_selection import train_test_split
except:
pass
try:
from sklearn.metrics import mean_squared_error, r2_score, explained_variance_score
except:
pass
try:
from sklearn.model_selection import cross_val_score, cross_val_predict, cross_validate, GridSearchCV, RandomizedSearchCV
except:
pass
try:
from sklearn.neural_network import MLPRegressor
except:
pass
try:
from keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
except:
pass
try:
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, Flatten, Dropout, LSTM, Bidirectional, Reshape
except:
pass
try:
from keras.models import Model, Sequential
except:
pass
try:
from keras.layers import BatchNormalization
except:
pass
try:
from keras.optimizers import Adam, RMSprop, SGD, Nadam
except:
pass
try:
from keras.regularizers import l2
except:
pass
try:
from keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard
except:
pass
try:
from keras import backend as K
except:
pass
>>> Hyperas search space:
def get_space():
return {
'learning_rate': hp.choice('learning_rate', [0.001,0.01,0.1,0.0001]),
'activation': hp.choice('activation', ['relu', 'linear','sigmoid','hard_sigmoid', 'tanh']),
'loss': hp.choice('loss', ['logcosh', 'mae', 'mse', 'hinge','squared_hinge']),
'Dropout': hp.uniform('Dropout', 0.05,0.7),
'Dropout_1': hp.uniform('Dropout_1', 0.05,0.7),
'Dropout_2': hp.uniform('Dropout_2', 0.05,0.7),
'batch_size': hp.choice('batch_size', [64,128,200,256]),
}
>>> Data
1:
2: X_train_augment = pd.read_excel("EscapeKanji_X.xlsx")
3: y_train_augment = pd.read_excel("EscapeKanji_y.xlsx")
4:
5: X_train_augment = X_train_augment.drop(columns="Del")
6: y_train_augment = y_train_augment.drop(columns="Del")
7:
8: Augmentor=60
9: Train_Num=1396
10:
11: X_train_augment_R=np.array(X_train_augment).reshape(Augmentor+1,Train_Num,1)
12: y_train_augment_R=np.array(y_train_augment).reshape(Augmentor+1,Train_Num,1)
13:
14: X_train_Hyperas, X_test_Hyperas, y_train_Hyperas, y_test_Hyperas = train_test_split(X_train_augment_R, y_train_augment_R, test_size=0.045, random_state=618)
15: x_train, y_train, x_test, y_test=X_train_Hyperas.astype('float32'), y_train_Hyperas.astype('float32'),X_test_Hyperas.astype('float32'), y_test_Hyperas.astype('float32')
16:
17:
18:
>>> Resulting replaced keras model:
1: def keras_fmin_fnct(space):
2:
3:
4: # X_train_R_Grid=X_train_smallLSTM.reshape(4,Train_Num,1)
5: # y_train_R_Grid=y_train_smallLSTM.reshape(4,Train_Num,1)
6:
7:
8: #K.clear_session()
9:
10: model = Sequential()
11: #model.add(Reshape((2,Train_Num,), input_shape=(1,2*Train_Num,)))
12: model.add(Bidirectional(LSTM(250, return_sequences=True,activation='relu'),input_shape=(Train_Num,1)))
13: model.add(Dropout(space['learning_rate']))
14: model.add(Bidirectional(LSTM(170, return_sequences=True,activation='relu')))
15: model.add(Dropout(space['activation']))
16: model.add(Bidirectional(LSTM(25, return_sequences=True, activation='relu')))
17: model.add(Dropout(space['loss']))
18: #model.add(Flatten())
19: model.add(Dense(1))
20: model.compile(optimizer='Nadam',loss='logcosh')
21:
22: model.fit(x_train, y_train,
23: batch_size=space['Dropout'],
24: epochs=15,
25: show_accuracy=True,
26: verbose=2,
27: validation_split=0.1)
28:
29: validation_acc = np.amax(result.history['mse'])
30: print('Best validation acc of epoch:', validation_acc)
31:
32: return {'loss': mse, 'status': STATUS_OK, 'model': model}
33:
0%| | 0/5 [00:00<?, ?it/s, best loss: ?]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-60-033c997d802b> in <module>
37 max_evals=5,
38 trials=Trials(),
---> 39 notebook_name='EscapeKanji')
40 #encoding='utf-8
41 x_train, y_train, x_test, y_test = LSTM_Hyperas_Data()
~\Anaconda3\envs\Tensorflow\lib\site-packages\hyperas\optim.py in minimize(model, data, algo, max_evals, trials, functions, rseed, notebook_name, verbose, eval_space, return_space, keep_temp)
67 notebook_name=notebook_name,
68 verbose=verbose,
---> 69 keep_temp=keep_temp)
70
71 best_model = None
~\Anaconda3\envs\Tensorflow\lib\site-packages\hyperas\optim.py in base_minimizer(model, data, functions, algo, max_evals, trials, rseed, full_model_string, notebook_name, verbose, stack, keep_temp)
137 trials=trials,
138 rstate=np.random.RandomState(rseed),
--> 139 return_argmin=True),
140 get_space()
141 )
~\Anaconda3\envs\Tensorflow\lib\site-packages\hyperopt\fmin.py in fmin(fn, space, algo, max_evals, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin, points_to_evaluate, max_queue_len, show_progressbar)
386 catch_eval_exceptions=catch_eval_exceptions,
387 return_argmin=return_argmin,
--> 388 show_progressbar=show_progressbar,
389 )
390
~\Anaconda3\envs\Tensorflow\lib\site-packages\hyperopt\base.py in fmin(self, fn, space, algo, max_evals, rstate, verbose, pass_expr_memo_ctrl, catch_eval_exceptions, return_argmin, show_progressbar)
637 catch_eval_exceptions=catch_eval_exceptions,
638 return_argmin=return_argmin,
--> 639 show_progressbar=show_progressbar)
640
641
~\Anaconda3\envs\Tensorflow\lib\site-packages\hyperopt\fmin.py in fmin(fn, space, algo, max_evals, trials, rstate, allow_trials_fmin, pass_expr_memo_ctrl, catch_eval_exceptions, verbose, return_argmin, points_to_evaluate, max_queue_len, show_progressbar)
405 show_progressbar=show_progressbar)
406 rval.catch_eval_exceptions = catch_eval_exceptions
--> 407 rval.exhaust()
408 if return_argmin:
409 return trials.argmin
~\Anaconda3\envs\Tensorflow\lib\site-packages\hyperopt\fmin.py in exhaust(self)
260 def exhaust(self):
261 n_done = len(self.trials)
--> 262 self.run(self.max_evals - n_done, block_until_done=self.asynchronous)
263 self.trials.refresh()
264 return self
~\Anaconda3\envs\Tensorflow\lib\site-packages\hyperopt\fmin.py in run(self, N, block_until_done)
225 else:
226 # -- loop over trials and do the jobs directly
--> 227 self.serial_evaluate()
228
229 try:
~\Anaconda3\envs\Tensorflow\lib\site-packages\hyperopt\fmin.py in serial_evaluate(self, N)
139 ctrl = base.Ctrl(self.trials, current_trial=trial)
140 try:
--> 141 result = self.domain.evaluate(spec, ctrl)
142 except Exception as e:
143 logger.info('job exception: %s' % str(e))
~\Anaconda3\envs\Tensorflow\lib\site-packages\hyperopt\base.py in evaluate(self, config, ctrl, attach_attachments)
842 memo=memo,
843 print_node_on_error=self.rec_eval_print_node_on_error)
--> 844 rval = self.fn(pyll_rval)
845
846 if isinstance(rval, (float, int, np.number)):
~\temp_model.py in keras_fmin_fnct(space)
~\Anaconda3\envs\Tensorflow\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name +
90 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~\Anaconda3\envs\Tensorflow\lib\site-packages\keras\layers\core.py in __init__(self, rate, noise_shape, seed, **kwargs)
100 def __init__(self, rate, noise_shape=None, seed=None, **kwargs):
101 super(Dropout, self).__init__(**kwargs)
--> 102 self.rate = min(1., max(0., rate))
103 self.noise_shape = noise_shape
104 self.seed = seed
TypeError: '>' not supported between instances of 'str' and 'float'
Is there any known fix to that. Much THX in advance!
Best regards, Tobias