本篇博文只是用来记录自己的学习过程,作为2018年积累的开始,从这篇博文开始,我要养成一周一篇博文的习惯。今年要实现50篇博文的目标。
目录
- 目录
- tensorflow的基础使用
- 创建图启动图
- 变量
- fetch and feed
- tensorflow的简单使用
- tensorflow线性回归以及分类的简单实用softmax介绍
- 交叉熵过拟合dropout以及tensorflow中各种优化器的介绍
- 交叉熵
- dropout
- optimizer优化器
- 本章作业继续优化网络提高识别率
- tensorboard可视化
- tensorboard网络结构
- tensorboard网络运行
- tensorboard可视化
- 卷积神经网络
- 卷积神经网络应用于MNIST数据集分类
- 作业卷积神经网络可视化
- 递归神经网络LSTM的讲解以及LSTM网络的使用
tensorflow的基础使用
创建图,启动图
import tensorflow as tf
#创建一个常量op
m1 = tf.constant([[3,3]])
#创建一个常量op
m2 = tf.constant([[2],[3]])
#创建一个矩阵乘法op,把m1和m2传入
product = tf.matmul(m1,m2)
print(product)
结果是:
Tensor(“MatMul_1:0”, shape=(1, 1), dtype=int32)
说明这是一个张量
#定义一个会话,自动启动默认图
sess = tf.Session()
#调用sess的run方法来执行矩阵乘法op
#run(product)触发了图中3个op
result = sess.run(product)
print(result)
sess.close()
结果是:
[[15]]
需要建立一个会话,才能得到两个常量的乘积
with tf.Session() as sess:
result = sess.run(product)
print(result)
变量
import tensorflow as tf
#定义变量
x = tf.Variable([1,2])
a = tf.constant([3,3])
#定义减法和加法的op
sub = tf.subtract(x,a)
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))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
[-2 -1]
[-1 1]
state = tf.Variable(0,name='counter')
new_value = tf.add(state,1)
update = tf.assign(state,new_value)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(state))
for _ in range(5):
sess.run(update)
print(sess.run(state))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
运行结果:
0
1
2
3
4
5
fetch and feed
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([add,mul])
print(result)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
[7.0, 21.0]
#Feed
#创建占位符 :placeholder
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
#可以在运行output时,再将input1和input2的值传入,传入时采用字典的形式
output = tf.multiply(input1,input2)
with tf.Session() as sess:
#传入时采用字典的形式
print(sess.run(output,feed_dict={input1:[8.],input2:[3.]}))
[ 24.]
tensorflow的简单使用
import tensorflow as tf
import numpy as np
#使用numpy生成100个随机点
x_data = np.random.rand(100)
y_data = x_data*0.1 + 0.2
#构建一个线性模型
b = tf.Variable(0.)
k = tf.Variable(0.)
y = x_data*k + b
#使用tensorflow训练出模型,即得到k和b的值
#定义二次代价函数
loss = tf.reduce_mean(tf.square(y_data - y))
#使用梯度下降函数来进行训练的优化器,下面学习率是0.2
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]))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
0 [0.0517033, 0.099609137]
20 [0.1019343, 0.1989941]
40 [0.10121739, 0.19936697]
60 [0.10076618, 0.19960159]
80 [0.10048222, 0.19974925]
100 [0.1003035, 0.19984218]
120 [0.10019101, 0.19990067]
140 [0.10012019, 0.19993751]
160 [0.10007564, 0.19996066]
180 [0.10004761, 0.19997525]
200 [0.10002997, 0.19998442]
tensorflow线性回归以及分类的简单实用,softmax介绍
非线性回归
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
x_data = np.linspace(-0.5,0.5,200)[:,np.newaxis]
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])
Weight_L1 = tf.Variable(tf.random_normal([1,10]))
biases_L1 = tf.Variable(tf.zeros([1,10]))
Wx_plus_b_L1 = tf.matmul(x,Weight_L1) + biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1)
Weight_L2 = tf.Variable(tf.random_normal([10,1]))
biases_L2 = tf.Variable(tf.zeros([1,1]))
Wx_plus_b_L2 = tf.matmul(L1,Weight_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()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
MNIST数据集分类简单版
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size
#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#创建一个简单的神经网络
Weight_L1 = tf.Variable(tf.zeros([784,10]))
biases_L1 = tf.Variable(tf.zeros([10]))
Wx_plus_b_L1 = tf.matmul(x,Weight_L1) + biases_L1
prediction = tf.nn.softmax(Wx_plus_b_L1)
#二次代价函数
loss = tf.reduce_mean(tf.square(y-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)) #argmax返回一个张量中最大值存在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将布尔型转换为float32, true->1 false ->0
with tf.Session() as sess:
sess.run(init)
for epoch in range(21):
for batch in range (n_batch):
#获取每个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))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
Iter 0,Testing Accuracy 0.8318
Iter 1,Testing Accuracy 0.8694
Iter 2,Testing Accuracy 0.8811
Iter 3,Testing Accuracy 0.8876
Iter 4,Testing Accuracy 0.8932
Iter 5,Testing Accuracy 0.897
Iter 6,Testing Accuracy 0.8992
Iter 7,Testing Accuracy 0.9021
Iter 8,Testing Accuracy 0.9024
Iter 9,Testing Accuracy 0.9056
Iter 10,Testing Accuracy 0.9064
Iter 11,Testing Accuracy 0.9063
Iter 12,Testing Accuracy 0.9079
Iter 13,Testing Accuracy 0.9095
Iter 14,Testing Accuracy 0.9096
Iter 15,Testing Accuracy 0.9114
Iter 16,Testing Accuracy 0.9113
Iter 17,Testing Accuracy 0.9121
Iter 18,Testing Accuracy 0.913
Iter 19,Testing Accuracy 0.9137
Iter 20,Testing Accuracy 0.9135
#第三章作业,修改神经网络使得识别率达到95%以上
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size
#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#创建一个简单的神经网络
Weight_L1 = tf.Variable(tf.zeros([784,20]))
biases_L1 = tf.Variable(tf.zeros([20]))
Wx_plus_b_L1 = tf.matmul(x,Weight_L1) + biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1)
#定义神经网络输出层
Weight_L2 = tf.Variable(tf.random_normal([20,10]))
biases_L2 = tf.Variable(tf.zeros([10]))
Wx_plus_b_L2 = tf.matmul(L1,Weight_L2) + biases_L2
prediction = tf.nn.softmax(Wx_plus_b_L2)
#二次代价函数
loss = tf.reduce_mean(tf.square(y-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)) #argmax返回一个张量中最大值存在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将布尔型转换为float32, true->1 false ->0
with tf.Session() as sess:
sess.run(init)
for epoch in range(100):
for batch in range (n_batch):
#获取每个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))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
交叉熵,过拟合,dropout以及tensorflow中各种优化器的介绍
交叉熵
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#将二次代价函数替换成交叉熵
# tf.nn.sigmod_cross_entropy_with_logits() 来表示跟sigmod搭配使用的交叉熵
# tf.nn.softmax_cross_entropy_with_logits() 来表示跟softmax搭配使用的交叉熵
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size
#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#创建一个简单的神经网络
Weight_L1 = tf.Variable(tf.zeros([784,10]))
biases_L1 = tf.Variable(tf.zeros([10]))
Wx_plus_b_L1 = tf.matmul(x,Weight_L1) + biases_L1
prediction = tf.nn.softmax(Wx_plus_b_L1)
#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))
#交叉熵
#首先看输入logits,它的shape是[batch_size, num_classes] ,一般来讲,就是神经网络最后一层的输入z。
#另外一个输入是labels,它的shape也是[batch_size, num_classes],就是我们神经网络期望的输出。
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)) #argmax返回一个张量中最大值存在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将布尔型转换为float32, true->1 false ->0
with tf.Session() as sess:
sess.run(init)
for epoch in range(21):
for batch in range (n_batch):
#获取每个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))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
Iter 0,Testing Accuracy 0.8242
Iter 1,Testing Accuracy 0.8941
Iter 2,Testing Accuracy 0.9006
Iter 3,Testing Accuracy 0.9053
Iter 4,Testing Accuracy 0.9087
Iter 5,Testing Accuracy 0.9102
Iter 6,Testing Accuracy 0.9111
Iter 7,Testing Accuracy 0.9139
Iter 8,Testing Accuracy 0.9151
Iter 9,Testing Accuracy 0.9156
Iter 10,Testing Accuracy 0.9172
Iter 11,Testing Accuracy 0.9189
Iter 12,Testing Accuracy 0.919
Iter 13,Testing Accuracy 0.9186
Iter 14,Testing Accuracy 0.9213
Iter 15,Testing Accuracy 0.9216
Iter 16,Testing Accuracy 0.9209
Iter 17,Testing Accuracy 0.9212
Iter 18,Testing Accuracy 0.9216
Iter 19,Testing Accuracy 0.9211
Iter 20,Testing Accuracy 0.9218
dropout
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#将二次代价函数替换成交叉熵
# tf.nn.sigmod_cross_entropy_with_logits() 来表示跟sigmod搭配使用的交叉熵
# tf.nn.softmax_cross_entropy_with_logits() 来表示跟softmax搭配使用的交叉熵
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size
#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob = tf.placeholder(tf.float32) #dropout的参数,表示多少神经元被激活,1 表示所有神经元都是工作的
#创建一个简单的神经网络
Weight_L1 = tf.Variable(tf.truncated_normal([784,2000],stddev=0.1))
biases_L1 = tf.Variable(tf.zeros([2000])+0.1)
Wx_plus_b_L1 = tf.nn.tanh(tf.matmul(x,Weight_L1) + biases_L1)
L1_drop = tf.nn.dropout(Wx_plus_b_L1,keep_prob)
Weight_L2 = tf.Variable(tf.truncated_normal([2000,2000],stddev=0.1))
biases_L2 = tf.Variable(tf.zeros([2000])+0.1)
Wx_plus_b_L2 = tf.nn.tanh(tf.matmul(L1_drop,Weight_L2) + biases_L2)
L2_drop = tf.nn.dropout(Wx_plus_b_L2,keep_prob)
Weight_L3 = tf.Variable(tf.truncated_normal([2000,1000],stddev=0.1))
biases_L3 = tf.Variable(tf.zeros([1000])+0.1)
Wx_plus_b_L3 = tf.nn.tanh(tf.matmul(L2_drop,Weight_L3) + biases_L3)
L3_drop = tf.nn.dropout(Wx_plus_b_L3,keep_prob)
Weight_L4 = tf.Variable(tf.truncated_normal([1000,10],stddev=0.1))
biases_L4 = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(L3_drop,Weight_L4) + biases_L4)
#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))
#交叉熵
#首先看输入logits,它的shape是[batch_size, num_classes] ,一般来讲,就是神经网络最后一层的输入z。
#另外一个输入是labels,它的shape也是[batch_size, num_classes],就是我们神经网络期望的输出。
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)) #argmax返回一个张量中最大值存在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #将布尔型转换为float32, true->1 false ->0
with tf.Session() as sess:
sess.run(init)
for epoch in range(21):
for batch in range (n_batch):
#获取每个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))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
optimizer优化器
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#将二次代价函数替换成交叉熵
# tf.nn.sigmod_cross_entropy_with_logits() 来表示跟sigmod搭配使用的交叉熵
# tf.nn.softmax_cross_entropy_with_logits() 来表示跟softmax搭配使用的交叉熵
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size
#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#创建一个简单的神经网络
Weight_L1 = tf.Variable(tf.zeros([784,10]))
biases_L1 = tf.Variable(tf.zeros([10]))
Wx_plus_b_L1 = tf.matmul(x,Weight_L1) + biases_L1
prediction = tf.nn.softmax(Wx_plus_b_L1)
#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))
#交叉熵
#首先看输入logits,它的shape是[batch_size, num_classes] ,一般来讲,就是神经网络最后一层的输入z。
#另外一个输入是labels,它的shape也是[batch_size, num_classes],就是我们神经网络期望的输出。
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
#使用梯度下降法训练
#train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#使用其他优化器
train_step = tf.train.AdamOptimizer(1e-2).minimize(loss)
#初始化变量
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)) #将布尔型转换为float32, true->1 false ->0
with tf.Session() as sess:
sess.run(init)
for epoch in range(21):
for batch in range (n_batch):
#获取每个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))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
Iter 0,Testing Accuracy 0.9219
Iter 1,Testing Accuracy 0.9259
Iter 2,Testing Accuracy 0.9234
Iter 3,Testing Accuracy 0.9283
Iter 4,Testing Accuracy 0.93
Iter 5,Testing Accuracy 0.9295
Iter 6,Testing Accuracy 0.9289
Iter 7,Testing Accuracy 0.931
Iter 8,Testing Accuracy 0.9324
Iter 9,Testing Accuracy 0.9294
Iter 10,Testing Accuracy 0.9309
Iter 11,Testing Accuracy 0.9313
Iter 12,Testing Accuracy 0.9297
Iter 13,Testing Accuracy 0.9319
Iter 14,Testing Accuracy 0.9295
Iter 15,Testing Accuracy 0.9304
Iter 16,Testing Accuracy 0.9323
Iter 17,Testing Accuracy 0.9324
Iter 18,Testing Accuracy 0.9314
Iter 19,Testing Accuracy 0.9318
Iter 20,Testing Accuracy 0.9307
本章作业,继续优化网络,提高识别率
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#将二次代价函数替换成交叉熵
# tf.nn.sigmod_cross_entropy_with_logits() 来表示跟sigmod搭配使用的交叉熵
# tf.nn.softmax_cross_entropy_with_logits() 来表示跟softmax搭配使用的交叉熵
#载入数据集
#one_hot是将数据改成0-1的格式
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size
#定义两个placeholder,一个图片是28x28=784
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob = tf.placeholder(tf.float32) #dropout的参数,表示多少神经元被激活,1 表示所有神经元都是工作的
#学习率的变量
lr = tf.Variable(0.001,dtype = tf.float32)
#创建一个简单的神经网络
Weight_L1 = tf.Variable(tf.truncated_normal([784,500],stddev=0.1))
biases_L1 = tf.Variable(tf.zeros([500])+0.1)
Wx_plus_b_L1 = tf.nn.tanh(tf.matmul(x,Weight_L1) + biases_L1)
L1_drop = tf.nn.dropout(Wx_plus_b_L1,keep_prob)
Weight_L2 = tf.Variable(tf.truncated_normal([500,300],stddev=0.1))
biases_L2 = tf.Variable(tf.zeros([300])+0.1)
Wx_plus_b_L2 = tf.nn.tanh(tf.matmul(L1_drop,Weight_L2) + biases_L2)
L2_drop = tf.nn.dropout(Wx_plus_b_L2,keep_prob)
Weight_L3 = tf.Variable(tf.truncated_normal([300,10],stddev=0.1))
biases_L3 = tf.Variable(tf.zeros([10])+0.1)
#Wx_plus_b_L3 = tf.nn.tanh(tf.matmul(L2_drop,Weight_L3) + biases_L3)
#L3_drop = tf.nn.dropout(Wx_plus_b_L3,keep_prob)
#Weight_L4 = tf.Variable(tf.truncated_normal([300,10],stddev=0.1))
#biases_L4 = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(L2_drop,Weight_L3) + biases_L3)
#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))
#交叉熵
#首先看输入logits,它的shape是[batch_size, num_classes] ,一般来讲,就是神经网络最后一层的输入z。
#另外一个输入是labels,它的shape也是[batch_size, num_classes],就是我们神经网络期望的输出。
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
#使用梯度下降法训练
#train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#使用其他优化器
train_step = tf.train.AdamOptimizer(lr).minimize(loss)
#初始化变量
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)) #将布尔型转换为float32, true->1 false ->0
with tf.Session() as sess:
sess.run(init)
for epoch in range(51):
sess.run(tf.assign(lr,0.001 * (0.95 ** epoch)))
for batch in range (n_batch):
#获取每个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)+", training Accuracy "+str(train_acc))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
Iter 0,Testing Accuracy 0.952, training Accuracy 0.955818
Iter 1,Testing Accuracy 0.9604, training Accuracy 0.9672
Iter 2,Testing Accuracy 0.9675, training Accuracy 0.976364
Iter 3,Testing Accuracy 0.9712, training Accuracy 0.982236
Iter 4,Testing Accuracy 0.9736, training Accuracy 0.985655
Iter 5,Testing Accuracy 0.9751, training Accuracy 0.988509
Iter 6,Testing Accuracy 0.9755, training Accuracy 0.989909
Iter 7,Testing Accuracy 0.9752, training Accuracy 0.990855
Iter 8,Testing Accuracy 0.9784, training Accuracy 0.992382
Iter 9,Testing Accuracy 0.977, training Accuracy 0.9936
Iter 10,Testing Accuracy 0.9782, training Accuracy 0.993345
Iter 11,Testing Accuracy 0.9764, training Accuracy 0.9944
Iter 12,Testing Accuracy 0.978, training Accuracy 0.994891
Iter 13,Testing Accuracy 0.9789, training Accuracy 0.995309
Iter 14,Testing Accuracy 0.9792, training Accuracy 0.995327
Iter 15,Testing Accuracy 0.9787, training Accuracy 0.995309
Iter 16,Testing Accuracy 0.9792, training Accuracy 0.9958
Iter 17,Testing Accuracy 0.9799, training Accuracy 0.996
Iter 18,Testing Accuracy 0.9801, training Accuracy 0.995927
Iter 19,Testing Accuracy 0.9806, training Accuracy 0.9962
Iter 20,Testing Accuracy 0.9805, training Accuracy 0.996055
Iter 21,Testing Accuracy 0.9815, training Accuracy 0.9964
Iter 22,Testing Accuracy 0.9808, training Accuracy 0.996545
Iter 23,Testing Accuracy 0.9802, training Accuracy 0.996273
Iter 24,Testing Accuracy 0.982, training Accuracy 0.996618
Iter 25,Testing Accuracy 0.9814, training Accuracy 0.996709
Iter 26,Testing Accuracy 0.9808, training Accuracy 0.996727
Iter 27,Testing Accuracy 0.9815, training Accuracy 0.9968
Iter 28,Testing Accuracy 0.9813, training Accuracy 0.996836
Iter 29,Testing Accuracy 0.9818, training Accuracy 0.997018
Iter 30,Testing Accuracy 0.9819, training Accuracy 0.997036
Iter 31,Testing Accuracy 0.9818, training Accuracy 0.997091
Iter 32,Testing Accuracy 0.9822, training Accuracy 0.997145
Iter 33,Testing Accuracy 0.9812, training Accuracy 0.9972
Iter 34,Testing Accuracy 0.9813, training Accuracy 0.997255
Iter 35,Testing Accuracy 0.9822, training Accuracy 0.997273
Iter 36,Testing Accuracy 0.9817, training Accuracy 0.997309
Iter 37,Testing Accuracy 0.9824, training Accuracy 0.997345
Iter 38,Testing Accuracy 0.9809, training Accuracy 0.997345
Iter 39,Testing Accuracy 0.9819, training Accuracy 0.997364
Iter 40,Testing Accuracy 0.981, training Accuracy 0.997236
Iter 41,Testing Accuracy 0.9818, training Accuracy 0.9974
Iter 42,Testing Accuracy 0.9821, training Accuracy 0.997418
Iter 43,Testing Accuracy 0.9816, training Accuracy 0.997473
Iter 44,Testing Accuracy 0.9815, training Accuracy 0.997473
Iter 45,Testing Accuracy 0.9817, training Accuracy 0.997491
Iter 46,Testing Accuracy 0.9817, training Accuracy 0.997491
Iter 47,Testing Accuracy 0.9819, training Accuracy 0.997491
Iter 48,Testing Accuracy 0.9819, training Accuracy 0.997509
Iter 49,Testing Accuracy 0.9819, training Accuracy 0.997527
Iter 50,Testing Accuracy 0.9818, training Accuracy 0.997527
tensorboard可视化
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'):
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'):
Weight_L1 = tf.Variable(tf.zeros([784,10]),name='W')
with tf.name_scope('biases'):
biases_L1 = tf.Variable(tf.zeros([10]),name='b')
with tf.name_scope('wx_plus_b'):
Wx_plus_b_L1 = tf.matmul(x,Weight_L1) + biases_L1
with tf.name_scope('softmax'):
prediction = tf.nn.softmax(Wx_plus_b_L1)
with tf.name_scope('loss'):
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=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))
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('C:/Users/zgyxf183/Desktop/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))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
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
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'):
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)
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))
with tf.name_scope('accuracy'):
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
tf.summary.scalar('accuracy',accuracy)
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))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
tensorboard可视化
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib.tensorboard.plugins import projector
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
max_steps = 1001
image_num = 3000
DIR = "C:/Users/zgyxf183/Documents/jupyter/tensorFlow Learning/"
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'):
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.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.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))
with tf.name_scope('accuracy'):
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
tf.summary.scalar('accuracy',accuracy)
if tf.gfile.Exists(DIR + 'projector/projector/metadata.tsv'):
tf.gfile.DeleteRecursively(DIR + 'projector/projector/metadata.tsv')
with open(DIR + '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')
merged = tf.summary.merge_all()
projector_writer = tf.summary.FileWriter(DIR + 'projector/projector',sess.graph)
saver = tf.train.Saver()
config = projector.ProjectorConfig()
embed = config.embeddings.add()
embed.tensor_name = embedding.name
embed.metadata_path = DIR + 'projector/projector/metadata.tsv'
embed.sprite.image_path = DIR + '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):
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 + 'projector/projector/a_model.ckpt', global_step=max_steps)
projector_writer.close()
sess.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
卷积神经网络
卷积神经网络应用于MNIST数据集分类
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
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(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
x_image = tf.reshape(x,[-1,28,28,1])
W_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
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([7*7*64,1024])
b_fc1 = bias_variable([1024])
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 = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
W_fc2 = weight_variable([1024,10])
b_fc2 = bias_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2) + b_fc2)
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
train_step = tf.train.AdamOptimizer(1e-4).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))
with tf.Session() as sess:
sess.run(init)
for epoch in range(51):
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})
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))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
作业,卷积神经网络可视化
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
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)
def weight_variable(shape,name):
initial = tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(initial,name=name)
def bias_variable(shape,name):
initial = tf.constant(0.1,shape=shape)
return tf.Variable(initial,name=name)
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')
with tf.name_scope('input'):
x = tf.placeholder(tf.float32,[None,784],name='x-input')
y = tf.placeholder(tf.float32,[None,10],name='y-input')
with tf.name_scope('x_image'):
x_image = tf.reshape(x,[-1,28,28,1],name='x_image')
with tf.name_scope('Conv1'):
with tf.name_scope('W_conv1'):
W_conv1 = weight_variable([5,5,1,32],name='W_conv1')
with tf.name_scope('b_conv1'):
b_conv1 = bias_variable([32],name='b_conv1')
with tf.name_scope('conv2d_1'):
conv2d_1 = conv2d(x_image,W_conv1) + b_conv1
with tf.name_scope('relu'):
h_conv1 = tf.nn.relu(conv2d_1)
with tf.name_scope('h_pool1'):
h_pool1 = max_pool_2x2(h_conv1)
with tf.name_scope('Conv2'):
with tf.name_scope('W_conv2'):
W_conv2 = weight_variable([5,5,32,64],name='W_conv2')
with tf.name_scope('b_conv2'):
b_conv2 = bias_variable([64],name='b_conv2')
with tf.name_scope('conv2d_2'):
conv2d_2 = conv2d(h_pool1,W_conv2) + b_conv2
with tf.name_scope('relu'):
h_conv2 = tf.nn.relu(conv2d_2)
with tf.name_scope('h_pool2'):
h_pool2 = max_pool_2x2(h_conv2)
with tf.name_scope('fc1'):
with tf.name_scope('W_fc1'):
W_fc1 = weight_variable([7*7*64,1024],name='W_fc1')
with tf.name_scope('b_fc1'):
b_fc1 = bias_variable([1024],name='b_fc1')
with tf.name_scope('h_pool2_flat'):
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64],name='h_pool2_flat')
with tf.name_scope('wx_plus_b1'):
wx_plus_b1 = tf.matmul(h_pool2_flat,W_fc1) + b_fc1
with tf.name_scope('relu'):
h_fc1 = tf.nn.relu(wx_plus_b1)
with tf.name_scope('keep_prob'):
keep_prob = tf.placeholder(tf.float32,name='keep_prob')
with tf.name_scope('h_fc1_drop'):
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob,name='h_fc1_drop')
with tf.name_scope('fc2'):
with tf.name_scope('W_fc2'):
W_fc2 = weight_variable([1024,10],name='W_fc2')
with tf.name_scope('b_fc2'):
b_fc2 = bias_variable([10],name='b_fc2')
with tf.name_scope('wx_plus_b2'):
wx_plus_b2 = tf.matmul(h_fc1_drop,W_fc2) + b_fc2
with tf.name_scope('softmax'):
prediction = tf.nn.softmax(wx_plus_b2)
with tf.name_scope('cross_entropy'):
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction),name='cross_entropy')
tf.summary.scalar('cross_entropy',cross_entropy)
with tf.name_scope('train'):
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(prediction,1),tf.argmax(y,1))
with tf.name_scope('accuracy'):
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
tf.summary.scalar('accuracy',accuracy)
merged = tf.summary.merge_all()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
train_writer = tf.summary.FileWriter('logs/train',sess.graph)
test_writer = tf.summary.FileWriter('logs/test',sess.graph)
for i in range(1001):
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.5})
summary = sess.run(merged,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0})
train_writer.add_summary(summary,i)
batch_xs,batch_ys = mnist.test.next_batch(batch_size)
summary = sess.run(merged,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0})
test_writer.add_summary(summary,i)
if i%100==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[:10000],y:mnist.train.labels[:10000],keep_prob:1.0})
print ("Iter " + str(i) + ", Testing Accuracy= " + str(test_acc) + ", Training Accuracy= " + str(train_acc))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
递归神经网络LSTM的讲解,以及LSTM网络的使用
递归神经网络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
n_inputs = 28 #输入一行,一行有28个数据
max_time = 28 #一共28行
lstm_size = 100 #隐层单元
n_classes = 10 # 10个分类
batch_size = 50 #每批次50个样本
n_batch = mnist.train.num_examples // batch_size #计算一共有多少个批次
#这里的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))
#初始化偏置值
biases = tf.Variable(tf.constant(0.1, shape=[n_classes]))
#定义RNN网络
def RNN(X,weights,biases):
# inputs=[batch_size, max_time, n_inputs]
inputs = tf.reshape(X,[-1,max_time,n_inputs])
#定义LSTM基本CELL
lstm_cell = tf.contrib.rnn.core_rnn_cell.BasicLSTMCell(lstm_size)
# final_state[state, batch_size, cell.state_size]
# final_state[0]是cell state
# final_state[1]是hidden_state
# outputs: The RNN output `Tensor`.
# If time_major == False (default), this will be a `Tensor` shaped:
# `[batch_size, max_time, cell.output_size]`.
# If time_major == True, this will be a `Tensor` shaped:
# `[max_time, batch_size, cell.output_size]`.
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(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))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
Iter 0, Testing Accuracy= 0.7221
Iter 1, Testing Accuracy= 0.8016
Iter 2, Testing Accuracy= 0.8763
Iter 3, Testing Accuracy= 0.9103
Iter 4, Testing Accuracy= 0.9223
Iter 5, Testing Accuracy= 0.9311