用Mnist数据集训练神经网络
这篇博客是我在学习用tensorflow搭建神经网络时,所作的一些笔记。搭建的神经网络有两层隐藏层,和输入输出层。采用全连接的方式进行传输,优化算法采用自适用矩估计算法。
1.首先,导入tensorflow官方提供的库
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
2.设置输入:
x = tf.placeholder(tf.float32,[None,784],name='x_input')
y_ = tf.placeholder(tf.float32,[None,10],name='y_input')
mnist数据集中的图片为2828个像素点,并且每次训练都是小批量同时输入,所以输入的维度设置为[None,2828],同时可以用batch_size 代替None。
3.定义参数
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
为了提高代码的可读性和简洁性,定义了两个方法,分别来定义参数w和b。
4.运算
#第一层
net = tf.nn.relu(tf.matmul(x,w1)+b1)
#net = tf.nn.dropout(net,0.25)
#第二层
net = tf.matmul(net,w2)+b2
y = tf.nn.softmax(net)
对于激活函数采用relu,去线性。
5.损失函数和优化算法
loss = -tf.reduce_mean(tf.reduce_sum(y_*tf.log(y)))
optim = tf.train.AdamOptimizer(0.001).minimize(loss)
6.进行批量训练
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(10000):
xs,ys = mnist.train.next_batch(batch_size)
_,l,accu=sess.run([optim,loss,accuracy],feed_dict={x:xs,y_:ys})
if(i%100==0):
print('after %d times,the loss is %g'%(i,l))
print('the modle accuracy is %g '%(accu))
总结
这个代码量和理解并不难,主要用于练手,提高对tensorflow的掌握,熟悉tensorflow的函数。
附上代码
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 3 16:09:17 2019
@author: ASUS
"""
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
batch_size =100
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
x = tf.placeholder(tf.float32,[None,784],name='x_input')
y_ = tf.placeholder(tf.float32,[None,10],name='y_input')
w1 = tf.get_variable('w1',shape=[784,300],initializer=tf.truncated_normal_initializer(stddev=0.1))
b1 = tf.Variable(tf.zeros([300]))
w2 = tf.get_variable('w2',shape=[300,10],initializer=tf.truncated_normal_initializer(stddev=0.1))
b2 = tf.Variable(tf.zeros([10]))
net = tf.nn.relu(tf.matmul(x,w1)+b1)
#net = tf.nn.dropout(net,0.25)
net = tf.matmul(net,w2)+b2
y = tf.nn.softmax(net)
loss = -tf.reduce_mean(tf.reduce_sum(y_*tf.log(y)))
optim = tf.train.AdamOptimizer(0.001).minimize(loss)
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
def train():
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(10000):
xs,ys = mnist.train.next_batch(batch_size)
_,l,accu=sess.run([optim,loss,accuracy],feed_dict={x:xs,y_:ys})
if(i%100==0):
print('after %d times,the loss is %g'%(i,l))
print('the modle accuracy is %g '%(accu))
def main(argv=None):
train()
if __name__ == '__main__':
tf.app.run()