最近在学图像分类识别,而说到图像的分类识别就不得不提起神经网络,而tensorflow则是搭建神经网络的神器,故本人近段时间在一直学tensorflow,写这篇博客目的就是为了记录和总结自己学习tensorflow搭建简单全连接神经网络的心得和方法
首先是基本步骤
第一步:导入各种包和数据
第二步:定义训练参数
第三步:定义网络参数
第四步:定义网络结构
第五步:创建网络及定义损失函数和优化器
第六步:创建会话,启动计算图
然后是分步介绍
第一步:导入各种包和数据
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./data/", one_hot=True)
第一行是导入tensorflow包,第二和第三行是导入MNIST数据集
第二步:定义训练参数
step = 5000
learning_rate = 0.001
batch = 128
step:网络迭代次数,5000代表网络迭代训练5000次
learning_rate:学习率,具体可以搜索梯度下降算法
batch:每次迭代输入网络的数据大小,128表示每次训练输入128张图片
第三步:定义网络参数
x = tf.placeholder(tf.float32,shape=[None,784])
y = tf.placeholder(tf.float32,shape=[None,10])
w1 = tf.Variable(tf.random_normal([784,256]))
w2 = tf.Variable(tf.random_normal([256,256]))
w_out = tf.Variable(tf.random_normal([256,10]))
b1 = tf.Variable(tf.random_normal([256]))
b2 = tf.Variable(tf.random_normal([256]))
b_out = tf.Variable(tf.random_normal([10]))
x,y占位符定义了输入数据的形状,x是图片输入,y是标签输入,后面的变量定义了网络中神经元的形状
第四步:定义网络结构
def Fully_neural_network(X):
layer_1 = tf.nn.relu(tf.add(tf.matmul(X,w1),b1))
layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1,w2),b2))
layer_out = tf.matmul(layer_2,w_out)+b_out
return layer_out
以一个函数来定义网络结构
第五步:创建网络及定义损失函数和优化器
net_out = Fully_neural_network(x)
pre = tf.nn.softmax(net_out)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=net_out,labels = y ))
optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate)
train_op = optimizer.minimize(loss)
correct_pre = tf.equal(tf.argmax(pre,1),tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pre,tf.float32))
net_out: 网络输出
pre: 网络预测
loss: 损失函数
optimizer: 优化器
correct_pre: 对网络预测与真实标签对比
accuracy:网络预测的正确率
第六步:创建会话,启动计算图
#定义全部变量初始化的节点
init = tf.global_variables_initializer()
#创建会话
with tf.Session() as sess:
#全局变量初始化
sess.run(init)
#开始训练
for i in range(1,step+1):
#读入数据
batch_x, batch_y = mnist.train.next_batch(batch)
#启动计算图,喂入数据
sess.run(train_op, feed_dict={x: batch_x, y: batch_y})
#没训练100次打印loss和accuracy
if i % 100 == 0 or i == 1:
l, acc = sess.run([loss, accuracy], feed_dict={x: batch_x,
y: batch_y})
print("Step " + str(i) + ", Minibatch Loss= " + "{:.4f}".format(l) + ", Training Accuracy= " + "{:.3f}".format(acc))
print("Optimization Finished!")
# Calculate accuracy for MNIST test images
print("Testing Accuracy:", \
sess.run(accuracy, feed_dict={x: mnist.test.images,
y: mnist.test.labels}))
完整代码
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 27 10:46:42 2019
@author: wcx
"""
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./data/", one_hot=True)
step = 5000
learning_rate = 0.001
batch = 128
x = tf.placeholder(tf.float32,shape=[None,784])
y = tf.placeholder(tf.float32,shape=[None,10])
w1 = tf.Variable(tf.random_normal([784,256]))
w2 = tf.Variable(tf.random_normal([256,256]))
w_out = tf.Variable(tf.random_normal([256,10]))
b1 = tf.Variable(tf.random_normal([256]))
b2 = tf.Variable(tf.random_normal([256]))
b_out = tf.Variable(tf.random_normal([10]))
def Fully_neural_network(X):
layer_1 = tf.nn.relu(tf.add(tf.matmul(X,w1),b1))
layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1,w2),b2))
layer_out = tf.matmul(layer_2,w_out)+b_out
return layer_out
net_out = Fully_neural_network(x)
pre = tf.nn.softmax(net_out)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=net_out,labels = y ))
optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate)
train_op = optimizer.minimize(loss)
correct_pre = tf.equal(tf.argmax(pre,1),tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pre,tf.float32))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(1,step+1):
batch_x, batch_y = mnist.train.next_batch(batch)
sess.run(train_op, feed_dict={x: batch_x, y: batch_y})
if i % 100 == 0 or i == 1:
l, acc = sess.run([loss, accuracy], feed_dict={x: batch_x,
y: batch_y})
print("Step " + str(i) + ", Minibatch Loss= " + "{:.4f}".format(l) + ", Training Accuracy= " + "{:.3f}".format(acc))
print("Optimization Finished!")
# Calculate accuracy for MNIST test images
print("Testing Accuracy:", \
sess.run(accuracy, feed_dict={x: mnist.test.images,
y: mnist.test.labels}))
结果