问题描述
我正在尝试同时使用Keras和SKlearn优化神经网络的超参数,我正在用KerasRegressor包装我的模型,因为这是一个回归问题。我正在尝试优化各种参数:
- 隐藏层数
- 每个隐藏层的神经元数量
- 优化程序
- 纪元数和Batch_Size
- 激活功能
我可以单独执行此操作,但我想尝试同时运行所有内容和所有参数:)来查看是否有可能进行全面比较,但我不断遇到以下错误。
错误
PicklingError: Could not pickle the task to send it to the workers.
在这里您可以在下面找到我的代码
def build_regressor(optimizer='adam',activation = 'relu',hidden_layers=1,neurons=1):
# Initialize the constructor
regressor = Sequential()
# Add an input layer
regressor.add(Dense(neurons,activation=activation,input_shape = (x_train.shape[1],)))
for i in range(hidden_layers):
# Add one hidden layer
regressor.add(Dense(neurons,activation=activation))
# Add an output layer
regressor.add(Dense(1,activation=activation))
#compile model
regressor.compile(loss='mean_squared_error',optimizer= optimizer,metrics= ['mse','mae'],epochs = epochs,batch_size = batch_size)
return model
#Wrap Model
regressor = KerasRegressor(build_fn=build_regressor,verbose=1)
# define the grid search parameters
batch_size = [10,20,40,60,80,100,150]
epochs = [10,50,150,200]
neurons = [1,32,64,128,256,512]
optimizer = ['SGD','adam']
#activation = ['relu','linear','softmax']
param_grid = dict(batch_size = batch_size,neurons = neurons,optimizer = optimizer)
#implement grid_search
grid = GridSearchCV(estimator=regressor,param_grid=param_grid,n_jobs=-1,cv=3,scoring = 'r2')
grid_result = grid.fit(x_train,y_train)
#Fit Model
results=regressor.fit(x_train,y_train)
y_pred= regressor.predict(x_valid)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_,grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean,stdev,param in zip(means,stds,params):
print("%f (%f) with: %r" % (mean,param))
代码
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)