当前位置:   article > 正文

tensorflow搭建简单神经网络进行MNIST数据集分类_使用自构建网络对mnist分类

使用自构建网络对mnist分类

tensorflow搭建简单神经网络进行MNIST数据集分类

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#载入数据集
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

#创建简单的神经网络
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W) + b)

#二次迭代代价函数
loss = tf.reduce_mean(tf.square(y-prediction))

#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

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

#结果存放在一个布尔类型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #argmax返回一维张量中最大值所在的位置

#求准确度
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) #先用cast将布尔类型转化为float32类型,然后求平均值

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(21):
        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})
        
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
        print('Iter' + str(epoch) + ', Testing Accuracy' + str(acc))
        
    acc_train = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels})    
    acc_test = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
    print('Training Accuracy:' + str(acc_train))
    print('Testing Accuracy:' + str(acc_test))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

输出结果:

Iter0, Testing Accuracy0.8318
Iter1, Testing Accuracy0.8696
Iter2, Testing Accuracy0.8813
Iter3, Testing Accuracy0.8876
Iter4, Testing Accuracy0.8942
Iter5, Testing Accuracy0.8972
Iter6, Testing Accuracy0.8995
Iter7, Testing Accuracy0.9013
Iter8, Testing Accuracy0.9039
Iter9, Testing Accuracy0.9052
Iter10, Testing Accuracy0.906
Iter11, Testing Accuracy0.9066
Iter12, Testing Accuracy0.9078
Iter13, Testing Accuracy0.9089
Iter14, Testing Accuracy0.91
Iter15, Testing Accuracy0.9105
Iter16, Testing Accuracy0.9113
Iter17, Testing Accuracy0.9127
Iter18, Testing Accuracy0.9124
Iter19, Testing Accuracy0.9133
Iter20, Testing Accuracy0.9142
Training Accuracy:0.90907276
Testing Accuracy:0.9142
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

调参后增加精度:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#载入数据集
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

#创建简单的神经网络
W_l1 = tf.Variable(tf.zeros([784,100]))
b_l1 = tf.Variable(tf.random_normal([100]))
l1 = tf.nn.relu(tf.matmul(x,W_l1) + b_l1)

W_l2 = tf.Variable(tf.random_normal([100,10]))
b_l2 = tf.Variable(tf.random_normal([10]))
prediction = tf.nn.softmax(tf.matmul(l1,W_l2) + b_l2)

#二次迭代代价函数
loss = tf.reduce_mean(tf.square(y-prediction))

#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

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

#结果存放在一个布尔类型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #argmax返回一维张量中最大值所在的位置

#求准确度
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) #先用cast将布尔类型转化为float32类型,然后求平均值

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(150):
        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})
            
#        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
#        print('Iter' + str(epoch) + ', Testing Accuracy' + str(acc))
#        
    acc_train = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels})    
    acc_test = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
    print('Training Accuracy:' + str(acc_train))
    print('Testing Accuracy:' + str(acc_test))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

输出结果:

Training Accuracy:0.9825818
Testing Accuracy:0.9605
  • 1
  • 2

如果采用交叉熵来定义loss函数:

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

  • 1
  • 2

结果:

Training Accuracy:0.9876
Testing Accuracy:0.9661
  • 1
  • 2

使用截断正态分布来初始化参数

W_l1 = tf.Variable(tf.truncated_normal([784,100],stddev=0.1))
  • 1

tf.truncated_normal(shape, mean, stddev)
shape:表示生成张量的维度
mean:是均值
stddev:是标准差。
这个函数产生正太分布,均值和标准差自己设定。这是一个截断的产生正太分布的函数,就是说产生正太分布的值如果与均值的差值大于两倍的标准差,那就重新生成。

结果:

Training Accuracy:0.9872909
Testing Accuracy:0.9672
  • 1
  • 2

调参:batch_size、网络层数结构、损失函数、迭代法、学习率、dropout

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#载入数据集
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size

#定义placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob1 = tf.placeholder(tf.float32)
keep_prob2 = tf.placeholder(tf.float32)

lr = tf.Variable(0.001, dtype=tf.float32)

#创建简单的神经网络
W_l1 = tf.Variable(tf.truncated_normal([784,500],stddev=0.1))
b_l1 = tf.Variable(tf.zeros([500])+0.1)
l1 = tf.nn.tanh(tf.matmul(x,W_l1) + b_l1)
l1_drop = tf.nn.dropout(l1,keep_prob1)

W_l2 = tf.Variable(tf.truncated_normal([500,300],stddev=0.1))
b_l2 = tf.Variable(tf.zeros([300])+0.1)
l2 = tf.nn.tanh(tf.matmul(l1_drop,W_l2) + b_l2)
l2_drop = tf.nn.dropout(l2,keep_prob2)

W_l3 = tf.Variable(tf.truncated_normal([300,10]))
b_l3 = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(l2_drop,W_l3) + b_l3)


#交叉熵代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))

#使用adam迭代
train_step = tf.train.AdamOptimizer(lr).minimize(loss)

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

#结果存放在一个布尔类型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #argmax返回一维张量中最大值所在的位置

#求准确度
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) #先用cast将布尔类型转化为float32类型,然后求平均值

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(50):
        sess.run(tf.assign(lr, 0.001*(0.95**epoch)))
        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_prob1:1,keep_prob2:1})
        learning_rate = sess.run(lr)    
        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob1:1,keep_prob2:1})
        print('Iter' + str(epoch) + ', Testing Accuracy' + str(acc) + ' , learning_rate:' + str(learning_rate))
        
    acc_train = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob1:1,keep_prob2:1})    
    acc_test = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob1:1,keep_prob2:1})
    print('Training Accuracy:' + str(acc_train))
    print('Testing Accuracy:' + str(acc_test))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/350213
推荐阅读
相关标签
  

闽ICP备14008679号