本小节实际上就是用KNN、SVM、MLP、CNN识别mnist数据集,实际上这些在《web安全之机器学习入门》中都有讲过,而这些也是非常基础的应用。
一、KNN
整体设计如下
源码如下所示
def do_knn_1d(x_train, y_train,x_test, y_test):
print ("KNN and 1d")
clf = neighbors.KNeighborsClassifier(n_neighbors=15)
print (clf)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
print (metrics.accuracy_score(y_test, y_pred))
二、SVM
整体设计如下
源码如下
def do_svm_1d(x_train, y_train,x_test, y_test):
print ("SVM and 1d")
clf = svm.SVC(decision_function_shape='ovo')
print (clf)
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
print (metrics.accuracy_score(y_test, y_pred))
三、MLP
整体设计如下
源码如下
def do_dnn_1d(X, Y, testX, testY ):
print ("DNN and 1d")
# Building deep neural network
input_layer = tflearn.input_data(shape=[None, 784])
dense1 = tflearn.fully_connected(input_layer, 64, activation='tanh',
regularizer='L2', weight_decay=0.001)
dropout1 = tflearn.dropout(dense1, 0.8)
dense2 = tflearn.fully_connected(dropout1, 64, activation='tanh',
regularizer='L2', weight_decay=0.001)
dropout2 = tflearn.dropout(dense2, 0.8)
softmax = tflearn.fully_connected(dropout2, 10, activation='softmax')
# Regression using SGD with learning rate decay and Top-3 accuracy
sgd = tflearn.SGD(learning_rate=0.1, lr_decay=0.96, decay_step=1000)
top_k = tflearn.metrics.Top_k(3)
net = tflearn.regression(softmax, optimizer=sgd, metric=top_k,
loss='categorical_crossentropy')
# Training
model = tflearn.DNN(net, tensorboard_verbose=0)
model.fit(X, Y, n_epoch=10, validation_set=(testX, testY),
show_metric=True, run_id="mnist")
四、CNN
设计如下
源码如下
def do_cnn_2d(X, Y, testX, testY ):
# Building convolutional network
network = input_data(shape=[None, 28, 28, 1], name='input')
network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = fully_connected(network, 128, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 256, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 10, activation='softmax')
network = regression(network, optimizer='adam', learning_rate=0.01,
loss='categorical_crossentropy', name='target')
# Training
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit({'input': X}, {'target': Y}, n_epoch=5,
validation_set=({'input': testX}, {'target': testY}),
snapshot_step=100, show_metric=True, run_id='mnist')
五、总结
识别mnist数据集较基础,深度学习的识别结果稍好。