TensorFlow 使用及手写字体训练

tensorflow入门

TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数组,即张量(tensor)。它灵活的架构让你可以在多种平台上展开计算,例如台式计算机中的一个或多个CPU(或GPU),服务器,移动设备等等。TensorFlow 最初由Google大脑小组(隶属于Google机器智能研究机构)的研究员和工程师们开发出来,用于机器学习和深度神经网络方面的研究,但这个系统的通用性使其也可广泛用于其他计算领域。(复制过来的)
中文社区链接:链接

安装:

python3:
终端下执行: pip(或者pip3) install --upgrade tensourflow

优势:

我们在进行深度学习的使用往往会进行后向传播,后向传播的实质是根据神经元微分求解,而tensorflow则不用考虑微积分,只实现前向传播就能运行。

f(b, c) = (b+3)*(b+c)

用tensorflow实现此函数(jupyter写的有输出内容),如下:

单个值测试

1
2
3
4
5
import tensorflow as tf #导入tf包
import numpy as np #导入数组包
'''
用tensorflow 实现 f(c, b) = (b+3)*(b+c)
'''
'\n用tensorflow 实现 f(c, b) = (b+3)*(b+c)\n'
1
2
b = tf.Variable(2., name='b') #定义一个2.0变量为b
c = tf.Variable(3., name='c') #定义一个3.0变量为c
1
2
3
4
5
#不妨设中间变量 e = (b+3) ,d = (b+c) ,a= f(b, c)吧
three = tf.constant(3., name='three') #定义一个常量3

e = tf.add(b,three)
d = tf.add(b, c)
1
2
a = tf.multiply(e, d) #乘法
print(a) #结果不是25 而是一个Tensor
Tensor("Mul:0", shape=(), dtype=float32)
1
2
3
4
5
6
7
8
#真实的运行结果如下:
init_top = tf.global_variables_initializer() #初始化变量
with tf.Session() as sess:
#打开一个tensor会话
sess.run(init_top) #顺序不能变,先运行初始化
f = sess.run(a) #运行tensor a
print(f)
sess.close() #关闭会话
25.0

多组值测试

这时候我们用到tensorflow的容器

1
2
import tensorflow as tf #导入tf包
import numpy as np #导入数组包
1
2
b = tf.placeholder(tf.float32, [None, 1],name='b') #定义一个b容器,大小可以不确定,[3,1]即为3个元素,None,为未知,来源数据大小
c = tf.Variable(3., name='c') #定义一个3.0变量为c
1
2
3
4
#不妨设中间变量 e = (b+3) ,d = (b+c) ,a= f(b, c)吧
three = tf.constant(3., name='three') #定义一个常量3
e = tf.add(b,three)
d = tf.add(b, c)
1
2
a = tf.multiply(e, d) #乘法
print(a) #结果不是25 而是一个Tensor
Tensor("Mul:0", shape=(?, 1), dtype=float32)
1
2
3
4
5
6
7
8
9
#真实的运行结果如下:
b_array = np.array([1,2,3,4,5]).reshape(5,1) #创建一个b的元素数组
init_top = tf.global_variables_initializer() #初始化变量
with tf.Session() as sess:
#打开一个tensor会话
sess.run(init_top) #顺序不能变,先运行初始化
f = sess.run(a,feed_dict={b: b_array}) #运行tensor a, 喂食词典 b的元素 来源于 b_array
print(f) #输出必然是多组值
sess.close() #关闭会话
[[16.]
 [25.]
 [36.]
 [49.]
 [64.]]

好了函数介绍就到这

MNIST 手写字体训练

注释解释的很清楚,推荐有基础的人去看,新手会完全看不懂的

1
2
3
4
5
6
7
8
9
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline
plt.rcParams['figure.figsize'] = (13.0, 11.0) #设置plt的大小
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
1
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)  #加载mnist数据集
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
1
2
3
4
5
learning_rate = 0.05  #学习率
epochs = 20 #迭代大次数
batch_size = 50 #迭代小次数,也是每次训练数据集的数据量 总次数 20*50
x = tf.placeholder(tf.float32, [None, 784], name='x') #定义变量x 原图为64*64的数据
y = tf.placeholder(tf.float32, [None, 10], name='y') #
1
2
3
4
5
6
7
8
9
10
#设置了3层神经元结构  第一次为300个 第二层 200个 最后输出层 10
W1 = tf.Variable(tf.random_normal([784, 300], stddev=0.01), name='W1')
#tf的正则化表示,不了解的可以忽略下行
tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(0.005)(W1))
b1 = tf.Variable(tf.random_normal([300]), name='b1')
W2 = tf.Variable(tf.random_normal([300, 200], stddev=0.01), name='W2')
tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(0.005)(W2))
b2 = tf.Variable(tf.random_normal([200]), name='b2')
W3 = tf.Variable(tf.random_normal([200, 10], stddev=0.01), name='W3')
b3 = tf.Variable(tf.random_normal([10]), name='b3')
1
2
3
4
5
hd_out1 = tf.add(tf.matmul(x,W1), b1)
hd_out1 = tf.nn.relu(hd_out1) #第一层的输出 用的是relu函数

hd_out2 = tf.add(tf.matmul(hd_out1,W2), b2)
hd_out2 = tf.nn.relu(hd_out2) #第二层的输出
1
y_ = tf.nn.softmax(tf.add(tf.matmul(hd_out2, W3), b3)) #最终输出 个数为10
1
2
3
4
y_clipped = tf.clip_by_value(y_, 1e-10, 0.9999999) #过滤
cross_entropy = -tf.reduce_mean(tf.reduce_sum(y*tf.log(y_clipped)+(1-y)*tf.log(1-y_clipped), axis=1)) #求解交叉熵
tf.add_to_collection('losses', cross_entropy)
loss = tf.add_n(tf.get_collection('losses')) #将交叉熵放入losses集,下面会用到
1
2
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train = optimizer.minimize(loss) #选用梯度下降算法,来进行学习,选用losses里面的最小值
1
2
3
4
5
init_top = tf.global_variables_initializer()
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) #求正确率
#保存模型
saver = tf.train.Saver()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
with tf.Session() as sess:
sess.run(init_top)
total_batch = int(len(mnist.train.labels)/ batch_size)
costs = []
for ep in range(epochs):
avg_cost = 0
for i in range(total_batch):
batch_x, batch_y = mnist.train.next_batch(batch_size)
_t, c = sess.run([train, cross_entropy], feed_dict={x:batch_x, y:batch_y})
avg_cost += c/total_batch
print('epoch%d,cost=%f'% (ep+1,avg_cost))
costs.append(avg_cost)
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}))
saver.save(sess,'./', global_step=epochs) #保存模型
sess.close()
costs = np.squeeze(costs)
plt.plot(costs)
plt.show()
epoch1,cost=1.764043
epoch2,cost=0.553732
epoch3,cost=0.377033
epoch4,cost=0.296909
epoch5,cost=0.250849
epoch6,cost=0.222690
epoch7,cost=0.203826
epoch8,cost=0.187270
epoch9,cost=0.176289
epoch10,cost=0.169246
epoch11,cost=0.160853
epoch12,cost=0.156802
epoch13,cost=0.151215
epoch14,cost=0.144273
epoch15,cost=0.142850
epoch16,cost=0.134713
epoch17,cost=0.135944
epoch18,cost=0.133344
epoch19,cost=0.129857
epoch20,cost=0.126890
0.9725

alt

emmm, 蜜汁随缘调参职能到97.25%了