《Web安全之机器学习入门》笔记:第十五章 15.5 TensorFlow多层感知机识别验证码(二)

本文阅读 1 分钟

本小节相对于15.4节,使用多层感知机来进行处理验证码。

一、多层感知机处理流程图如下所示

img

二、参数定义

由于使用多层感知机,故而参数需定义为W1,b1, W2,b2,这里隐藏层配置为300个神经网络节点。

in_units=784
h1_units=300
W1=tf.Variable(tf.truncated_normal([in_units,h1_units],stddev=0.1))
b1=tf.Variable(tf.zeros([h1_units]))
W2=tf.Variable(tf.zeros([h1_units,10]))
b2=tf.Variable(tf.zeros([10]))

整体函数如下所示

hidden1=tf.nn.relu(tf.matmul(x,W1)+b1)
hidden1_drop=tf.nn.dropout(hidden1,keep_prob)
y = tf.nn.softmax(tf.matmul(hidden1_drop,W2) + b2)

 在配置dropout时,运行时会报警

Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.

这是因为随着版本的升级,dropout函数中keep_prob 参数 是一个已经“deprecated”的参数,这个参数后边版本就不要了,它现在还能用,目前它的功能要参考rate。出错的语句如下所示:

keep_prob=tf.placeholder(tf.float32)
hidden1_drop=tf.nn.dropout(hidden1, keep_prob)

想让不报警,可以进行如下修改:

keep_prob=tf.placeholder(tf.float32)
hidden1_drop=tf.nn.dropout(hidden1, rate = 1- keep_prob)

或者进行如下修改(不过配套的keep_prob相关都要修改为drop_prob相关) 

drop_prob=tf.placeholder(tf.float32)
hidden1_drop=tf.nn.dropout(hidden1, rate = drop_prob)

三、 完整代码

import tensorflow as tf
import pickle
import gzip
import tensorflow.compat.v1 as tf

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def get_one_hot(x,size=10):
    v=[]
    for x1 in x:
        x2=[0]*size
        x2[(x1-1)]=1
        v.append(x2)
    return v

def load_data():
    with gzip.open('../data/MNIST/mnist.pkl.gz') as fp:
        training_data, valid_data, test_data = pickle.load(fp, encoding="bytes")
    return training_data, valid_data, test_data

training_data, valid_data, test_dat=load_data()

x_training_data,y_training_data=training_data
x1,y1=test_dat

y_training_data=get_one_hot(y_training_data)
y1=get_one_hot(y1)


batch_size=100

in_units=784
h1_units=300
W1=tf.Variable(tf.truncated_normal([in_units,h1_units],stddev=0.1))
b1=tf.Variable(tf.zeros([h1_units]))
W2=tf.Variable(tf.zeros([h1_units,10]))
b2=tf.Variable(tf.zeros([10]))

x = tf.placeholder(tf.float32, [None, in_units])

hidden1=tf.nn.relu(tf.matmul(x,W1)+b1)

keep_prob=tf.placeholder(tf.float32)
hidden1_drop=tf.nn.dropout(hidden1, rate = 1- keep_prob)


y = tf.nn.softmax(tf.matmul(hidden1_drop,W2) + b2)
y_ = tf.placeholder(tf.float32, [None,10])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)



for i in range(int(len(x_training_data)/batch_size)):
    batch_xs=x_training_data[(i*batch_size):((i+1)*batch_size)]
    batch_ys=y_training_data[(i*batch_size):((i+1)*batch_size)]

    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys,keep_prob:0.75})


correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print(sess.run(accuracy, feed_dict={x: x1, y_: y1,keep_prob:1.0}))

四、运行结果

0.956
本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/mooyuan/article/details/122766880
-- 展开阅读全文 --
Web安全—逻辑越权漏洞(BAC)
« 上一篇 03-13
Redis底层数据结构--简单动态字符串
下一篇 » 04-10

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复