keras 如何用运行循环获得的值更新 Dataframe ?

nqwrtyyt  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(143)

目前运行keras模型,为了分析算法参数变化的最终输出,我尝试在循环内运行模型,并使用所需输出(损失)更新 Dataframe
请参阅代码。
输出包含列名的空 Dataframe

epochs = [1,5,10,15,20,25,30]
batch_sizes = [64,128,256,512]
modeldata = pd.DataFrame()

for e in epochs:
    
    modeldata['Epochs'] = e
    
    for bs in batch_sizes:
        
        modeldata['Batch Size'] = bs
    
        training = mod_nvp.fit(
        x_train, y_train,    
        batch_size = bs, 
        epochs = e,
        validation_split = 0.2,
        verbose = 0
    )
        y_pred = mod_nvp.predict(x_test, verbose = 0) 
        
        modeldata['Loss'] = custom_loss_nvp1(y_test,y_pred)

        #modeldata['Training Loss'] = np.sum(training.history['loss'])
        #modeldata['Test Loss'] = np.sum(training.history['val_loss'])
    
        print('current running epoch',e,'with batchsize',bs)

`

xhv8bpkk

xhv8bpkk1#

我对代码做了这样的编辑,它完成了所需的工作

for e in epochs:

    for bs in batch_sizes:

        training = mod_nvp.fit(
        x_train, y_train,    
        batch_size = bs, 
        epochs = e,
        validation_split = 0.2,
        verbose = 0
       )
        y_pred = mod_nvp.predict(x_test, verbose = 0) 
    
        #train_loss = np.sum(training.history['loss'])
        #test_loss = np.sum(training.history['val_loss'])
    
        inv_cost = custom_loss_nvp1(y_test,y_pred)
    
        l = pd.DataFrame([e, bs, inv_cost, train_loss, test_loss])
    
        modeldata = pd.concat([modeldata,l], axis=1)
    
        #print('trained epoch-',e,'with batch size-',bs)

相关问题