scikit-learn - 如何在 Scikit-learn 管道中访问回归器的权重

标签 scikit-learn keras

我使用 Keras 回归器对数据进行回归拟合。我使用 Scikit-learn wrapper 和 Pipeline 来首先标准化数据,然后将其拟合到 Keras 回归器上。有点像这样:

from sklearn.grid_search import GridSearchCV
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.cross_validation import cross_val_score
from sklearn.cross_validation import KFold
from sklearn.externals import joblib
import cPickle
import pandas as pd
import os
from create_model import *

estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp', KerasRegressor(build_fn=create_model, nb_epoch=50,         batch_size=5, verbose=0, neurons = 1)))
pipeline = Pipeline(estimators)

然后我通过 GridSearchCv 进行网格搜索以获得最佳拟合,并在变量中获得最佳拟合:

batch_size = [60, 80, 100, 200]
epochs = [2, 4, 6, 8, 10, 50]
neurons = np.arange(3,10,1)
optimizer = ['sgd', 'adam', 'rmsprom']
activation = ['relu', 'tanh']
lr = [0.001, 0.01, 0.1]
param_grid = dict(mlp__neurons = neurons, mlp__batch_size = batch_size, mlp__nb_epoch = epochs, mlp__optimizer = optimizer, mlp__activation = activation, mlp__learn_rate = lr)
grid = GridSearchCV(estimator=pipeline, param_grid=param_grid, cv = kfold,scoring='mean_squared_error')
grid_result = grid.fit(X, Y)
clf = []
clf = grid_result.best_estimator_  

clf 变量有 2 个在管道中定义的进程。 我的问题是如何通过 get_params 函数提取 keras 回归量的权重和偏差以获得最佳拟合 (clf)?:

clf.get_params()

我找不到这方面的好文档。

最佳答案

权重 = KerasRegressor.model.layers[0].get_weights()[0] biases = KerasRegressor.model.layers[0].get_weights()[1]

关于scikit-learn - 如何在 Scikit-learn 管道中访问回归器的权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41979003/

相关文章:

tensorflow - 如何使用 LabelBinarizer 解决 2 标签问题?

python - 使用 scikit-learn SVM 和 optunity 时出现“错误的输入形状”

python - sklearn test train split - 获取原始列表文件名的索引

python - `eli5.show_weights` 显示的标准偏差与`feature_importances_std_`中的值不一致

python - 如何在 python 中实现 EM-GMM?

python - Keras - 如何将权重设置为单层

python - 解释多类逻辑回归中的预测概率

python - Keras LSTM 预测时间序列被压缩和移动

python - 无法将大小为 47040000 的数组 reshape 为预训练神经网络的形状 (60000,32,32,1)

python - keras cnn模型仅针对所有测试图像预测一个类别