注意:
该程序运行环境为:pycharm2020+python3.7+tensorflow2.2 cpu版本
因为在学习的过程时,学习视频使用的是tensorflow2.x以下的版本,所以在运行中出现了许多错误,不过已经更正。下面代码都可正常运行。需注意的是: 当导入mnist数据集时会报出 ModuleNotFoundError: No module named ‘tensorflow.examples.tutorials’ 错误
原因是tensorflow_core中缺tutorial文件夹
解决办法就是下载缺失的tutorial文件夹放到 tensorflow_core 目录下
但是tensorflow2.2可能找不到tensorflow_core,这是因为2.2版本将core合并到tensorflow,所以,将下载好的tutorial文件夹放在tensorflow的examples里即可,下载地址如下: tutorial文件【mnist数据集】
1、神经网络流程分析
1.1 步骤1使用的API
1.1.1 全连接-从输入直接到输出
特征加权:
tf.matmul(a, b,name=None)+bias
return:全连接结果,供交叉损失运算不需要激活函数(因为是最后的输出)
1.2 步骤2使用的API
1.2.1 SoftMax计算、交叉熵
tf.nn.softmax_cross_entropy_with_logits(labels=None, logits=None,name=None)
计算logits和labels之间的交叉损失熵
labels:标签值(真实值)logits:样本加权之后的值,预测值return:返回损失值列表
1.2.2 损失值列表平均值计算
tf.reduce_mean(input_tensor)
计算张量的尺寸的元素平均值
1.2.3 准确性计算
1、equal_list = tf.equal(tf.argmax(y, 1), tf.argmax(y_label, 1))
2、accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))
2、手写数字识别案例
2.1 单层(全连接层)实现⼿写数字识别流程分析
特征值[None, 784] ⽬标值[None, 10]
1、建⽴模型
随机初始化权重和偏置w [784, 10] <——————>b = [10]y_predict = tf.matmul(x, w) + b
2、计算损失
loss 平均样本损失
3、梯度下降优化
0.1学习率、 步数 2000 、准确率
2.1 ⼿写数字识别代码
import tensorflow
as tf
from tensorflow
.examples
.tutorials
.mnist
import input_data
FLAGS
= tf
.compat
.v1
.app
.flags
.FLAGS
tf
.compat
.v1
.disable_eager_execution
()
tf
.compat
.v1
.flags
.DEFINE_integer
("is_train", 1, "指定程序是预测还是训练")
def full_connected():
mnist
= input_data
.read_data_sets
("./data/mnist/input_data/", one_hot
=True)
with tf
.compat
.v1
.variable_scope
("data"):
x
= tf
.compat
.v1
.placeholder
(tf
.compat
.v1
.float32
, [None, 784])
y_true
= tf
.compat
.v1
.placeholder
(tf
.compat
.v1
.int32
, [None, 10])
with tf
.compat
.v1
.variable_scope
("fc_model"):
weight
= tf
.compat
.v1
.Variable
(tf
.compat
.v1
.random
.normal
([784, 10], mean
=0.0, stddev
=1.0), name
="w")
bias
= tf
.compat
.v1
.Variable
(tf
.compat
.v1
.constant
(0.0, shape
=[10]))
y_predict
= tf
.compat
.v1
.matmul
(x
, weight
) + bias
with tf
.compat
.v1
.variable_scope
("soft_cross"):
loss
= tf
.compat
.v1
.reduce_mean
(tf
.compat
.v1
.nn
.softmax_cross_entropy_with_logits
(labels
=y_true
, logits
=y_predict
))
with tf
.compat
.v1
.variable_scope
("optimizer"):
train_op
= tf
.compat
.v1
.train
.GradientDescentOptimizer
(0.1).minimize
(loss
)
with tf
.compat
.v1
.variable_scope
("acc"):
equal_list
= tf
.compat
.v1
.equal
(tf
.compat
.v1
.argmax
(y_true
, 1), tf
.compat
.v1
.argmax
(y_predict
, 1))
accuracy
= tf
.compat
.v1
.reduce_mean
(tf
.compat
.v1
.cast
(equal_list
, tf
.float32
))
tf
.compat
.v1
.summary
.scalar
("losses", loss
)
tf
.compat
.v1
.summary
.scalar
("acc", accuracy
)
tf
.compat
.v1
.summary
.histogram
("weightes", weight
)
tf
.compat
.v1
.summary
.histogram
("biases", bias
)
init_op
= tf
.compat
.v1
.global_variables_initializer
()
merged
= tf
.compat
.v1
.summary
.merge_all
()
saver
= tf
.compat
.v1
.train
.Saver
()
with tf
.compat
.v1
.Session
() as sess
:
sess
.run
(init_op
)
filewriter
= tf
.compat
.v1
.summary
.FileWriter
("./tmp/summary/test/", graph
=sess
.graph
)
if FLAGS
.is_train
== 1:
for i
in range(2000):
mnist_x
, mnist_y
= mnist
.train
.next_batch
(50)
sess
.run
(train_op
, feed_dict
={x
: mnist_x
, y_true
: mnist_y
})
summary
= sess
.run
(merged
, feed_dict
={x
: mnist_x
, y_true
: mnist_y
})
filewriter
.add_summary
(summary
, i
)
print("训练第%d步,准确率为:%f" % (i
, sess
.run
(accuracy
, feed_dict
={x
: mnist_x
, y_true
: mnist_y
})))
saver
.save
(sess
, "./tmp/ckpt/fc_model")
else:
saver
.restore
(sess
, "./tmp/ckpt/fc_model")
for i
in range(100):
x_test
, y_test
= mnist
.test
.next_batch
(1)
print("第%d张图片,手写数字图片目标是:%d, 预测结果是:%d" % (
i
,
tf
.compat
.v1
.argmax
(y_test
, 1).eval(),
tf
.compat
.v1
.argmax
(sess
.run
(y_predict
, feed_dict
={x
: x_test
, y_true
: y_test
}), 1).eval()
))
return None
if __name__
== "__main__":
full_connected
()
结果输出:训练第0步,准确率为:0.160000 训练第1步,准确率为:0.060000 训练第2步,准确率为:0.140000
训练第3步, 准确率为:0.040000
训练第4步,准确率为:0.160000
训练第5步,准确率为:0.120000
...
训练第1986步,准确率为:0.900000
训练第1987步,准确率为:0.800000
训练第1988步,准确率为:0.900000
训练第1989步,准确率为:0.940000
...
训练第1998步,准确率为:0.800000
训练第1999步,准确率为:0.860000
import tensorflow
as tf
from tensorflow
.examples
.tutorials
.mnist
import input_data
FLAGS
= tf
.compat
.v1
.app
.flags
.FLAGS
tf
.compat
.v1
.disable_eager_execution
()
tf
.compat
.v1
.flags
.DEFINE_integer
("is_train", 0, "指定程序是预测还是训练")
预测结果: 第0张图片,手写数字图片目标是:8, 预测结果是:8
第1张图片,手写数字图片目标是:3, 预测结果是:3
第2张图片,手写数字图片目标是:0, 预测结果是:0
第3张图片,手写数字图片目标是:5, 预测结果是:5
....
第95张图片,手写数字图片目标是:9, 预测结果是:9
第96张图片,手写数字图片目标是:2, 预测结果是:2
第97张图片,手写数字图片目标是:6, 预测结果是:6
第98张图片,手写数字图片目标是:8, 预测结果是:5
第99张图片,手写数字图片目标是:7, 预测结果是:7
Column 1Column 2