热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

《Web安全之深度学习实战》笔记:第五章验证码识别

本小节实际上就是用KNN、SVM、MLP、CNN识别mnist数据集,实际上这些在《web安全之机器学习入门》中都有讲过,而这些也是非常基础的应用。一

本小节实际上就是用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 networkinput_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 accuracysgd = 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')# Trainingmodel = 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 networknetwork = 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')# Trainingmodel = 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数据集较基础,深度学习的识别结果稍好。


推荐阅读
author-avatar
DilWilling
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有