赞
踩
数据集可以从UCI机器学习仓库下载(http://archive.ics.uci.edu/ml/datasets/Iris)
深度学习中要求数据全部都是数据
下例,数据集具有4个数值型输入项目,输出项目是鸢尾花的3个子类。使用scikit-learn中提供的数据集。
输入层(4个输入)—> 隐藏层(4个神经元)—> 隐藏层(6个神经元)—> 输出层(3个输出)
from sklearn import datasets 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'): # 构建模型 *输入层(4个输入)---> 隐藏层(4个神经元)---> 隐藏层(6个神经元)---> 输出层(3个输出) 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=200, 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()))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。