Suleka_28
Suleka_28

Reputation: 2909

only integer scalar arrays can be converted to a scalar index error in Hyperopt

I am trying to optimize a set of parameters using the Hyperopt library. I implemented the code following this tutorial. Everything works fine as long as I put max_evals to less than 30 runs. When I put max_evals to 30 I get the bellow error at the 20th iteration:

Traceback (most recent call last): File "/Users/sulekahelmini/Documents/fyp/fyp_work/MLscripts/Optimizehyperopt.py", line 149, in trials=trials) File "/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site-packages/hyperopt/fmin.py", line 482, in fmin show_progressbar=show_progressbar, File "/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site-packages/hyperopt/base.py", line 686, in fmin show_progressbar=show_progressbar, File "/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site-packages/hyperopt/fmin.py", line 509, in fmin rval.exhaust() File "/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site-packages/hyperopt/fmin.py", line 330, in exhaust self.run(self.max_evals - n_done, block_until_done=self.asynchronous) File "/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site-packages/hyperopt/fmin.py", line 266, in run new_ids, self.domain, trials, self.rstate.randint(2 ** 31 - 1) File "/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site-packages/hyperopt/tpe.py", line 939, in suggest 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", line 911, in rec_eval rval = scope._impls[node.name](*args, **kwargs) File "/Users/sulekahelmini/Documents/fyp/condaEnv/envConda/lib/python3.7/site-packages/hyperopt/tpe.py", line 430, in adaptive_parzen_normal srtd_mus[:prior_pos] = mus[order[:prior_pos]] TypeError: only integer scalar arrays can be converted to a scalar index

Shown below is my code, What am I doing wrong here?

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)

Upvotes: 1

Views: 398

Answers (1)

quarkz
quarkz

Reputation: 121

I have the same problem as you. For my case, it also happens at the 20th iteration. I later found out that it's because, starting from that iteration, a new combination of input variables has been selected and one of these variables is not a simple number. It could be a 1 value array or list. So please check your input variables. Once that's changed, it will work.

Upvotes: 0

Related Questions