我正在尝试使用Hyperopt库优化一组参数。我按照本教程实现了代码。只要我将max_evals设置为少于 30 次,一切正常。当我将max_evals 设置为 30 时,在第 20 次迭代时出现以下错误:
Traceback(最近一次通话最后):文件“/Users/sulekahelmini/Documents/fyp/fyp_work/MLscripts/Optimizehyperopt.py”,第 149 行,在 trial=trials 中)文件“/Users/sulekahelmini/Documents/fyp/condaEnv/envConda /lib/python3.7/site-packages/hyperopt/fmin.py”,第 482 行,在 fmin show_progressbar=show_progressbar,文件“/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site- packages/hyperopt/base.py”,第 686 行,在 fmin show_progressbar=show_progressbar,文件“/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site-packages/hyperopt/fmin.py”中,第 509 行,在 fmin rval.exhaust() 文件“/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site-packages/hyperopt/fmin.py”中,第 330 行,在排气 self.run (自己。max_evals - n_done,block_until_done=self.asynchronous)文件“/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site-packages/hyperopt/fmin.py”,第 266 行,运行 new_ids,self .domain,试验,self.rstate.randint(2 ** 31 - 1)文件“/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site-packages/hyperopt/tpe.py”,第 939 行,建议 idxs,vals = pyll.rec_eval(posterior, memo=memo, print_node_on_error=False) File "/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site-packages/hyperopt/ pyll/base.py”,第 911 行,在 rec_eval rval = scope._impls[node.name](*args, **kwargs) 文件“/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7 /site-packages/hyperopt/tpe.py”,第 430 行,在adaptive_parzen_normal srtd_mus[:prior_pos] = mus[order[:prior_pos]] TypeError: only integer scalar arrays can be convert to a scalar index
下面显示的是我的代码,我在这里做错了什么?
from hyperopt import hp, tpe, fmin, Trials, STATUS_OK
import hyperopt.pyll.stochastic
import pandas as pd
import sys
import subprocess
import csv
import argparse
space={}
fileName="JVMFlags_GC_Com.csv"
isFirstRun="False"
def ConstructSpace(df):
dependent= None
for row in df.itertuples():
if(row.Type == "int" or row.Type=="positive int"):
if(not (pd.isnull(row.Range))):
split_range = str(row.Range).split("/")
else:
if(sys.maxsize > 2**32):
split_range = str(row.OS_64).split("/")
else:
split_range = str(row.OS_32).split("/")
if(row.Name == "ms"):
dependent = hp.uniform(row.Name,int(split_range[0]),int(split_range[1]))
space[row.Name] = dependent
continue
elif(row.Name == "mx"):
space[row.Name] = hp.uniform(row.Name, dependent, int(split_range[1]))
else:
space[row.Name] = hp.uniform(row.Name,int(split_range[0]),int(split_range[1]))
elif(row.Type == "bool"):
space[row.Name] = hp.choice(row.Name, [True,False])
elif(row.Type =="choice"):
split_range = str(row.Range).split("_")
choice_list=[]
for element in split_range:
choice_list.append(element)
space[row.Name] = hp.choice(row.Name, choice_list)
def WriteFlags(param):
df = pd.read_csv(fileName)
with open("flags.txt", "w") as flag_file:
for row in df.itertuples():
if(row.Type == "bool"):
if(param[row.Name]):
flagName = "-XX:+"+row.Name
else:
flagName = "-XX:-" + row.Name
elif (row.Name == "ms" or row.Name == "mx"):
tempVal= int(float(param[row.Name]))
flagName = "-X" + row.Name + str(tempVal)
elif(row.Type == "int" or row.Type=="positive int" or row.Type =="choice"):
tempVal = int(float(param[row.Name]))
flagName = "-XX:" + row.Name+"="+ str(tempVal)
flag_file.write(flagName+" ")
def WriteCsv(param):
df = pd.read_csv(fileName)
latency_res = pd.read_csv("agg_test.csv")
with open('temp_opt_res.csv', mode='a') as opt_file:
writer = csv.writer(opt_file, delimiter=',')
valuList=[]
for row in df.itertuples():
valuList.append(int(float(param[row.Name])))
bottom = latency_res.tail(1)
valuList.append(bottom["Average"].values[0])
valuList.append(bottom["Median"].values[0])
valuList.append(bottom["90% Line"].values[0])
valuList.append(bottom["95% Line"].values[0])
valuList.append(bottom["99% Line"].values[0])
valuList.append(bottom["Error %"].values[0])
valuList.append(bottom["Throughput"].values[0])
writer.writerow(valuList)
def HyperparameterTuning(param):
WriteFlags(param)
subprocess.check_call(['./microwise.sh', isFirstRun])
WriteCsv(param)
latency_vals = pd.read_csv("agg_test.csv")
bottom = latency_vals.tail(1)
print(bottom["99% Line"].values[0])
return {'loss': bottom["99% Line"].values[0], 'status': STATUS_OK}
if __name__ == "__main__":
df = pd.read_csv(fileName)
ConstructSpace(df)
with open('temp_opt_res.csv', mode='w') as opt_file:
writer = csv.writer(opt_file, delimiter=',')
flagList = df['Name'].tolist()
flagList.append("Average")
flagList.append("Median")
flagList.append("90%")
flagList.append("95%")
flagList.append("99%")
flagList.append("Error%")
flagList.append("Throughput")
writer.writerow(flagList)
# parser = argparse.ArgumentParser(description='Short sample app')
# parser.add_argument('--fileName', action="store", dest='fileName')
# parser.add_argument('--isFirstIteration', action="store", dest='isFirstIteration')
# parser.add_argument('--isFirstRun', action="store", dest='isFirstRun')
# args = parser.parse_args()
#
# fileName=args.fileName
# isFirstIteration=args.isFirstIteration
# isFirstRun=args.isFirstRun
trials = Trials()
best = fmin(fn=HyperparameterTuning,
space=space,
algo=tpe.suggest,
max_evals=30,
trials=trials)
print (best)