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

时间:2017-02-01 11:45:24

标签: scikit-learn keras

我使用了Keras回归量来对数据进行回归。我使用Scikit-learn包装器和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()

我无法找到好的文档。

1 个答案:

答案 0 :(得分:0)

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