希望对你有帮助呀!!💜💜 如有更好理解的思路,欢迎大家留言补充 ~ 一起加油叭 💦
欢迎关注、订阅专栏 【深度学习从 0 到 1】谢谢你的支持!
⭐ Keras 快速入门:
神经网络的基本数据结构是层,你可以将层看作深度学习的乐高积木,Keras等框架则将这种比喻具体化
在 Keras 中,构建深度学习模型就是将相互兼容的多个层拼接在一起,以建立有用的数据变换流程
- 相同的代码可以在CPU或GPU上无缝切换运行
- Keras 基于宽松的MIT许可证发布,这意味着可以在商业项目中免费使用它。它与所有版本的Python都兼容
常见层实现代码示例:
- 全连接层[fully connected layer] 💜
.Dense
from keras import layers layer = layers.Dense(32, input_shape=(784,)) # 有32个输出单元的密集层
⭐ 实战:手写数字分类: Keras + MNIST 数据集
手写数字分类任务
任务:将手写数字的灰度图像(28像素×28像素)划分到10个类别中(0~9)
MNIST数据集包含60 000张训练图像和10 000张测试图像,由美国国家标准与技术研究院(National Institute of Standards and Technology,即 MNIST 中的NIST)在20世纪80年代收集得到
- 样本示例如下:(hint: 显示数据集的第一个数字的代码:
plt.imshow(train_images[0], cmap=plt.cm.binary)
)
💜步骤一 : 加载Keras中的MNIST数据集
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data() # 包括4个Numpy数组
# 准备数据
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255
# 准备标签
from keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
💜步骤二 : 构建网络架构 (两层全连接层为例)
from keras import models
from keras import layers
## 写法一:利用 Sequential 类定义 model
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax')) # 没指定 input_shape, 则自动匹配上一层
## 写法二:利用函数式 API 定义 model
input_tensor = layers.Input(shape=(28 * 28,))
x = layers.Dense(32, activation='relu')(input_tensor)
output_tensor = layers.Dense(10, activation='softmax')(x)
model = models.Model(inputs=input_tensor, outputs=output_tensor)
此处我们应用了最常见的(全连接)层的线性堆叠,后续博客会进一步展开其他层的网络拓扑结构如:双分支(two-branch)网络,多头(multihead)网络 和 Inception 模块
💜步骤三: 编译步骤 (optimizer + loss + metrics)
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
💜步骤四:训练网络
network.fit(train_images, train_labels, epochs=5, batch_size=128)
💜步骤五:测试网络
test_loss, test_acc = network.evaluate(test_images, test_labels)
完整代码参考:
from keras.datasets import mnist
from keras import models
from keras import layers
(train_images, train_labels), (test_images, test_labels) = mnist.load_data() # 包括4个Numpy数组
# 准备数据
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255
# 准备标签
from keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# 构建网络架构
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))
# 编译步骤
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# 训练网络
network.fit(train_images, train_labels, epochs=5, batch_size=128)
# 测试网络
test_loss, test_acc = network.evaluate(test_images, test_labels)
print("Loss: {}, Acc: {}".format(test_loss, test_acc))
----- 结束后会得到类似如下结果:
Epoch 1/5
469/469 [==============================] - 2s 5ms/step - loss: 0.2598 - accuracy: 0.9253
Epoch 2/5
469/469 [==============================] - 2s 5ms/step - loss: 0.1041 - accuracy: 0.9692
Epoch 3/5
469/469 [==============================] - 2s 5ms/step - loss: 0.0684 - accuracy: 0.9795
Epoch 4/5
469/469 [==============================] - 2s 5ms/step - loss: 0.0492 - accuracy: 0.9848
Epoch 5/5
469/469 [==============================] - 2s 5ms/step - loss: 0.0367 - accuracy: 0.9892
313/313 [==============================] - 0s 702us/step - loss: 0.0665 - accuracy: 0.9803
Loss: 0.06652633100748062, Acc: 0.9803000092506409
参考书籍:Python 深度学习