tensorflow学习框架(炼数成金网络版学习记录)

时间:2022-05-07 09:19:43

chapter01

#变量

import tensorflow as tf
x = tf.Variable([1,2])
a = tf.constant([3,3])
#增加一个减法op
sub = tf.subtract(x,a)
#增加一个假发op
add = tf.add(x,sub)
#初始化所有变量
init = tf.global_variables_initializer() with tf.Session() as sess:
#变量初始化操作
sess.run(init)
print(sess.run(sub))
print(sess.run(add))

#Fetch

import tensorflow as tf
input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)
add = tf.add(input2,input3)
mul = tf.multiply(input1,add)
with tf.Session() as sess:
  result = sess.run([mul,add])
  print(resutl)

#Feed

#Feed
import tensorflow as tf
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32) #创建占位符
output = tf.multiply(input1,input2)
with tf.Session() as sess:
#feed数据以字典的形式传入
print(sess.run(output,feed_dict={input1:[7.],input2:[2.]})

chapter02

#线性迭代求k,b值

import numpy as np
import tensorflow as tf #use numpy to create 100 points
x_data = np.random.rand(100)
y_data = x_data * 0.1 + 0.2 #真实值,确定值 #create a linear model
b = tf.Variable(0.)
k = tf.Variable(0.)
y = k*x_data + b #预测值 #优化线性模型,接近于样本点的分布
#二次代价函数,即最小二乘法(最小平方法)
loss = tf.reduce_mean(tf.square(y_data-y))
#定义一个梯度下降法来进行训练的优化器
optimizer = tf.train.GradientDescentOptimizer(0.2)
#最下化代价函数
train = optimizer.minimize(loss) #初始化变量
init = tf.global_variables_initializer() with tf.Session() as sess:
sess.run(init)
for step in range(201):
sess.run(train)
if step%20 == 0:
print(step,sess.run([k,b]))

#非线性回归

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt #使用numpy生成200个随机点
x_data = np.linspace(-0.5,0.5,200)[:,np.newaxis] #均匀分布200个点,200lines and 1 row
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise #定义两个placeholder
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1]) #定义神经网络中间层
Weights_L1 = tf.Variable(tf.random_normal([1,10])) #权重
biases_L1 = tf.Variable(tf.zeros([1,10])) #10个偏置值
Wx_plus_b_L1 = tf.matmul(x,Weights_L1) + biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1) #双曲正切函数 #定义神经网络输出层
Weights_L2 = tf.Variable(tf.random_normal([10,1]))
biases_L2 = tf.Variable(tf.zeros([1,1]))
Wx_plus_b_L2 = tf.matmul(L1,Weights_L2) + biases_L2
prediction = tf.nn.tanh(Wx_plus_b_L2) #二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction))
#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) with tf.Session() as sess:
#变量初始化
sess.run(tf.global_variables_initializer())
for _ in range(2000):
sess.run(train_step,feed_dict={x:x_data,y:y_data})
#获得预测值
prediction_value = sess.run(prediction,feed_dict={x:x_data})
#画图
plt.figure()
plt.scatter(x_data,y_data)
plt.plot(x_data,prediction_value,'r-',lw=5)
plt.show()

tensorflow学习框架(炼数成金网络版学习记录)

chapter03

#手写数字集和softmax

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data #载入数据集
mnist = input_data.read_data_sets('MNIST_data',one_hot=True) #每个批次的大小
batch_size = 100 #tip1 批次大小修改
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size #定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10]) #创建一个简单的神经网络
W = tf.Variable(tf.zeros([784,10])) #tip2增加隐藏层和激活函数 #tip3权值和偏置值修改,初始化值修改
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b) #二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction)) #tip4交叉熵使用效果
#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) #tip5改学习率 #tip6优化方式 #初始化变量
init = tf.global_variables_initializer() #定义求准确率方法
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大值所在位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) with tf.Session() as sess:
sess.run(init)
for epoch in range(200): #tip7训练次数修改
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys}) acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("Iter" + str(epoch) + ",Testing Accuracy" + str(acc))
训练结果
Iter0,Testing Accuracy0.8296
Iter1,Testing Accuracy0.8712
Iter2,Testing Accuracy0.8805
Iter3,Testing Accuracy0.8878
Iter4,Testing Accuracy0.8934
Iter5,Testing Accuracy0.8969
Iter6,Testing Accuracy0.8993
Iter7,Testing Accuracy0.9014
Iter8,Testing Accuracy0.9037
Iter9,Testing Accuracy0.9051
Iter10,Testing Accuracy0.9063
Iter11,Testing Accuracy0.9072
Iter12,Testing Accuracy0.9073
Iter13,Testing Accuracy0.9091
Iter14,Testing Accuracy0.9102
Iter15,Testing Accuracy0.9107
Iter16,Testing Accuracy0.9121
Iter17,Testing Accuracy0.9124
Iter18,Testing Accuracy0.9124
Iter19,Testing Accuracy0.9134

#手写数字集MNIST&softmax优化,提示7tip,更改7处

#tip1 批次大小修改

#tip2修改激活函数

#tip3权值和偏置值修改,初始化值修改

#tip4使用交叉熵

#tip5修改学习率

#tip6修改优化方式

#tip7训练次数修改

import tensorflow
from tensorflow.examples.tutorials.mnist import input_data
#载入数据集
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)
#one_hot称为独热编码,又称一位有效编码,N位状态寄存器来对N个状态进行编码,每个状态都有独立的寄存位,并且在任意时候,只有一位有效 #每个批次的大小
batch_size = 200
#tip1 批次大小修改
#tip1实践结果:批次越大,准确率越低;批次越小,准确率越高,训练速度慢 #计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size #定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10]) ##定义一个中间层
Weights_L1 = tf.Variable(tf.random_normal([784,10]))
biases_L1 = tf.Variable(tf.zeros([1,10])) #10个偏置值
Wx_plus_b_L1 = tf.matmul(x,Weights_L1) + biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1) #定义一个输出层
Weights_L2 = tf.Variable(tf.random_normal([10,1])) #[n_batch,1]
biases_L2 = tf.Variable(tf.zeros([10,1]))
Wx_plus_b_L2 = tf.matmul(L1,Weights_L2)
prediction = tf.nn.tanh(Wx_plus_b_L2) #tip2修改激活函数
#使用激活函数 prediction = tf.nn.softmax(tf.matmul(x,W)+b)
#tip2实践结果:
#结果不理想,准确率稳定在0.1135
#tip3权值和偏置值修改,初始化值修改
#权值和偏置值修改,结果稳定在0.098和0.1135 #二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction)) #tip4使用交叉熵
#tip4实践结果,使用效果不佳,不如二次代价函数。。
#交叉熵有4中类型:
#tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction)
#tf.nn.sparse_softmax_cross_entropy_with_logits()
#tf.nn.sigmoid_cross_entropy_with_logits()
#tf.nn.weighted_cross_entropy_with_logits() #使用梯度下降法
train_step = tf.train.FtrlOptimizer(0.6).minimize(loss)
#tip5修改学习率
#tip5实践结果 改学习率学习率越大,准确率越高,同时优化速度变慢,该学习率最理想在区间[0.5-0.8]
#tip6修改优化方式,优化算法参考地址https://blog.csdn.net/u014033218/article/details/89602916
#tip6实践结果 FtrlOptimizer优化方式好于GradientDescentOptimizer #初始化变量
init = tf.global_variables_initializer() #定义求准确率方法
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大值所在位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) with tf.Session() as sess:
sess.run(init)
for epoch in range(21):
#tip7训练次数修改
#tip7实践结果:对结果影响不大,稳定在0.93
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys}) acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("Iter" + str(epoch) + ",Testing Accuracy" + str(acc))

chapter04

how to solve the problem of overfitting

1.increase the number of dataset

2.use the normalized method

3.dropout

use some of the neural networks to train but use the whole neural networks to test.

tensorflow学习框架(炼数成金网络版学习记录)

4.常用优化器

tf.train.GradientDescentOptimizer

tf.train.AdadeltaOptimizer

tf.train.MomentumOptimizer

tf.train.AdamOptimizer

tf.train.RMSPropOptimizer(Root Mean Square)

tensorflow学习框架(炼数成金网络版学习记录)

tensorflow学习框架(炼数成金网络版学习记录)

tensorflow学习框架(炼数成金网络版学习记录)

tensorflow学习框架(炼数成金网络版学习记录)

tensorflow学习框架(炼数成金网络版学习记录)

tensorflow学习框架(炼数成金网络版学习记录)

tensorflow学习框架(炼数成金网络版学习记录)

以上5中优化器中,随机模型梯度下降法的收敛速度比较慢。

tensorflow学习框架(炼数成金网络版学习记录)

5.homework

将tensorflow的week04的模型的识别率调到>98%

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data #载入数据集
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)
#one_hot称为独热编码,又称一位有效编码,N位状态寄存器来对N个状态进行编码,每个状态都有独立的寄存位,并且在任意时候,只有一位有效 #每个批次的大小
batch_size = 100
#tip1 批次大小修改
#tip1实践结果:批次越大,准确率越低;批次越小,准确率越高,训练速度慢 #计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size #定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob = tf.placeholder(tf.float32) #定义中间层神经网络
W1 = tf.Variable(tf.truncated_normal([784,500],stddev=0.1))
b1 = tf.Variable(tf.zeros([500])+0.1)
L1 = tf.nn.tanh(tf.matmul(x,W1)+b1)
L1_drop = tf.nn.dropout(L1,keep_prob) W2 = tf.Variable(tf.truncated_normal([500,300],stddev=0.1))
b2 = tf.Variable(tf.zeros([300])+0.1)
L2 = tf.nn.tanh(tf.matmul(L1_drop,W2)+b2)
L2_drop = tf.nn.dropout(L2,keep_prob) W3 = tf.Variable(tf.truncated_normal([300,100],stddev=0.1))
b3 = tf.Variable(tf.zeros([100])+0.1)
L3 = tf.nn.tanh(tf.matmul(L2_drop,W3)+b3)
L3_drop = tf.nn.dropout(L3,keep_prob) W4 = tf.Variable(tf.truncated_normal([100,10],stddev=0.1))
b4 = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(L3_drop,W4)+b4) #二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction)) #使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(1.0).minimize(loss)
#if learning rate is too small, low convergence; if learning rate is too large
#may not decrease on every iteration, may not converge
#learning rate is really important for accuracy #初始化变量
init = tf.global_variables_initializer() #定义求准确率方法
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大值所在位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) with tf.Session() as sess:
sess.run(init)
for epoch in range(41):
#tip7训练次数修改
#tip7实践结果:对结果影响不大,稳定在0.93
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0}) test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1.0})
print("Iter" + str(epoch) + ",Testing Accuracy" + str(test_acc) + ",Training Accuracy" + str(train_acc)

chapter05

第四章作业

将训练模型的识别率提高到98%

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data #载入数据集
#1,神经网络层数和每层个数影响准确率
#2,学习率大小影响准确率, 越接近收敛,学习率应该越小
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)
#one_hot称为独热编码,又称一位有效编码,N位状态寄存器来对N个状态进行编码,每个状态都有独立的寄存位,并且在任意时候,只有一位有效 #每个批次的大小
batch_size = 20
#tip1 批次大小修改
#tip1实践结果:批次越大,准确率越低;批次越小,准确率越高,训练速度慢 #计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size #定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob = tf.placeholder(tf.float32)
lr = tf.Variable(0.001,dtype=tf.float32) #定义中间层神经网络
W1 = tf.Variable(tf.truncated_normal([784,300],stddev=0.1))
b1 = tf.Variable(tf.zeros([300])+0.1)
L1 = tf.nn.tanh(tf.matmul(x,W1)+b1)
L1_drop = tf.nn.dropout(L1,keep_prob) W2 = tf.Variable(tf.truncated_normal([300,200],stddev=0.1))
b2 = tf.Variable(tf.zeros([200])+0.1)
L2 = tf.nn.tanh(tf.matmul(L1_drop,W2)+b2)
L2_drop = tf.nn.dropout(L2,keep_prob) W3 = tf.Variable(tf.truncated_normal([200,100],stddev=0.1))
b3 = tf.Variable(tf.zeros([100])+0.1)
L3 = tf.nn.tanh(tf.matmul(L2_drop,W3)+b3)
L3_drop = tf.nn.dropout(L3,keep_prob) W4 = tf.Variable(tf.truncated_normal([100,10],stddev=0.1))
b4 = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(L3_drop,W4)+b4) #交叉熵代价函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction)) #使用梯度下降法
train_step = tf.train.AdamOptimizer(lr).minimize(loss) #if learning rate is too small, low convergence; if learning rate is too large
#may not decrease on every iteration, may not converge #初始化变量
init = tf.global_variables_initializer() #定义求准确率方法
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大值所在位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) with tf.Session() as sess:
sess.run(init)
for epoch in range(51):
sess.run(tf.assign(lr, 0.001 * (0.95 ** epoch))) #tf.assing(A,B)用法,把B赋值给A,比如sess.run(tf.assign(A, [1,2,5])) A=[1,2,5]
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0})
learning_rate = sess.run(lr)
test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
#train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1.0})
print("Iter" + str(epoch) + ",Testing Accuracy" + str(test_acc) + ",learning rate" + str(learning_rate))
Iter46,Testing Accuracy0.981,learning rate9.446825e-05
Iter47,Testing Accuracy0.9803,learning rate8.974483e-05
Iter48,Testing Accuracy0.9816,learning rate8.525759e-05
Iter49,Testing Accuracy0.9813,learning rate8.099471e-05
Iter50,Testing Accuracy0.9815,learning rate7.6944976e-05

tensorboard可能会出现的问题

1.如果按照正常流程走,logs的绝对路径也输对了,最后出现下图的问题,说明logs的安装路径有问题,解决方案,是把logs的文件夹放到tensorflow库的安装目录下,比如我是这样做的,我把tensorflow放到了这个目录下C:\Users\xxx(计算机名)\Anaconda3\Lib\site-packages\tensorflow\models\research\(logs)

tensorflow学习框架(炼数成金网络版学习记录)

解决完问题后,出现的界面是这样的。

tensorflow学习框架(炼数成金网络版学习记录)

tensorboard生成过程:

1.编辑好jupyter程序,运行;

2.tensorflow\research\logs中多出来一个文件;

3.cmd到命令行,cd到C:\Users\yuyuk文件夹下后,输入 tensorboard --logdir=C:\Users\yuyuk\Anaconda3\Lib\site-packages\tensorflow\models\research

4.在浏览器中输入命令行窗口返回的url即可

或者另一种方案是在开启jupyter后,直接在浏览器中把窗口切换到tensorflow安装目录下,但必须是用户名,比如C:\Users\yuyuk\Anaconda3\Lib\site-packages\tensorflow\models\research\logs,那么在编程时生成的文件/文件夹自动在tensorflow的文件夹下。

tensorboard网络结构

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data #载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True) #每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size #命名空间
with tf.name_scope('input'):
#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784],name='input-x')
y = tf.placeholder(tf.float32,[None,10],name='input-y') with tf.name_scope('layer'):
with tf.name_scope('weights'):
W = tf.Variable(tf.zeros([784,10]),name='W')
with tf.name_scope('biases'):
b = tf.Variable(tf.zeros([10]),name='b')
with tf.name_scope('wx_plus_b'):
wx_plus_b = tf.matmul(x,W) + b
with tf.name_scope('softmax'):
prediction = tf.nn.softmax(wx_plus_b) #二次代价函数
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.square(y-prediction)) with tf.name_scope('train'):
#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) #初始化变量
init = tf.global_variables_initializer() with tf.name_scope('accuracy'):
with tf.name_scope('correct_prediction'):
#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
with tf.name_scope('accuracy'):
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) with tf.Session() as sess:
sess.run(init)
writer = tf.summary.FileWriter('logs/',sess.graph)
for epoch in range(1):
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys}) acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))

tensorflow学习框架(炼数成金网络版学习记录)

全局展开后

tensorflow学习框架(炼数成金网络版学习记录)

tensorboard网络运行

#载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True) #每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size #参数概要
def variable_summaries(var):
with tf.name_scope('summaries'):
mean = tf.reduce_mean(var)
tf.summary.scalar('mean', mean)#平均值
with tf.name_scope('stddev'):
stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
tf.summary.scalar('stddev', stddev)#标准差
tf.summary.scalar('max', tf.reduce_max(var))#最大值
tf.summary.scalar('min', tf.reduce_min(var))#最小值
tf.summary.histogram('histogram', var)#直方图 #命名空间
with tf.name_scope('input'):
#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784],name='x-input')
y = tf.placeholder(tf.float32,[None,10],name='y-input') with tf.name_scope('layer'):
#创建一个简单的神经网络
with tf.name_scope('wights'):
W = tf.Variable(tf.zeros([784,10]),name='W')
variable_summaries(W)
with tf.name_scope('biases'):
b = tf.Variable(tf.zeros([10]),name='b')
variable_summaries(b)
with tf.name_scope('wx_plus_b'):
wx_plus_b = tf.matmul(x,W) + b
with tf.name_scope('softmax'):
prediction = tf.nn.softmax(wx_plus_b) #二次代价函数
# loss = tf.reduce_mean(tf.square(y-prediction))
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
tf.summary.scalar('loss',loss)
with tf.name_scope('train'):
#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) #初始化变量
init = tf.global_variables_initializer() with tf.name_scope('accuracy'):
with tf.name_scope('correct_prediction'):
#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
with tf.name_scope('accuracy'):
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
tf.summary.scalar('accuracy',accuracy) #合并所有的summary
merged = tf.summary.merge_all() with tf.Session() as sess:
sess.run(init)
writer = tf.summary.FileWriter('logs/',sess.graph)
for epoch in range(51):
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
summary,_ = sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys}) writer.add_summary(summary,epoch)
acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))

Graphs中的内容tensorboard网络结构没有太大区别,但在scalars中,多出了三个图,分别是accuracy,layer,loss

首先是accuracy,描述的是随着循环次数的增加,准确率不断上升

tensorflow学习框架(炼数成金网络版学习记录)

左边框中的smoothing可以调节平滑率

tensorflow学习框架(炼数成金网络版学习记录)

在layer中,有两个内容,一是权重的四个参数,二是偏置的四个参数;

tensorflow学习框架(炼数成金网络版学习记录)

tensorflow学习框架(炼数成金网络版学习记录)

最后是loss层

tensorflow学习框架(炼数成金网络版学习记录)

在distribution中的layer中,有两幅图,一张是layer/biases/summaries/histogram,另一张是layer/wights/summaries/histogram,显示的是权重和偏置值的分布

tensorflow学习框架(炼数成金网络版学习记录)

在histograms中也有两幅权重和偏置值的直方图

tensorflow学习框架(炼数成金网络版学习记录)

tensorboard可视化

#载入数据集
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
#运行次数
max_steps = 1001
#图片数量
image_num = 3000
#文件路径
DIR = r"C:/Users/yuyuk/Anaconda3/Lib/site-packages/tensorflow/" #windows中一律用'\\',不用'/' #定义会话
sess = tf.Session() #载入图片
embedding = tf.Variable(tf.stack(mnist.test.images[:image_num]), trainable=False, name='embedding') #参数概要
def variable_summaries(var):
with tf.name_scope('summaries'):
mean = tf.reduce_mean(var)
tf.summary.scalar('mean', mean)#平均值
with tf.name_scope('stddev'):
stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
tf.summary.scalar('stddev', stddev)#标准差
tf.summary.scalar('max', tf.reduce_max(var))#最大值
tf.summary.scalar('min', tf.reduce_min(var))#最小值
tf.summary.histogram('histogram', var)#直方图 #命名空间
with tf.name_scope('input'):
#这里的none表示第一个维度可以是任意的长度
x = tf.placeholder(tf.float32,[None,784],name='x-input')
#正确的标签
y = tf.placeholder(tf.float32,[None,10],name='y-input') #显示图片
with tf.name_scope('input_reshape'):
image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])
tf.summary.image('input', image_shaped_input, 10) with tf.name_scope('layer'):
#创建一个简单神经网络
with tf.name_scope('weights'):
W = tf.Variable(tf.zeros([784,10]),name='W')
variable_summaries(W)
with tf.name_scope('biases'):
b = tf.Variable(tf.zeros([10]),name='b')
variable_summaries(b)
with tf.name_scope('wx_plus_b'):
wx_plus_b = tf.matmul(x,W) + b
with tf.name_scope('softmax'):
prediction = tf.nn.softmax(wx_plus_b) with tf.name_scope('loss'):
#交叉熵代价函数
loss = tf.reduce_mean(tf.square(y-prediction))
tf.summary.scalar('loss',loss)
with tf.name_scope('train'):
#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss) #初始化变量
sess.run(tf.global_variables_initializer()) with tf.name_scope('accuracy'):
with tf.name_scope('correct_prediction'):
#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
with tf.name_scope('accuracy'):
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))#把correct_prediction变为float32类型
tf.summary.scalar('accuracy',accuracy) #产生metadata文件
if tf.gfile.Exists(DIR + r'projector/projector/metadata.tsv'):
tf.gfile.DeleteRecursively(DIR + r'projector/projector/metadata.tsv')
with open(DIR + r'projector/projector/metadata.tsv', 'w') as f:
labels = sess.run(tf.argmax(mnist.test.labels[:],1))
for i in range(image_num):
f.write(str(labels[i]) + '\n') #合并所有的summary
merged = tf.summary.merge_all() projector_writer = tf.summary.FileWriter(DIR + r'projector/projector',sess.graph)
saver = tf.train.Saver()
config = projector.ProjectorConfig() #定义了配置文件
embed = config.embeddings.add()
embed.tensor_name = embedding.name
embed.metadata_path = DIR + r'projector/projector/metadata.tsv'
embed.sprite.image_path = DIR + r'projector/data/mnist_10k_sprite.png'
embed.sprite.single_image_dim.extend([28,28])
projector.visualize_embeddings(projector_writer,config) for i in range(max_steps):
#每个批次100个样本
batch_xs,batch_ys = mnist.train.next_batch(100)
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
summary,_ = sess.run([merged,train_step],feed_dict={x:batch_xs,y:batch_ys},options=run_options,run_metadata=run_metadata)
projector_writer.add_run_metadata(run_metadata, 'step%03d' % i)
projector_writer.add_summary(summary, i) if i%100 == 0:
acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print ("Iter " + str(i) + ", Testing Accuracy= " + str(acc)) saver.save(sess, DIR + r'projector/projector/a_model.ckpt', global_step=max_steps)
projector_writer.close()
sess.close()

当迭代次数增加到1000时,准确率的密度变大

chapter 06

使用CNN后预测模型的准确率有明显升高

mnist = input_data.read_data_sets('MNIST_data',one_hot=True)

#每个批次的大小
batch_size = 100
n_batch = mnist.train.num_examples // batch_size #初始化权值
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):
#ksize [1,x,y,1]
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') #[1,2,2,1]中2,2代表窗口大小 #定义两个placeholder
x = tf.placeholder(tf.float32,[None,784]) #28*28 None代表行,784代表列,28x28
y = tf.placeholder(tf.float32,[None,10]) #改变x的格式转为AD的向量[batch, in_height, in_width, in_channels]
x_image = tf.reshape(x,[-1,28,28,1]) #1表示黑白图片,3表示彩色图片 #初始化第一个卷积层的权值和偏置值
W_conv1 = weight_variable([5,5,1,32]) #5x5的采样窗口,32个卷积核从1个平面抽取特征
b_conv1 = bias_variable([32]) #每一个卷积核,一个偏置值 #把x_image和权值向量进行卷积,再加上偏置值,然后应用于relu激活函数
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1) #进行max-pooling #初始化第二个卷积层的权值和偏置
W_conv2 = weight_variable([5,5,32,64]) #5*5的采样窗口,64个卷积核从32个平面抽取特征
b_conv2 = bias_variable([64]) #每一个卷积核一个偏置值 #把h_pool1和权值向量进行卷积,再加上偏置值,然后应用于relu激活函数
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2) #进行max-pooling #28*28的图片第一次卷积后还是28*28,第一次池化后变为14*14
#第二次卷积后为14*14,第二池化后变为7*7
#经过上面操作后得到64张7*7平面 #初始化第一个全连接层的权值
W_fc1 = weight_variable([7*7*64,1024]) #上一层有7*7*64个神经元,全连接层有1024个神经元
b_fc1 = bias_variable([1024]) #1024个节点 #把池化层2的输出扁平化为1维
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
#求第一个去全连接层的输出
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1) + b_fc1) #keep_prob用来表示神经元的输出频率
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob) #初始化第二个全连接层
W_fc2 = weight_variable([1024,10]) #1024代表2014个神经元,10代表有10个分类
b_fc2 = bias_variable([10]) #计算输出
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2) + b_fc2) #交叉熵代价函数
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#使用AdamOtimizer进行优化
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
#结果放在一个布尔列表中
correct_prediction = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1)) #argmax返回一维张量中最大值所在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(21):
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.7}) acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
print("Iter" + str(epoch) + ",Testing Accuracy" + str(acc))

迭代7次的结果,准确率>98%,最终预计可以>99%

使用tensorboard对CNN进行可视化,准确率有所降低,代码待更新。

chpater 07

RNN结构

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data #载入数据集
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True) #输入28*28图片数据
one_line_inputs = 28 #输入一行,一行有28个数据
max_time = 28 #一共28行
lstm_size = 100 #100个隐藏单元(block)
n_classes = 10 #10个分类(0-9)
one_batch_size = 50 #每个批次50个样本
n_batch = mnist.train.num_examples // one_batch_size #计算一共有多少个批次 #定义2个placeholder,x是输入,y是输出,none代表这个维度可以是任意长度
x = tf.placeholder(tf.float32,[None,784])
#正确的标签
y = tf.placeholder(tf.float32,[None,10]) #初始化权值
weights = tf.Variable(tf.truncated_normal([lstm_size,n_classes],stddev=0.1)) #参数 stddev 用于设置正太分布被截断前的标准差,提高训练精度
#初始化偏置值
biases = tf.Variable(tf.constant(0.1,shape=[n_classes])) #定义RNN网络
def RNN(X,weights,biases):
# inputs=[batch_size, max_time, one_line_inputs] 这个列子 50,28,28
inputs = tf.reshape(X,[-1,max_time,one_line_inputs])
#定义LSTM基本CELL
lstm_cell = tf.contrib.rnn.core_rnn_cell.BasicLSTMCell(lstm_size) #BasicLSTMCell可改变
#final_state[0]是cell state
#final_state[1]是hidden_state
outputs,final_state = tf.nn.dynamic_rnn(lstm_cell,inputs,dtype=tf.float32)
results = tf.nn.softmax(tf.matmul(final_state[1],weights) + biases)
return results #计算RNN的返回结果
prediction = RNN(x,weights,biases)
#损失函数
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
#使用AdamOptimizer进行优化
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #训练值和测试值进行比较,argmax返回一维张量中最大的值所在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #把correct_prediction变成float32类型,并求准确率
#初始化
init = tf.global_variables_initializer() with tf.Session() as sess:
sess.run(init)
for epoch in range(6):
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(one_batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys}) accuracy_1 = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("Iter" + str(epoch) + "Accuracy" + str(accuracy_1)) #pandas库报错,有个别函数要更新,否则数据出不来

LSTM:Long-Short Term Memory 长短期记忆网络

chpater 08

8.1chapter07 作业1

RNN框架中output和final_state各有多少个维度,以及每个维度的意思?

outputs对应的是每个状态的输出

tf.nn.dynamic_rnn(cell,inuts,sequence_length=None,initial_state=None,dtype=None,parallel_iteration=None,swap_memory=False,time_major=False,scope=None)

cell表示LSTM单元;

inputs表示RNN输入;

sequence_length表示主动告知模型有效长度;

initial_state: shape = (batch_size, cell.state_size),初始状态。一般可以取零矩阵;

dtype表示单元类型;

parallel_iteration表示并行运行迭代次数,默认32;

swap_memory表示透明交换推理中产生的张量,适用于训练不适合GPU的单个RNN;

time_major表示最大行数,最大批次(列数)和深度;if True,则默认为[max_time,batch_size,depth];if False, 则time_major为[batch_size,max_time,depth];

scope表示创建子图Variablescope,默认为RNN

参数详细参考:

cell:RNNCell的一个实例.
inputs:RNN输入.如果time_major == False(默认),则是一个shape为[batch_size, max_time, ...]的Tensor,或者这些元素的嵌套元组.如果time_major == True,则是一个shape为[max_time, batch_size, ...]的Tensor,或这些元素的嵌套元组.这也可能是满足此属性的Tensors(可能是嵌套的)元组.前两个维度必须匹配所有输入,否则秩和其他形状组件可能不同.在这种情况下,在每个时间步输入到cell将复制这些元组的结构,时间维度(从中获取时间)除外.在每个时间步输入到个cell将是一个Tensor或(可能是嵌套的)Tensors元组,每个元素都有维度[batch_size, ...].
sequence_length:(可选)大小为[batch_size]的int32/int64的向量.超过批处理元素的序列长度时用于复制状态和零输出.所以它更多的是正确性而不是性能.
initial_state:(可选)RNN的初始状态.如果cell.state_size是整数,则必须是具有适当类型和shape为[batch_size, cell.state_size]的Tensor.如果cell.state_size是一个元组,则应该是张量元组,在cell.state_size中为s设置shape[batch_size, s].
dtype:(可选)初始状态和预期输出的数据类型.如果未提供initial_state或RNN状态具有异构dtype,则是必需的.
parallel_iterations:(默认值:32).并行运行的迭代次数.适用于那些没有任何时间依赖性并且可以并行运行的操作.该参数用于交换空间的时间.远大于1的值会使用更多内存但占用更少时间,而较小值使用较少内存但计算时间较长.
swap_memory:透明地交换推理中产生的张量,但是需要从GPU到CPU的支持.这允许训练通常不适合单个GPU的RNN,具有非常小的(或没有)性能损失.
time_major:inputs和outputsTensor的形状格式.如果是true,则这些 Tensors的shape必须为[max_time, batch_size, depth].如果是false,则这些Tensors的shape必须为[batch_size, max_time, depth].使用time_major = True更有效,因为它避免了RNN计算开始和结束时的转置.但是,大多数TensorFlow数据都是batch-major,因此默认情况下,此函数接受输入并以batch-major形式发出输出.
scope:用于创建子图的VariableScope;默认为“rnn”.

final_state对应的是最终状态的输出

final_state[state,batch_size,cell.state_size];

state表示final_state的状态,只有两个值,final_state[0]/final_state[1],final_state[0]表示cell state,final_state[1]表示hidden state;

batch_size表示每个样本有多少个批次,这里设置为50;

cell.state_size表示lstm_size隐藏单元的个数,这里设置为100;

作业2

描述LSTM信号传播和过程

常见RNN模型:

tensorflow学习框架(炼数成金网络版学习记录)

左图中的RNN是没有按照时间展开的网络,右图是按照时间展开的RNN网络

1)x(t)代表在序列索引号 t 时训练样本的输入。同样的,x(t-1) 和 x(t+1) 代表在序列索引号 t−1 和 t+1 时训练样本的输入。

2)h(t) 代表在序列索引号 t 时模型的隐藏状态。h(t)由x(t)和 h(t-1) 共同决定。

3)o(t) 代表在序列索引号 t 时模型的输出。o(t)只由模型当前的隐藏状态 h(t) 决定。

4)L(t) 代表在序列索引号 t 时模型的损失函数,模型整体的损失函数是所有的L(t)相加和。

5)y(t) 代表在序列索引号 t 时训练样本序列的真实输出。

6)U,W,V这三个矩阵就是我们的模型的线性关系参数,它在整个RNN网络中是共享的。也正是因为是共享的,它体现了RNN的模型的“循环反馈”的思想。

循环网络前向传播算法:

  对于t时刻:

 tensorflow学习框架(炼数成金网络版学习记录)

  其中ϕ(.)为激活函数,一般来说会选择tanh函数,b为偏置。则 t 时刻的输出:

  tensorflow学习框架(炼数成金网络版学习记录)

  最终模型的预测输出为:

  tensorflow学习框架(炼数成金网络版学习记录)

  其中σ为激活函数,激活函数通常选择softmax函数。

LSTM的四种状态:遗忘门,输入门,输出门,细胞状态。

tensorflow学习框架(炼数成金网络版学习记录)

以下参数命名:i为输入,o为输出,f为遗忘,sigma为学习率,W和U为系数,b为偏置值,c为cell state,h为hidden state,t为时间,x为输入信息,配合上图说明下面公式含义

tensorflow学习框架(炼数成金网络版学习记录)tensorflow学习框架(炼数成金网络版学习记录)

1.在输入端上,数据分三端进入,和学习率作同或运算得到i(t);

2.在临时cell state端上,数据分三端进入,和学习率作同或运算得到c(t)-;

3.做操作i(t)*c(t)-得到c(t);

4.在输出端上,数据分三端进入,和学习率作同或运算得到o(t);

5.对c(t)作正切函数处理,和o(t)作同或运算得到隐藏层h(t)输出。

8.2保存和导入训练已训练模型

保存模型到test_net.ckpt

#训练模型后保存
saver = tf.train.Saver()
#保存模型
saver.save(sess,"net/test_net.ckpt")

完整代码

#载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True) #每个批次100张照片
batch_size=100 #计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size #定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10]) #创建一个简单的神经网络,输入层784个单元,输出层10个单元
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b) #二次代价函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) #初始化变量
init = tf.global_variables_initializer() #结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) saver = tf.train.Saver() with tf.Session() as sess:
sess.run(init)
for epoch in range(11):
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys}) acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc)) #保存模型
saver.save(sess,"net/test_net.ckpt")

重行导入模型,同时比较光初始化后的训练模型和经过优化(梯度算法)后的训练模型

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data #载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True) #每个批次100张照片
batch_size=100 #计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size #定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10]) #创建一个简单的神经网络,输入层784个单元,输出层10个单元
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b) #二次代价函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) #初始化变量
init = tf.global_variables_initializer() #结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) #求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) saver = tf.train.Saver() with tf.Session() as sess:
sess.run(init)
print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))
#重行载入训练好的模型
saver.restore(sess,"net/test_net.ckpt")
print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})) result:
0.098
INFO:tensorflow:Restoring parameters from net/test_net.ckpt
0.9168

8.3下载google图像识别inception-v3

同时创建了inception_model和inception_log两个文件夹

import tensorflow as tf
import os
import tarfile
import requests #模型下载地址
inception_pretrain_model_url = "http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz" #模型存放地址
inception_pretrain_model_dir = "inception_model"
if not os.path.exists(inception_pretrain_model_dir):
os.makedirs(inception_pretrain_model_dir) #获取文件名,以及文件路径
filename = inception_pretrain_model_dir.split('/')[-1]
filepath = os.path.join(inception_pretrain_model_dir,filename)
#路径拼接:inception_model/inception-2015-12-05.tgz #模型下载
if not os.path.exists(filepath):
print("download:",filename)
r = requests.get(inception_pretrain_model_url)
with open(filepath,"wb") as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk) print("finished:",filename)
#解压文件
tarfile.open(filepath,"r:gz").extractall(inception_pretrain_model_dir) #模型结构存放文件
log_dir = "inception_log"
if not os.path.exists(log_dir):
os.makedirs(log_dir) inception_graph_def_file = os.path.join(inception_pretrain_model_dir,"classify_image_graph_def.pb")
with tf.Session() as sess:
#创建图来存放google训练好的模型
with tf.gfile.FastGFile(inception_graph_def_file,"rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def,name="")
#保存图的结构
writer = tf.summary.FileWriter(log_dir,sess.graph)
writer.close()

tensorflow学习框架(炼数成金网络版学习记录)

tensorflow学习框架(炼数成金网络版学习记录)

8.4根据8.3建立的inception-v3模型做图像处理

随机导入图片至images下, 导入os,numpy,re,Image,matplotlib.pyplot;

其次定义图存放训练好的google模型;

开启会话后,输出训练模型和置信度。

import tensorflow as tf
import os
import numpy as np
import re
from PIL import Image
import matplotlib.pyplot as plt class NodeLookup(object):
def __init__(self):
label_lookup_path = "inception_model/imagenet_2012_challenge_label_map_proto.pbtxt"
uid_lookup_path = "inception_model/imagenet_synset_to_human_label_map.txt"
self.node_lookup = self.load(label_lookup_path,uid_lookup_path) def load(self, label_lookup_path, uid_lookup_path):
#加载分类字符串,对应分类名称的文件
proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines()
uid_to_human = {}
#一行一行读取数据
for line in proto_as_ascii_lines:
#去掉换行符
line = line.strip("\n")
#按照"\t"分割
parsed_items = line.split("\t")
#获取分类编号
uid = parsed_items[0] #pbtxt头列数据
#获取分类名称
string = parsed_items[1]
#保存编号字符串**与分类名称映射关系
uid_to_human[uid] = string #uid_to_human[uid] = string如下所示
#n00004475 organism, being
#n00005787 benthos
#n00006024 heterotroph #增加分类字符串n**对应分类编号1-1000的文件
proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()
node_id_to_uid = {}
for line in proto_as_ascii:
if line.startswith(" target_class:"):
#获取分类编号1-1000
target_class = int(line.split(": ")[1])
if line.startswith(" target_class_string:"):
#获取该编号字符串
target_class_string = line.split(": ")[1]
#保存分类编号1-1000与编号字符串n**映射关系
node_id_to_uid[target_class] = target_class_string[1:-2]
#
#entry {
# target_class: 449
# target_class_string: "n01440764"
# }
#建立分类编号1-1000对应分类名称的映射关系
node_id_to_name = {} #target_class中存放分类编号
for key,val in node_id_to_uid.items(): #key = 449,val = n01440764
#获取分类名称
name = uid_to_human[val] #两个表中靠uid来建立联系,有点类似SQL中的连接关系
#建立分类编号1-1000与分类名称的映射关系
node_id_to_name[key] = name
return node_id_to_name #返回一堆字典 #传入分类编号1-1000返回分类名称
def id_to_string(self,node_id):
if node_id not in self.node_lookup:
return ""
return self.node_lookup[node_id] #以上对两个文件进行预处理
#创建一个图来存放google训练好的模型
with tf.gfile.FastGFile("inception_model/classify_image_graph_def.pb","rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def,name="") with tf.Session() as sess:
softmax_tensor = sess.graph.get_tensor_by_name("softmax:0")
#遍历目录
for root,dirs,files in os.walk("images/"): #root是目录,dirs是子目录,files是目录里的文件
for file in files:
#载入图片
image_data = tf.gfile.FastGFile(os.path.join(root,file),"rb").read()
predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0': image_data})#图片格式是jpg格式
#把结果转为1维数据
predictions = np.squeeze(predictions) #打印图片路径和名称
image_path = os.path.join(root,file)
print(image_path)
#显示图片
img=Image.open(image_path)
plt.imshow(img)
plt.axis("off")
plt.show() #排序
top_probablity_k = predictions.argsort()[-5:][::-1] #取概率最大的五个数,进行从小到大排序
node_lookup = NodeLookup() #实现类
for node_id in top_probablity_k:
#获取分类名称
human_string = node_lookup.id_to_string(node_id) #1-1000的编号
#获取该分类的置信度
score = predictions[node_id]
#获取该分类的置信度
print("%s (score = %.5f)"%(human_string,score))
print()

inception网络的优点:

前言:为什么引入inception-v1网络? 为了克服Alexnet和Vgg参数量多、训练等问题,inception出发点就是通过稀疏连接进行近似成“large and dense layers”

inception优点:1)增加网络宽度;2)增加网络对尺度的适应性,提高网络内部计算资源利用率;3)1x1减少网络参数,起到信息融合作用。

tensorflow学习框架(炼数成金网络版学习记录)

其次,引入inception-v2;

1)加入了BN层,减少了Internal Covariate Shift(内部neuron的数据分布发生变化),使每一层的输出都规范化到一个N(0, 1)的高斯;

2)参考VGG(visual Geometry Group)用2个3x3的conv替代inception模块中的5x5,既降低了参数数量,也加速计算;

tensorflow学习框架(炼数成金网络版学习记录)

引入inception-v3模块优点:

1)factorization因子化后,加速计算,将一些较大的卷积分解成几个较小的卷积,比如1个卷积层拆成2个卷积层(5x5--->3x3),使得网络深度进一步增加,增加网络的非线性;

2)其次的因子化是将大卷积变为小卷积,比如nxn的卷积分解成1xn和nx1卷积,如对3x3卷积进行分解,网络输入从224x224变为299x299,更加精细设计了35x35/17x17/8x8的模块。

tensorflow学习框架(炼数成金网络版学习记录)
tensorflow学习框架(炼数成金网络版学习记录)
细节:

1)在辅助层加入了BN-auxiliary;

2)全连接层后面也进行BN操作。

tensorflow学习框架(炼数成金网络版学习记录)

inception-v3网络结构

tensorflow学习框架(炼数成金网络版学习记录)

tensorflow学习框架(炼数成金网络版学习记录)

tensorflow学习框架(炼数成金网络版学习记录)

另外一点是Inception-v3采用了一种Label Smoothing技术来正则化模型,提升泛化能力。

至于inception-v4,模型几乎与v3类似,但是v4对版本进行了梳理,原始模型采用分区训练方法,而迁移到tensorflow框架后可以对inception进行一定的规范和细化,inception-v4整体结构如下,模型采用299x299输入。

在输入inception之前,有一个stem模块,它的输入是35x35的。

tensorflow学习框架(炼数成金网络版学习记录)

inception-v4网络结构

tensorflow学习框架(炼数成金网络版学习记录)

inception-v4网络模块

chpater09

9.1.1定制分类学习任务的三种方法:

1.从无到有,重新开始训练模型:准备数据集,模型框架和参数;

2.准备好inception-v3模型:不修改用于提取特征的部分,修改用于分类的部分

卷积层作用:图像特征提取

mixed1-mixed10用于特征提取

3.对模型特征提取的卷积+池化层的参数作微调(比如修改learning-rate到较小值),主要调整用于分类的部分

9.1.2任务模型思路:

用inception-v3训练图片集,再写程序对新导入的图片进行识别

9.1.3作业

收集若干种类图片

写博客做记录,找数据集,存放位置

写批处理脚本,检测模型质量

chpater10