3

I have used Keras regressor to fit a regression to data. I used Scikit-learn wrapper and Pipeline to first standardize the data and then fit it on Keras regressor. Sort of like this:

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)

I then Grid Search for best fit through GridSearchCv and get a best fit in a variable:

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_  

The clf variable has 2 process as defined in pipeline. My question is how do I extract weights and biases of keras regressor for best fit (clf), through get_params function?:

clf.get_params()

I couldn't find good documentation for this.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.