第一步:加载数据
import tensorflow as tf
import os
import numpy as np
import scipy.io
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' #close the warning
scipy.io–调用读取mat数据
开启警告通知os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0'
开启警告关闭通知os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
关闭警告开启通知os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
关闭警告通知os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
featurelNum = 400
classNum = 50
trainNum = 250
testNum = 50
nnlambda = 0.009
train = 'trainData.mat'
trainData = scipy.io.loadmat(train)['trainFeatures'].ravel()
trainData = np.reshape(trainData,[featurelNum ,trainNum ])
trainData = np.transpose(trainData)
trainl = 'trainLabel.mat'
trainLabel = scipy.io.loadmat(trainl)['trainLabel'].ravel()
trainLabel = np.reshape(trainLabel,[trainNum, classNum ])
test = 'testData.mat'
testData = scipy.io.loadmat(test)['testFeatures'].ravel()
testData = np.reshape(testData,[featurelNum ,classNum ])
testData = np.transpose(testData)
testl = 'testLabel.mat'
testLabel = scipy.io.loadmat(testl)['testLabel'].ravel()
testLabel = np.reshape(testLabel,[50,classNum ])
nnlambda=0.009是正则化里的参数,影响数据过拟合,此处可以去掉。
第二步:构建模型
(1)定义输入数据,初始化权重、偏置
sess = tf.InteractiveSession()
x = tf.placeholder("float", shape=[None, 400])
y_ = tf.placeholder("float", shape=[None, 50])
W = tf.Variable(tf.zeros([400, 50]))
b = tf.Variable(tf.zeros([50]))
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)
(2)定义网络模型函数
卷积层:使原信号特征增强,并且降低噪音(增加网络训练参数)
池化层:降低网络训练参数及模型的过拟合程度(最大池化/平均池化)
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')
第一层卷积、池化
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1, 20, 20, 1]) #20*20*1
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) #20*20*32
h_pool1 = max_pool_2x2(h_conv1) #10*10*32
第二层卷积、池化
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
全连接层
全连接层的每一个结点都与上一层的所有结点相连,用来把前边提取到的特征综合起来。
W_fc1 = weight_variable([5*5*64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 5*5*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
dropout层
背景:1989年lenet8层网络没有dropout层,2012年alexnet152层开始采用dropout层
dropout(随机失活),相当于每次迭代都随机根据概率(keep_prob)挑选特征的子空间经过神经元。
优点:不仅实现集成的思想,而且还降低特征之间的关联性,增强泛化(类似环境适应)能力。
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
输出层
W_fc2 = weight_variable([784, 50])
b_fc2 = bias_variable([50])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
softmax函数:归一化指数函数
有限项离散概率分布的梯度对数归一化(将多分类的输出数值转化为相对概率)。
S i = e V i Σ i C e V i S_i = \frac{e^{V_i}}{\Sigma^C_i e^{V_i}} Si=ΣiCeVieVi
其中,Vi 是分类器前级输出单元的输出。i 表示类别索引,总的类别个数为 C。Si 表示的是当前元素的指数与所有元素指数和的比值。
(数字图像处理讲过归一化问题)
[ 1 , 2 , 3 , 4 ] 归一化处理 [ 0.1 , 0.2 , 0.3 , 0.4 ]
Sigmoid是极端情况(类别数为2)下的Softmax
(3)定义损失函数,得到预测值
此处损失函数(也叫成本函数)是交叉熵,比较预测值和真实值的差异。
交叉熵:用于度量两个概率分布间的差异性信息。
离散变量: Σ x p ( x ) ⋅ log ( 1 q ( x ) ) \Sigma_x p(x)\cdot \log(\frac{1}{q(x)}) Σxp(x)⋅log(q(x)1) 连续变量: − ∫ X P ( x ) log Q ( x ) d r ( x ) = E p [ − log Q ] -\int_X P(x)\log Q(x)d r(x) = E_p [-\log Q] −∫XP(x)logQ(x)dr(x)=Ep[−logQ]
p表示真实标记的分布,q则为训练后的模型的预测标记分布,交叉熵损失函数可以衡量p与q的相似性。
优点:使用sigmoid函数在梯度下降时能避免均方误差损失函数学习速率降低的问题,因为学习速率可以被输出的误差所控制。
with tf.name_scope(‘cross_entropy’):
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
tf.summary.scalar(‘cross_entropy’, cross_entropy)
定义训练操作,采用优化器 AdamOptimizer
定义预测操作prediction
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
with tf.name_scope('accuracy'): #可以让变量有相同的命名
with tf.name_scope('correct_prediction'):
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
with tf.name_scope('accuracy'):
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
tf.summary.scalar('accuracy', accuracy)
把训练测试的结果写入log,方便tensorboard可视化显示
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter('log/train', sess.graph)
test_writer = tf.summary.FileWriter('log/test')
第三步:训练模型和评估模型
tf.global_variables_initializer().run()#在一个会话中启动图
for i in range(4000):
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x: trainData , y_: trainLabel, keep_prob: 1.0})
print ("setup_%d,_training_accuracy%g" % (i, train_accuracy))
print ("test_accuracy_%g" % accuracy.eval(feed_dict={
x: testData, y_: testLabel, keep_prob: 1.0}))
summary, _ = sess.run([merged, train_step], feed_dict={x: trainData, y_: trainLabel, keep_prob: 0.5}) #失活率是0.5
train_writer.add_summary(summary, i)