Keras基础自学五(构建基准模型)

from sklearn import datasets#sklearn提供的自带的数据集
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold

# 导入数据
dataset = datasets.load_iris()

x = dataset.data
Y = dataset.target

# 设定随机种子
seed = 7
np.random.seed(seed)

# 构建模型函数
def create_model(optimizer='adam', init='glorot_uniform'):
    model = Sequential()#构建顺序模型
    model.add(Dense(units=4, activation='relu', input_dim=4, kernel_initializer=init))
    model.add(Dense(units=6, activation='relu', kernel_initializer=init))
    model.add(Dense(units=3, activation='softmax', kernel_initializer=init))
    # 编译模型
    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    return model

model = KerasClassifier(build_fn=create_model, epochs=150, batch_size=5, verbose=0)
kfold = KFold(n_splits=10, shuffle=True, random_state=seed)
results = cross_val_score(model, x, Y, cv=kfold)
print('Accuracy: %.2f%% (%.2f)' % (results.mean()*100, results.std()))


'''
Sequential 顺序模型
Dense 全连接层  
input_dim 输入层(已知X)
units 隐藏层(求未知数W)
kernel_initializer权重初始化 =>glorot_uniform
model.add第一个是输入层,中间的全部是隐藏层,最后一个是输出层

 

Keras基础自学五(构建基准模型)Keras基础自学五(构建基准模型) gamblerofdestinyR14 发布了38 篇原创文章 · 获赞 1 · 访问量 733 私信 关注
上一篇:FE.TEST-前端测试初探


下一篇:两个SQL查询,横向合并为一个查询结果