如何将Keras模型摘要写入数据帧?

2024-10-03 17:18:47 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我要说这不是正确运行Keras模型的方法。应该有列车和测试装置。任务是严格地发展直觉,所以没有测试集。在

我正在运行一个模型,通过几个神经元的排列,激活函数,批次和层次。这是我使用的代码。在

from sklearn.datasets import make_classification
X1, y1 = make_classification(n_samples=90000, n_features=17, n_informative=6, n_redundant=0, n_repeated=0, n_classes=8, n_clusters_per_class=3, weights=None, flip_y=.3, class_sep=.4, hypercube=False, shift=3, scale=2, shuffle=True, random_state=840780)

class_num = 8

# ----------------------------------------------------------------

import itertools

final_param_list = []

# param_list_gen order is  units, activation function, batch size, layers
param_list_gen = [[10, 20, 50], ["sigmoid", "relu", "LeakyReLU"], [8, 16, 32], [1, 2]]
for element in itertools.product(*param_list_gen):
    final_param_list.append(element)

# --------------------------------------------------------------------------------------

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, LeakyReLU
from keras.callbacks import History
import tensorflow as tf
import numpy as np
import pandas as pd

# --------------------------------------------------------------------------------------



# --------  Model 1 - permutations of neurons, activation funtions batch size and layers -------- #

for param in final_param_list:
    q2model1 = Sequential()

    # hidden layer 1
    q2model1.add(Dense(param[0]))
    if param[1] != 'LeakyReLU':
        q2model1.add(Activation(param[1]))
    else:
        q2model1.add(LeakyReLU(alpha=0.1))

    if param[3] == 2:
        # hidden layer 2
        q2model1.add(Dense(param[0]))
        if param[1] != 'LeakyReLU':
            q2model1.add(Activation(param[1]))
        else:
            q2model1.add(LeakyReLU(alpha=0.1))

    # output layer
    q2model1.add(Dense(class_num, activation='softmax'))

    q2model1.compile(loss='sparse_categorical_crossentropy', optimizer='RMSProp', metrics=['accuracy'])

    # Step 3: Fit the model

    history = q2model1.fit(X1, y1, epochs=20)

似乎工作得很好。现在,我的任务是输出每个历元的精度,包括神经元、激活函数、批处理、层

现在,这给了我每个时代的所有精度

^{pr2}$

这给了我参数

print(param)

这给了我一个总结,尽管我不确定这是否是最好的方法

print(q2model1.summary())

有没有办法把每一个历元都打印到熊猫数据框中,这样看起来像这样?在

阶段(列表索引+1)|#神经元|激活功能|批量大小|层| Acc epoch1 | Acc epoch2 |。。。。。。。。。|附件20

就这样。如果你看到模型本身有什么明显的错误,或者我遗漏了一些关键代码,请让我知道


Tags: from模型importaddparamlayersactivationlist
1条回答
网友
1楼 · 发布于 2024-10-03 17:18:47

你可以试试:

import pandas as pd

# assuming you stored your model.fit results in a 'history' variable:
history = model.fit(x_train, y_train, epochs=20)

# convert the history.history dictionary to a pandas dataframe:     
hist_df = pd.DataFrame(history.history) 

# checkout result with print e.g.:    
print(hist_df)

# or the describe() method:
hist_df.describe()

Keras还有一个CSVLogger:https://keras.io/callbacks/#csvlogger,这可能会引起注意。在

相关问题 更多 >