TensorFlow实战2
TensorFlow实现多层神经网络
输入层到隐层
H = tf.sigmoid(tf.matmul(x_data,W0)+b0)
隐层到输出层
y = tf.nn.softmax(tf.matmul(H,W1)+b1)
训练优化
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(tf.clip_by_value(y,1e-10,1.0)))) #计算交叉熵
optimizer = tf.train.GradientDescentOptimizer(0.005)
train_step = optimizer.minimize(cross_entropy)
from sklearn.datasets import load_iris
import tensorflow as tf
from sklearn.model_selection import train_test_split
data, label = load_iris(True) # 导入数据,data是样本自变量,label是样本目标变量
with tf.Session() as sess: # 将样本标签转为独热编码的形式
label = sess.run(tf.one_hot(label, 3))
data_tr, data_te, label_tr, label_te = train_test_split(data, label, test_size=0.2) # 将数据集拆成训练集和测试集
'''
搭建神经网络(定义计算)
'''
x_data = tf.placeholder(tf.float32, [None, 4]) # 占位符:网络的输入
y_data = tf.placeholder(tf.float32, [None, 3]) # 占位符:网络的目标输出
w0 = tf.Variable(tf.zeros([4, 6])) # 隐层神经元的权值矩阵
w1 = tf.Variable(tf.zeros([6, 3])) # 输出层神经元的权值矩阵
b0 = tf.Variable(tf.zeros([6])) # 隐神经元的阈值
b1 = tf.Variable(tf.zeros([3])) # 输出层神经元的阈值
H = tf.sigmoid(tf.matmul(x_data, w0) + b0) # 隐层神经元的输出
y = tf.nn.softmax(tf.matmul(H, w1) + b1) # 网路的输出
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_data*tf.log(y), axis=1)) # 交叉熵
optimizer = tf.train.GradientDescentOptimizer(0.35)
train = optimizer.minimize(cross_entropy) # 训练节点
acc = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y_data, 1), tf.argmax(y, 1)), dtype=tf.float32)) # 精度
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(1000):
sess.run(train, feed_dict={x_data:data_tr, y_data:label_tr})
acc_tr = sess.run(acc, feed_dict={x_data:data_tr, y_data:label_tr})
print(acc_tr)
acc_te = sess.run(acc, feed_dict={x_data:data_te, y_data:label_te})
print('测试样本的精度:', acc_te)
sess.close()
TensorFlow动态学习速率
动态学习速率设置
global_step = tf.Variable(0, trainable=False)
训练轮数,在训练过程中会发生变化,但它不属于被优化的变量
learning_rate = tf.train.exponential_decay(0.2, global_step, 100, 0.96):以指数衰减的动态学习速率
0.2:初始学习速率
global_step:训练轮数,随着训练进行而动态变化
100:每训练100轮更新一次学习速率
0.96:衰减系数,即每100轮后学习速率乘以0.96
注:一般来说初始学习速率、衰减系数和衰减速度都是根据工程经验进行设置。
动态学习速率使用
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
梯度下降法优化器,传入的是动态学习速率
train = optimizer.minimize(cross_entropy,global_step=global_step)
利用优化器对交叉熵进行优化,注意优化时按照训练轮数对学习速率进行对动态更新
TensorFlow模型保存与调用
模型保存
import tensorflow as tf
w1 = tf.Variable(tf.random_normal(shape=[2]), name='w1')
w2 = tf.Variable(tf.random_normal(shape=[5]), name='w2')
saver = tf.train.Saver() # 模型保存的类
sess = tf.Session() # 启动会话
sess.run(tf.global_variables_initializer())
saver.save(sess, 'temp/model') # 保存模型
sess.close()
Meta graph(.meta)
计算图:i.e. all variables, operations, collections etc. This file has .meta extension
Checkpoint file(.index .data)
存储所有变量/操作的取值
checkpoint(checkpoint)
最新的checkpoint文件存储记录
模型调用
import tensorflow as tf
sess = tf.Session()
saver = tf.train.import_meta_graph('temp/model.meta') # 导入保存好的计算图
saver.restore(sess, 'temp/model') # 导入计算图中的所有参数
w1_new = tf.get_default_graph().get_tensor_by_name('w1:0') # 通过name获取保存好的tensor
sess.run(w1_new)
sess.close()
模型训练
综合示例:softmax数字识别
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
tf.reset_default_graph() # 重置计算图
mnist = input_data.read_data_sets('MNIST_data', one_hot=True) # 读取数据
x_data = tf.placeholder(tf.float32, [None, 784], name='input') # 占位符:样本自变量
y_data = tf.placeholder(tf.float32, [None, 10]) # 占位符:样本目标变量
w = tf.Variable(tf.zeros([784, 10])) # 网络权值矩阵
bias = tf.Variable(tf.zeros([10])) # 网络阈值
y = tf.nn.softmax(tf.matmul(x_data, w) + bias, name='output') # 网络输出
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_data*tf.log(y), axis=1)) # 交叉熵(损失函数)
optimizer = tf.train.GradientDescentOptimizer(0.03) # 梯度下降法优化器
train = optimizer.minimize(cross_entropy) # 训练节点
acc = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y, axis=1), tf.argmax(y_data, axis=1)), dtype=tf.float32)) # 模型预测值与样本实际值比较的精度
saver = tf.train.Saver()
sess = tf.Session() # 启动会话
sess.run(tf.global_variables_initializer()) # 执行变量初始化操作
for i in range(20000):
x_s, y_s = mnist.train.next_batch(100)
if i%1000 == 0:
acc_tr = sess.run(acc, feed_dict={x_data: x_s, y_data: y_s})
print(i, '轮训练的精度', acc_tr)
sess.run(train, feed_dict={x_data:x_s, y_data:y_s}) # 模型训练
acc_te = sess.run(acc, feed_dict={x_data:mnist.test.images, y_data:mnist.test.labels}) # 测试集精度
print('模型测试精度:', acc_te)
saver.save(sess, 'model/softmax_model')
sess.close()
模型模型加载调用
import tensorflow as tf
import cv2
import numpy as np
path = 'testimages/'
tf.reset_default_graph() # 重置计算图
sess = tf.Session()
saver = tf.train.import_meta_graph('model/softmax_model.meta')
saver.restore(sess, 'model/softmax_model')
graph = tf.get_default_graph() # 获取当前计算图
input = graph.get_tensor_by_name('input:0') # 模型输入节点
output = graph.get_tensor_by_name('output:0') # 模型输出节点
for i in range(30):
img = cv2.imread(path + str(i) +'.jpg')[:, :, 0] / 255 # 读取图片数据
img = img.reshape([1, 28 * 28]) # 进行维度转化
pre = sess.run(output, feed_dict={input: img}) # 将新样本放入模型中进行预测
res = np.argmax(pre, 1) # 预测标签
print('图片 ', str(i)+'.jpg 中的数字是: ', res[0])
sess.close()