利用DNN训练mnist数据集(1)

"""A very simple MNIST classifier.
See extensive documentation at
https://www.tensorflow.org/get_started/mnist/beginners
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys

from tensorflow.examples.tutorials.mnist import input_data

import tensorflow as tf

alpha = tf.constant(0.0) #为0表示取消惩罚

def weights_variable(n_in, n_out, name_str=None):
    return tf.Variable(tf.truncated_normal([n_in, n_out], stddev=0.1, name=name_str))

def biases_variable(n_out, name_str=None):
    return tf.Variable(tf.truncated_normal([n_out], stddev=0.1, name=name_str))

def calc_penalty(w, penalty="l2"):
    if penalty == "l2":
        return tf.contrib.layers.l2_regularizer(alpha)(w)
    else:
        return tf.contrib.layers.l1_regularizer(alpha)(w)
    
def calc_cell(x, w, b, activate_func=tf.nn.relu):
    if activate_func is None:
        return tf.add(tf.matmul(x, w), b)
    else:
        return activate_func(tf.matmul(x, w) + b + calc_penalty(w))

# Import data
data_dir = './data'
mnist = input_data.read_data_sets(data_dir, one_hot=True)

#添加两层隐含层
x = tf.placeholder(tf.float32, [None, 784])
W1 = weights_variable(784, 600, "w1")
b1 = biases_variable(600, "b1")
h1 = calc_cell(x, W1, b1)

W2 = weights_variable(600, 500, "w2")
b2 = biases_variable(500, "b2")
h2 = calc_cell(h1, W2, b2)

W3 = weights_variable(500, 10, "w3")
b3 = biases_variable(10, "b3")
y = calc_cell(h2, W3, b3, None)

#定义损失和优化器
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))

batch_size = 100
n_batch = mnist.train.num_examples // batch_size #"//"表示取整
global_step = tf.Variable(0)  
"""
指数衰减学习率,步骤:
1.首先使用较大学习率
2.然后通过迭代逐步减少学习率
"""
learning_rate = tf.train.exponential_decay(0.5,global_step,decay_steps=n_batch,decay_rate=0.99,staircase=True)      

train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy, global_step=global_step)

#在一个session里面启动我们的模型并初始化变量
sess = tf.Session()
init_op = tf.global_variables_initializer()
sess.run(init_op)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

#Train
for i in range(5000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
    if i%500 == 0:
        print(i, sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys}))

#Test trained model
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

运行:

0 0.2
500 1.0
1000 1.0
1500 1.0
2000 0.99
2500 1.0
3000 1.0
3500 1.0
4000 1.0
4500 1.0
0.9828

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值