In [2]:
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score, KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
In [7]:
from sklearn.datasets import load_boston
boston = load_boston()
X = boston.data
y = boston.target
In [16]:
def base_model():
model = Sequential()
model.add(Dense(X.shape[1], input_dim=X.shape[1], kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
model.compile(loss='mse', optimizer='adam')
return model
In [20]:
np.random.seed(7)
model = KerasRegressor(build_fn=base_model, epochs=100, batch_size=5, verbose=0)
pipeline = Pipeline([
("scaler", StandardScaler()),
('reg', model)
])
kfold = KFold(n_splits=10, random_state=7)
results = cross_val_score(pipeline, X, y, cv=kfold)
print(results.mean(), results.std())
In [1]:
def smaller_model():
model = Sequential()
model.add(Dense(int(X.shape[1]/2), input_dim=X.shape[1], kernel_initializer='normal', activation='relu' ))
model.add(Dense(1, kernel_initializer='normal'))
model.compile(loss='mse', optimizer='adam')
return model
In [9]:
np.random.seed(7)
model = KerasRegressor(build_fn=smaller_model, epochs=100, batch_size=5, verbose=0)
pipeline = Pipeline([
('scaler', StandardScaler()),
('reg', model)
])
kfold = KFold(n_splits=10, random_state=7)
results = cross_val_score(pipeline, X, y, cv=kfold)
print(results.mean(), results.std())
In [10]:
def larger_model():
model = Sequential()
model.add(Dense(X.shape[1] * 2, input_dim=X.shape[1], kernel_initializer='normal', activation='relu' ))
model.add(Dense(1, kernel_initializer='normal'))
model.compile(loss='mse', optimizer='adam')
return model
In [11]:
model = KerasRegressor(build_fn=larger_model, epochs=100, batch_size=5, verbose=0)
pipeline = Pipeline([
('scaler', StandardScaler()),
('reg', model)
])
kfold = KFold(n_splits=10, random_state=7)
results = cross_val_score(pipeline, X, y, cv=kfold)
print(results.mean(), results.std())
In [17]:
def wider_model():
model = Sequential()
model.add(Dense(X.shape[1], input_dim=X.shape[1], kernel_initializer='normal', activation='relu' ))
model.add(Dense(int(X.shape[1]/2), kernel_initializer='normal', activation='relu' ))
model.add(Dense(1, kernel_initializer='normal'))
model.compile(loss='mse', optimizer='adam')
return model
In [18]:
model = KerasRegressor(build_fn=wider_model, epochs=100, batch_size=5, verbose=0)
pipeline = Pipeline([
('scaler', StandardScaler()),
('reg', model)
])
kfold = KFold(n_splits=10, random_state=7)
results = cross_val_score(pipeline, X, y, cv=kfold)
print(results.mean(), results.std())