活动地址:CSDN21天学习挑战赛
学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。各位小伙伴,如果您:
想系统/深入学习某技术知识点…
一个人摸索学习很难坚持,想组团高效学习…
想写博客但无从下手,急需写作干货注入能量…
热爱写作,愿意让自己成为更好的人…
**
1,机缘
A,分享mnist手写数字识别案例
B,分享遇到的问题解决方式
C,通过文章进行技术交流
2,收获
A,获得了10粉丝的关注
B,获得了20如赞、阅读量
C,认识和了解了mnist手写数字识别的作者
3,日常
- 创作是否已经成为我学习的一部分了
- 有限的时间下,抽出星期二、四、六的时间进行创作,其余时间学习
4,憧憬
创作规划是熟悉TensorFlow的基本语法,测试方法,模型调优
**
**
1,学习目标
掌握 TensorFlow运行机器学习模型的方法,了解数据集的构成和卷积神经网络的结构。
2,学习内容
A,搭建 TensorFlow 开发环境
B,掌握 TensorFlow基本语法
C,掌握机器学习模型的构成和数据集的结构
3,学习时间
周一至周五晚上 7 点—晚上9点
周六下午 7 点-下午 9 点
周日下午 7 点-下午 9 点
4,学习产出
技术笔记 10 遍
CSDN技术博客 5 篇
学习的vlog 视频 2个
**
**
1,学习知识点
神经网络程序的流程
2,学习遇到的问题
数据归一化、损失函数和优化器的选择
3,学习的收获
学会了神经网络程序的基本流程
4,实操
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
import numpy as np
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
# 将像素的值标准化至0到1的区间内。
train_images, test_images = train_images / 255.0, test_images / 255.0
print(train_images.shape,test_images.shape,train_labels.shape,test_labels.shape)
plt.figure(figsize=(20,10))
for i in range(20):
plt.subplot(5,4,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(train_labels[i])
plt.show()
#调整数据到我们需要的格式
train_images = train_images.reshape((60000, 28, 28, 1))
test_images = test_images.reshape((10000, 28, 28, 1))
print(train_images.shape,test_images.shape,train_labels.shape,test_labels.shape)
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), # 卷积层
layers.MaxPooling2D((2, 2)), # 池化层1
layers.Conv2D(64, (3, 3), activation='relu'), # 卷积层
layers.MaxPooling2D((2, 2)), # 池化层
layers.Flatten(), # Flatten层
layers.Dense(64, activation='relu'), # 全连接层
layers.Dense(10) # 输出层
])
# 打印网络结构
model.summary()
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
test_loss, test_acc=model.evaluate(test_images, test_labels)
print(test_loss,test_acc)
#附加一个 softmax 层,将 logits 转换成更容易理解的概率
probability_model = tf.keras.Sequential([model,
tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
print(predictions[5])#输出10个概率值
print('查看第0个预测值:',np.argmax(predictions[5]))#返回最大值对应的索引
print('真实值:',test_labels[5])
输出:0.03543371707201004 0.9912999868392944
[4.91323336e-11 9.99999881e-01 5.80148169e-12 1.51072036e-14
8.19891355e-08 1.01238275e-10 1.01550857e-09 1.59300317e-09
1.34930056e-09 1.17530541e-10]
查看第0个预测值: 1
真实值: 1
分析:
可以看出模型预测准确率为99%,错误率为3.5%,第5个样本的为1的概率为0.99
预测值为1,预测正确。