簡體   English   中英

即使將 Overwrite 標志設置為 true,Kerastuner 搜索也不會重新啟動

[英]Kerastuner search doesn't get restarted even with Overwrite flag set to true

調整如下:

tuner = kt.RandomSearch(
    MyHyperModel(),
    objective="mae",
    max_trials=30,
    overwrite=True,
    directory=results_dir,
    project_name="tune_hypermodel",
)

我正在迭代使用多少功能:

data[name]=pd.read_pickle(os.path.join(root, name)+'/'+name+'.data.reindexed_by_pc.pkl')
CpG_num_lst=[100,500,1000,5000,10000,20000,40000]
train_score, valid_score = [], []
hps_by_CpG_num = []

for CpG_num in CpG_num_lst:
    print("CpG_num:",CpG_num)
    # force overwrite tune search (to start new search). Cause even with overwrite=True it doesn't overwrite
    if  os.path.exists(results_dir+'/tune_hypermodel'):
        shutil.rmtree(results_dir+'/tune_hypermodel')
        
    # initialize 
    X1, y1, X2, y2 = [dict() for _ in range(4)]
    X1[name] = data[name][fold1_ids].head(CpG_num).values.T
    X2[name] = data[name][fold2_ids].head(CpG_num).values.T
    # get the ages of the corresponding persons. Notice info_1 and info_2 only contain "Ctrl" and not "Test" samples
    y1[name] = info_1[name].query("`Train.Test`=='Train'")['Age'].values.astype(float) 
    y2[name] = info_2[name].query("`Train.Test`=='Train'")['Age'].values.astype(float) 
    # Split the data
    X1_train, X1_valid, y1_train, y1_valid = train_test_split(X1[name], y1[name], test_size=0.2, shuffle= True)
    # Grid search
    tuner.search(X1_train, y1_train, validation_data = (X1_valid,y1_valid))
    best_hps = tuner.get_best_hyperparameters(num_trials=1)[0]
    # Get best hyperparameters
    hp_dict = dict()
    hp_dict['num_layers'] = best_hps.get('num_layers')
    hp_dict['batch_size'] = best_hps.get('batch_size')
    hp_dict['act_1'] = best_hps.get('act_1')
    hp_dict['act_2'] = best_hps.get('act_2')
    hp_dict['units_1'] = best_hps.get('units_1')
    hp_dict['units_2'] = best_hps.get('units_2')
    hps_by_CpG_num.append(hp_dict)
    
    # Build best model
    best_model = MyHyperModel().build(hp=best_hps)
    history = best_model.fit(X1_train, y1_train, validation_data = (X1_valid,y1_valid), batch_size=best_hps.get('batch_size'), epochs=200)
    

然而,對於 for 循環中的每個元素,調諧器搜索不會重新開始,它只會使用第一次搜索中的最佳超參數 (CpG_num = 500)

我錯過了什么? 為什么 Keras 使用舊的超參數?

解決方案是在 for 循環中包含調諧器實例化。

tuner = kt.RandomSearch(
    MyHyperModel(),
    objective="mae",
    max_trials=30,
    overwrite=True,
    directory=results_dir,
    project_name="tune_hypermodel",
    )

雖然不知道為什么,但有效......如果有人對此有任何見解,請告訴我。 我以為調諧器會被覆蓋。 有更好的方法嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM