两层全连接,固定学习率的mnist数据集各优化器对比,结果在注释中,仅供参考

注意执行之前关掉所有其他无关的应用, 将内存腾出来, 我的是8G内存, RTX2060结果头一层的2000个unit的全连接没法跑, 所以减少到1000, 注意内存不足的情况.

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import time
# 载入数据


def time_count(func):
    print("开启装饰器")
    def call_func(*args, **kwargs):
        strat = time.time()
        func(*args, **kwargs)
        finish = time.time()
        print("此优化器的执行时间为:%f" % (finish-strat))
    return call_func

@time_count
def main(optimizer):
    mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
    # 批次的大小
    batch_size = 128
    n_batch = mnist.train.num_examples // batch_size

    x = tf.placeholder(tf.float32, [None,784])
    y = tf.placeholder(tf.float32, [None, 10])
    keep_prob = tf.placeholder(tf.float32)

    # 创建神经网络
    W1 = tf.Variable(tf.truncated_normal([784,1000],stddev=0.1))
    b1 = tf.Variable(tf.zeros([1000]))
    # 激活层
    layer1 = tf.nn.tanh(tf.matmul(x,W1) + b1)
    # drop层
    layer1 = tf.nn.dropout(layer1,keep_prob=keep_prob)

    # 第二层
    W2 = tf.Variable(tf.truncated_normal([1000,500],stddev=0.1))
    b2 = tf.Variable(tf.zeros([1, 500]))
    layer2 = tf.nn.tanh(tf.matmul(layer1,W2) + b2)
    layer2 = tf.nn.dropout(layer2,keep_prob=keep_prob)

    # 第三层
    W3 = tf.Variable(tf.truncated_normal([500,10],stddev=0.1))
    b3 = tf.Variable(tf.zeros([1,10]))
    prediction = tf.matmul(layer2,W3) + b3

    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))

    # 梯度下降法
    train_step = optimizer.minimize(loss)


    # 初始化变量
    init = tf.global_variables_initializer()

    prediction_2 = tf.nn.softmax(prediction)
    # 得到一个布尔型列表,存放结果是否正确
    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(prediction_2,1)) #argmax 返回一维张量中最大值索引

    # 求准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) # 把布尔值转换为浮点型求平均数

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as sess:
        sess.run(init)
        for epoch in range(30):
            for batch in range(n_batch):
                # 获得批次数据
                batch_xs, batch_ys = mnist.train.next_batch(batch_size)
                sess.run(train_step, feed_dict={x:batch_xs, y:batch_ys, keep_prob:0.8})
            acc = sess.run(accuracy, feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0} )
            print("Iter " + str(epoch) + " Testing Accuracy: " + str(acc))



if __name__ == '__main__':
    """
    在相同学习率的情况下比对各个优化器的迭代速度和准确率(利用装饰器)
    """
    # 梯度下降法
    train_step1 = tf.train.GradientDescentOptimizer(0.01)  # Iter 29 Testing Accuracy: 0.9607 此优化器的执行时间为:65.501123


    train_step2 = tf.train.AdadeltaOptimizer(0.01)         #Iter 29 Testing Accuracy: 0.934 此优化器的执行时间为:38.532741

    train_step3 = tf.train.AdamOptimizer()                  #Iter 29 Testing Accuracy: 0.9822 此优化器的执行时间为:38.130356

    train_step4 = tf.train.RMSPropOptimizer(learning_rate=0.001) #Iter 29 Testing Accuracy: 0.9829 此优化器的执行时间为:39.639571

    train_step5 = tf.train.AdagradOptimizer(learning_rate=0.01) #Iter 29 Testing Accuracy: 0.9729 此优化器的执行时间为:36.045025



    train_step7 = tf.train.MomentumOptimizer(0.01, momentum = 0.7) #Iter 29 Testing Accuracy: 0.9765 此优化器的执行时间为:36.148152

    main(train_step7)

你可能感兴趣的:(深度学习)