我们先把官方的代码抄写一遍,然后逐个解析学习其中不懂的地方。

# 导入Tensorflow模块
import tensorflow as tf

# 起别名
mnist = tf.keras.datasets.mnist

# 加载mnist数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 把训练集从整数转换为浮点数
x_train, x_test = x_train / 255.0, x_test /255.0

# 通过堆叠,构建tf.keras.Sequential模型
model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation='softmax')
])
# 选择一个 优化器 和 损失函数 用来训练
model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)
# 训练并评估模型
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)

tf.keras.layers.Flatten

Flatten把多维数据一维化,比如你输入的数据是3x3x3的三维数据,通过flatten操作,可以得到一个27个参数的一维数据。(27 = 3 x 3 x 3)

相当于把一堆堆在一起的苹果,一颗接着一颗放在在一条横线上,呈一字排开。

flatten不影响batch的大小

用法:

model = Sequential()
model.add(Convolution2D(64, 3, 3,
                        border_mode='same',
                        input_shape=(3, 32, 32)))
# now: model.output_shape == (None, 64, 32, 32)

model.add(Flatten())
# now: model.output_shape == (None, 65536)

tf.keras.layers.Dense

Dense implements the operation:

output = activation(dot(input, kernel) + bias)

where activation is the element-wise activation function passed as the activation argument,
kernel is a weights matrix created by the layer,
and bias is a bias vector created by the layer (only applicable if use_bias is True).

element-wise 元素层面上的

Note: If the input to the layer has a rank greater than 2, then it is flattened prior to the initial dot product with kernel.(理解不了,layer has a rank greater than 2)

例子:

# as first layer in a sequential model:
model = Sequential()
model.add(Dense(32, input_shape=(16,)))
# now the model will take as input arrays of shape (*, 16)
# and output arrays of shape (*, 32)

# after the first layer, you don't need to specify
# the size of the input anymore:
# 在第一层之后就不用管输入的形状了。
model.add(Dense(32))

参数:

units: Positive integer, dimensionality of the output space.
正整数,输出空间的shape的维度

activation: Activation function to use. 要使用的激活函数
If you don't specify anything, no activation is applied (ie. "linear" activation: a(x) = x).如果你不指定激活函数,激活函数相当于没有(相当于 y = x 相当于输入什么就输出什么)

use_bias: Boolean, whether the layer uses a bias vector.
是否使用偏移向量

kernel_initializer: Initializer for the kernel weights matrix.
卷积核矩阵的初始化器

bias_initializer: Initializer for the bias vector.
偏置向量的初始化器

kernel_regularizer: Regularizer function applied to the kernel weights matrix.
卷积核的正则化器

bias_regularizer: Regularizer function applied to the bias vector.
偏置向量的正则化器

activity_regularizer: Regularizer function applied to the output of the layer (its "activation")..


kernel_constraint: Constraint function applied to the kernel weights matrix.

bias_constraint: Constraint function applied to the bias vector.

什么是正则化?

简单来说,正则化是一种为了减小测试误差的行为(有时候会增加训练误差)。

我们在构造机器学习模型时,最终目的是让模型在面对新数据的时候,可以有很好的表现。

当你用比较复杂的模型比如神经网络,去拟合数据时,很容易出现过拟合现象(训练集表现很好,测试集表现较差),这会导致模型的泛化能力下降,这时候,我们就需要使用正则化,降低模型的复杂度。

tf.keras.datasets.mnist

加载mnist数据集。

什么是mnist数据集?
首先看官网的解释,再看看百度搜索前两个博客的解释,如下链接,看完后相信你对mnist数据集会有一个初步的认识。

mnist数据集官网:http://yann.lecun.com/exdb/mnist/

简述的一篇介绍mnist的博客:https://www.jianshu.com/p/d282bce1a999

博客园的一篇介绍mnist的博客:

mnist.load_data()

tf.keras.datasets.mnist.load_data(path='mnist.npz')

参数:
path: path where to cache the dataset locally (relative to ~/.keras/datasets).

返回值:
Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test).

一般用法:

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

为什么要转化为浮点数

实际上,这个步骤是 归一化处理 的经典应用。
归一化处理数据预处理的内容之一。
人工智能训练需要数据,而数据参差不一,需要加工到符合一个标准范式的程度。
所以需要专门了解一下数据预处理的知识

tf.keras.Sequential

官方解释:https://tensorflow.google.cn/api_docs/python/tf/keras/Sequential?hl=en Sequential可以理解为:神经网络层(layer) 的 堆叠。
众所周知:构建一个模型需要很多层,输入层,隐藏层,输出层,等等。
Sequential就是可以方便的按顺序添加指定的层,添加完了层,模型就构建好了,
让你不用担心层与层之间的架构设计。
所以,我们需要学习Sequential的知识

参数:
layers: list of layers to add to the model.

总结:

这是官方关于TF2.0的第一个教程源码。
我们从它这里获取的信息节点有:
1,MNIST数据集
2,数据预处理
3,Sequential模型
总结出信息节点,就可以去百度搜索信息了。

总之,训练神经网络需要数据集,数据集需要预处理,搭建模型可以用Sequential
,训练和评估可以使用Sequential提供的函数
有了这个方向,就可以前进了,因为选择往往比努力更重要
现在要做的就是不停的问类似下面的问题:
why and how?
数据集是什么?
怎么对数据集进行预处理?
怎么搭建模型?
Sequential是什么?
然后还会发展出很多小的问题,只要把每个小问题都搞懂,合起来这些大问题就搞懂了。
最重要的是,细节细节细节

开始搜集信息

什么是MNIST?官网是这么说的

The MNIST database of handwritten digits, available from this page, has a training set of 60,000 examples, and a test set of 10,000 examples. It is a subset of a larger set available from NIST. The digits have been size-normalized and centered in a fixed-size image.
It is a good database for people who want to try learning techniques and pattern recognition methods on real-world data while spending minimal efforts on preprocessing and formatting.

我觉得它大概是说MNIST是NIST数据集的子集,而且适合新手学习使用,有一大堆手写数字图片,而且裁剪整齐了。
Tensorflow把MNIST加载为Turple of Numpy arrays 也就是(x_train, y_train), (x_test, y_test) 这种形式。
什么是Turple of Numpy arrays?

百度没查到,所以我就直接理解为 Numpy数组的元组,有点不通顺,无法理解,所以继续细分问题。

Turple是什么?

元组(Turple)与列表类似,不同点是: 元组的元素不可修改; 元组使用圆括号(),列表使用方括号[]。
内容太多了,贴个链接后边再学
https://www.jianshu.com/p/a4394612f887

好了,把Turple学了一点,并写了博客↓

python的array是什么?
能搜到一大堆,太多了,基础不扎实的时候,就会发生这种现象,
遇到不会的,去查,结果发现一大堆需要学的。
那么这个时候是去学一大堆基础知识,还是继续手头的学习呢?
很让人纠结,所以每当发生这种情况时,我都会先去学基础知识,而搁置当前的内容。
把当前内容做个记录,当基础知识学完后,再来看这里的内容,就能理解了

那么Numpy数组和python的数组有什么区别?

numpy turple 和python turple有啥区别?

能提出很多问题,说明基础不扎实,说明得去补基础
补完基础才能继续后面的学习。