Multi_Layer Perceptron

"""深度学习特点：层数越深，概念越抽象，需要背诵的知识点(神经网络隐含节点)就越少。不过实际应用中，使用层数较深的神经网络会遇到许多困难，比如容易过拟合，参数难以调试，梯度弥散，等等。

"""

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
sess= tf.InteractiveSession()

in_units = 784
h1_units = 300
w1 = tf.Variable(tf.truncated_normal([in_units,h1_units], stddev=0.1))  #将权重初始化为截断的正太分布，其标准差为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])
keep_prob = tf.placeholder(tf.float32)

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)

y_ = tf.placeholder(tf.float32,[None,10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),
reduction_indices=[1]))

tf.global_variables_initializer().run()
for i in range(3000):   #3000个batch.每个batch包含100个样本，一共30万的样本，相当于是对全数据集进行了5轮（epoch)迭代。
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run({x:batch_xs,y_:batch_ys, keep_prob:0.75})   #训练时设为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(accuracy.eval({x:mnist.test.images, y_:mnist.test.labels,keep_prob:1.0}))   #预测时设为1.0



• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120