MNIST数据集在TensorFlow具有两个隐藏层的神经网络的表现

发布于 / 机器学习 / 0 条评论

引入相关库

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import gzip
import os
import tempfile
import numpy as np
from six.moves import urllib
from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
import matplotlib.pyplot as plt
mnist = read_data_sets("MNIST/", one_hot=True)

一个隐藏层的模型:

784->50->10

x = tf.placeholder(tf.float32, [None, 784]) #输入的X
#输入层-隐藏层权重和bias
w1 = tf.Variable(tf.truncated_normal([784, 50], stddev=0.1))              #权重矩阵
b1 = tf.Variable(tf.constant(0.1), [50])
#隐藏层-输出层权重
w2 = tf.Variable(tf.truncated_normal([50, 10], stddev=0.1))
b2 = tf.Variable(tf.constant(0.1), [10])
#隐藏层
hidden = tf.matmul(x, w1)+b1
hidden_y=tf.nn.tanh(hidden)
evidance=tf.matmul(hidden_y, w2)+b2
y = tf.nn.softmax(evidance)
y_ = tf.placeholder(tf.float32, [None,10]) 
#loss = tf.reduce_sum(tf.square(y-y_))               #损失函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_, logits = y))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

运行&测试

#运行&求准确度
sess = tf.Session()
sess.run(tf.initialize_all_variables())
for i in range(10000):
    batch_xs, batch_ys = mnist.train.next_batch(50)
    sess.run(train_step, feed_dict={ x:batch_xs, y_:batch_ys })
    if i % 1000 == 0:
        print("loss:", sess.run(loss, feed_dict={ x:batch_xs, y_:batch_ys }))
        
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
            #判断是否相等(是否正确)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
            #reduce_mean用来求平均值。cast用来布尔转浮点(true=>1)
#sess.run(tf.initialize_all_variables())
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

结果:

blob.png

准确率大约93.93%

两个隐藏层的模型:

784->100->50->10

x = tf.placeholder(tf.float32, [None, 784]) #输入的X
#输入层-隐藏层1的权重
w1 = tf.Variable(tf.truncated_normal([784, 100], stddev=0.1))              #权重矩阵
b1 = tf.Variable(tf.constant(0.1), [100])
#隐藏层1-隐藏层2的权重
w2 = tf.Variable(tf.truncated_normal([100, 50], stddev=0.1))
b2 = tf.Variable(tf.constant(0.1), [50])
#隐藏层2-输出层的权重
w3 = tf.Variable(tf.truncated_normal([50, 10], stddev=0.1))
b3 = tf.Variable(tf.constant(0.1), [10])
#隐藏层1
hidden_1 = tf.matmul(x, w1)+b1
hidden_1_y=tf.nn.tanh(hidden_1)
#隐藏层2
hidden_2 = tf.matmul(hidden_1_y, w2)+b2
hidden_2_y=tf.nn.tanh(hidden_2)
#输出层
evidance = tf.matmul(hidden_2_y, w3)+b3
y = tf.nn.softmax(evidance)
y_ = tf.placeholder(tf.float32, [None,10]) 
#loss = tf.reduce_sum(tf.square(y-y_))               #损失函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_, logits = y))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

运行&测试

#运行&求准确度
sess = tf.Session()
sess.run(tf.initialize_all_variables())
for i in range(10000):
    batch_xs, batch_ys = mnist.train.next_batch(50)
    sess.run(train_step, feed_dict={ x:batch_xs, y_:batch_ys })
    if i % 1000 == 0:
        print("loss:", sess.run(loss, feed_dict={ x:batch_xs, y_:batch_ys }))
        
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
            #判断是否相等(是否正确)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
            #reduce_mean用来求平均值。cast用来布尔转浮点(true=>1)
#sess.run(tf.initialize_all_variables())
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

结果:

blob.png

准确率大约94.87%

转载原创文章请注明,转载自: 斐斐のBlog » MNIST数据集在TensorFlow具有两个隐藏层的神经网络的表现
目前还没有评论,快来抢沙发吧~