深度学习中整个模型建立的大概思路
1、导入所需要的包(tensorflow,numpy)
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
1.5编写所需的函数
#权重初始化
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)
#卷积和池化
def conv2d(x,w):
return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding="SAME")
def max_pool_2x2(x):
return tf.nn.max_pool(x,
ksize=[1,2,2,1],
strides=[1,2,2,1],
padding="SAME")
2、把训练所用的数据提取出来
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
2.5设置神经网络基本参数
# 设置神经网络基本参数
train_steps = 30000 # 训练轮数
learning_rate = 0.1 # 初始学习率
decay_rate = 0.90 # 学习率衰减指数
decay_steps = 700 # 衰减频率控制
scale = 0.1 # 正则率
MOVING_AVERAGE_DECAY = 0.99#滑动平均
3、思考神经网络的深度为多少和卷积层、全连接层的参数设置。
4、设置训练用的占位符。
x=tf.placeholder("float",shape=(None,784))
y_=tf.placeholder("float",shape=(None,10))
5、对图片数据进行变形,使其成为可用来卷积的数据
picture_start=tf.reshape(x,[-1,28,28,1])
6、设置卷积层、激活函数,并对其池化(缩小模型的面积,压缩图片的特征)(可以根据情况设置多个卷积)
W_1=weight_variable([5,5,1,32])
B_1=bias_variable([32])
conv_1=tf.nn.relu(conv2d(picture_start,W_1)+B_1)
#池化
pool_1=max_pool_2x2(conv_1)
7、把卷积后的图片进行变形,使其可以进行全连接。
picture_end=tf.reshape(pool_1,[-1,图片的像素总量×卷积层数])
8、进行全连接
W_3=weight_variable([7*7*64,1000])
B_3=bias_variable([1000])
#设置激活函数
conv_3 = tf.nn.relu(tf.matmul(picture_end,W_3)+B_3)
9、Dropout层(减少全连接的节点,防止出现过拟合,同时可以减少模型的计算量,增强泛化能力,)
#Dropout层
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(conv_3, keep_prob)
10、设置输出层
#输出层
W_fc2 = weight_variable([1000, 10])
b_fc2 = bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
11、对所有用来增强函数泛化能力的参数进行正则化(可以增强泛化能力)
# 使用正则化
tf.add_to_collection(tf.GraphKeys.WEIGHTS, W_1)
tf.add_to_collection(tf.GraphKeys.WEIGHTS, W_2)
tf.add_to_collection(tf.GraphKeys.WEIGHTS, W_3)
tf.add_to_collection(tf.GraphKeys.WEIGHTS, b_fc2)
regularizer = tf.contrib.layers.l2_regularizer(scale)
regTerm = tf.contrib.layers.apply_regularization(regularizer)
12、设置损失函数
#设置损失函数
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))+regTerm
13、设置指数衰减学习率(开始衰减快,后面衰减慢)
#设置指数衰减学习率
learning_ratea = tf.train.exponential_decay(learning_rate,
train_steps,
decay_steps,
decay_rate)
14、设置优化函数
#设置优化函数
train_step = tf.train.GradientDescentOptimizer(learning_ratea).minimize(cross_entropy)
15、设置滑动平均
global_step = tf.Variable(0, trainable=False) #建立训练计数器
ema=tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step) #设置滑动平均
ema_op = ema.apply(tf.trainable_variables()) #所有变量都进行滑动平均
with tf.control_dependencies([train_step, ema_op]): #进行绑定操作
train_op = tf.no_op(name='train') #设置绑定对象,以及名称
16、设置初始化函数
init =tf.global_variables_initializer()
17、设置会话,并进行初始化
sess = tf.InteractiveSession() #交互式
sess.run(init)
18、给占位符输入值并进行训练
for i in range(train_steps):
batch_xs, batch_ys = mnist.train.next_batch(50)
sess.run(train_op, feed_dict={x: batch_xs, y_: batch_ys,keep_prob:0.4})
19、对训练的数据每训练100次输出它的准确率
if i % 100 == 0:
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("step",i,":",sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels,keep_prob:1.0}))
print("test accuracy %g"%accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
完整代码展示:
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
# 设置神经网络基本参数
train_steps = 30000 # 训练轮数
learning_rate = 0.1 # 初始学习率
decay_rate = 0.90 # 学习率衰减指数
decay_steps = 700 # 衰减频率控制
scale = 0.1 # 正则率
MOVING_AVERAGE_DECAY = 0.99#滑动平均
#权重初始化
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)
#卷积和池化
def conv2d(x,w):
return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding="SAME")
def max_pool_2x2(x):
return tf.nn.max_pool(x,
ksize=[1,2,2,1],
strides=[1,2,2,1],
padding="SAME")
#设置输入与对比用的站位符
x=tf.placeholder("float",shape=(None,784))
y_=tf.placeholder("float",shape=(None,10))
#第一次卷积
#设置权重和偏值
W_1=weight_variable([5,5,1,32])
B_1=bias_variable([32])
#对导入的图片进行变形
picture_start=tf.reshape(x,[-1,28,28,1])
#进行卷积和激活
conv_1=tf.nn.relu(conv2d(picture_start,W_1)+B_1)
#池化
pool_1=max_pool_2x2(conv_1)
#第二次卷积
#设置权重和偏值
W_2=weight_variable([5,5,32,64])
B_2=bias_variable([64])
#进行卷积和激活
conv_2=tf.nn.relu(conv2d(pool_1,W_2)+B_2)
#池化
pool_2=max_pool_2x2(conv_2)
#设置全链接
#设置权重和偏值
W_3=weight_variable([7*7*64,1000])
B_3=bias_variable([1000])
#改变图片行状
picture_end=tf.reshape(pool_2,[-1,7*7*64])
#设置激活函数
conv_3 = tf.nn.relu(tf.matmul(picture_end,W_3)+B_3)
#Dropout层
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(conv_3, keep_prob)
#输出层
W_fc2 = weight_variable([1000, 10])
b_fc2 = bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
# 使用正则化
tf.add_to_collection(tf.GraphKeys.WEIGHTS, W_1)
tf.add_to_collection(tf.GraphKeys.WEIGHTS, W_2)
tf.add_to_collection(tf.GraphKeys.WEIGHTS, W_3)
tf.add_to_collection(tf.GraphKeys.WEIGHTS, b_fc2)
regularizer = tf.contrib.layers.l2_regularizer(scale)
regTerm = tf.contrib.layers.apply_regularization(regularizer)
#设置损失函数
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))+regTerm
#设置指数衰减学习率
learning_ratea = tf.train.exponential_decay(learning_rate,
train_steps,
decay_steps,
decay_rate)
#设置优化函数
train_step = tf.train.GradientDescentOptimizer(learning_ratea).minimize(cross_entropy)
global_step = tf.Variable(0, trainable=False) #建立训练计数器
ema=tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step) #设置滑动平均
ema_op = ema.apply(tf.trainable_variables()) #所有变量都进行滑动平均
with tf.control_dependencies([train_step, ema_op]): #进行绑定操作
train_op = tf.no_op(name='train') #设置绑定对象,以及名称
init =tf.global_variables_initializer()
sess = tf.InteractiveSession() #交互式
sess.run(init)
for i in range(train_steps):
batch_xs, batch_ys = mnist.train.next_batch(50)
sess.run(train_op, feed_dict={x: batch_xs, y_: batch_ys,keep_prob:0.4})
if i % 100 == 0:
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("step",i,":",sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels,keep_prob:1.0}))
print("test accuracy %g"%accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
来源:CSDN
作者:qq_41503511
链接:https://blog.csdn.net/qq_41503511/article/details/104691914