我正在尝试对神经网络中的多类问题进行网格搜索。 我无法获得最佳参数,内核继续编译。 我的代码有问题吗?请帮忙
import keras
from keras.models import Sequential
from keras.layers import Dense
# defining the baseline model:
def neural(output_dim=10,init_mode='glorot_uniform'):
model = Sequential()
model.add(Dense(output_dim=output_dim,
input_dim=2,
activation='relu',
kernel_initializer= init_mode))
model.add(Dense(output_dim=output_dim,
activation='relu',
kernel_initializer= init_mode))
model.add(Dense(output_dim=3,activation='softmax'))
# Compile model
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV
estimator = KerasClassifier(build_fn=neural,
epochs=5,
batch_size=5,
verbose=0)
# define the grid search parameters
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
init_mode = ['uniform', 'lecun_uniform', 'normal', 'zero',
'glorot_normal', 'glorot_uniform', 'he_normal', 'he_uniform']
output_dim = [10, 15, 20, 25, 30,40]
param_grid = dict(batch_size=batch_size,
epochs=epochs,
output_dim=output_dim,
init_mode=init_mode)
grid = GridSearchCV(estimator=estimator,
scoring= 'accuracy',
param_grid=param_grid,
n_jobs=-1,cv=5)
grid_result = grid.fit(X_train, Y_train)
# 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, stdev, param))
答案 0 :(得分:0)
您的代码中没有错误。
您当前的参数网格有864种不同的参数组合。
('batch_size'
中的'epochs'
×3值'init_mode'
×8 'output_dim'
×6 cv=5
中的6个值= 864
GridSearchCV将迭代所有这些可能性,您的估算器将被多次克隆。由于您已设置verbose
,因此再次重复5次。
因此,您的模型将被克隆(编译并根据可能性设置参数)总共864 x 5 = 4320次。
因此,您会在输出中看到模型正在多次编译。
要检查GridSearchCV是否正常工作,请使用其grid = GridSearchCV(estimator=estimator,
scoring= 'accuracy',
param_grid=param_grid,
n_jobs=1,cv=5, verbose=3)
参数。
import mysql.connector
import sys
from PIL import Image
import base64
import six
import io
import PIL.Image
import pymysql
from flask import Flask,render_template,request,flash
app=Flask(__name__)
app.secret_key = 'dont tell anyone'
@app.route('/')
def index():
db = mysql.connector.connect(user='root', password='tejA@1612',
host='localhost',
database='students')
#photo = request.form['inputFile']
image = Image.open('statics/rohan2.jpg')
blob_value = open('statics/rohan2.jpg', 'rb').read()
sql = 'INSERT INTO images(photo) VALUES(%s)'
args = (blob_value, )
cursor=db.cursor()
cursor.execute(sql,args)
sql1='select photo from images'
db.commit()
cursor.execute(sql1)
data=cursor.fetchall()
#print type(data[0][0])
file_like=io.BytesIO(data[0][0])
img=PIL.Image.open(file_like)
#img.show()
db.close()
return render_template("home.html",result=img)
if __name__=="__main__":
app.run(debug=True)
这将打印当前可能尝试的参数,cv迭代,适合它的时间,当前准确度等。