Python + 深度学习从 0 到 1(02 / 99)

希望对你有帮助呀!!💜💜 如有更好理解的思路,欢迎大家留言补充 ~ 一起加油叭 💦
欢迎关注、订阅专栏 【深度学习从 0 到 1】谢谢你的支持!

⭐ Keras 快速入门:

神经网络的基本数据结构是层,你可以将层看作深度学习的乐高积木,Keras等框架则将这种比喻具体化

在 Keras 中,构建深度学习模型就是将相互兼容的多个层拼接在一起,以建立有用的数据变换流程

  • 相同的代码可以在CPU或GPU上无缝切换运行
  • Keras 基于宽松的MIT许可证发布,这意味着可以在商业项目中免费使用它。它与所有版本的Python都兼容
常见层实现代码示例:
  1. 全连接层[fully connected layer] 💜 .Dense
    from keras import layers 
    layer = layers.Dense(32, input_shape=(784,))  # 有32个输出单元的密集层
    
  2. 卷积层[conv layer] 💜 .Conv2D:通常使用 3x3 窗口和步幅 1
    from keras import layers 
    layer = layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))
    
    • input_shape:卷积神经网络接收形状为(image_height, image_width, image_channels)的输入张量(不包括批量维度)
    • padding: 不填充(默认) - “valid”,填充使得输入输出宽&高相等 - “same”
  3. 池化[pooling layer] 💜 .MaxPooling2D:通常使用 2x2 窗口和步幅 2

    作用:1. 减少需要处理的特征图的元素个数;2.使得连续卷积层的观察窗口越来越大

    from keras import layers 
    layer = layers.MaxPooling2D((2, 2))
    

⭐ 实战:手写数字分类: 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 
# 		  Model 实例化需要两个参数:一个输入张量(或输入张量的列表)和一个输出张量(或输出张量的列表)
#		  得到的类是一个Keras 模型,就像 Sequential 模型一样,将特定输入映射为特定输出
#		  Model 类允许模型有多个输出 outputs ,这一点与 Sequential 模型不同
input_tensor = layers.Input(shape=(28 * 28,)) 
x = layers.Dense(32, activation='relu')(input_tensor) 
output_tensor = layers.Dense(10, activation='softmax')(x) 
network = models.Model(inputs=input_tensor, outputs=output_tensor)

# network.summary()  # 用来打印查看网络架构 (layer + output shape + param #)

此处我们应用了最常见的(全连接)层的线性堆叠,后续博客会进一步展开其他层的网络拓扑结构如:双分支(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,
			validation_data=(test_images, test_labels))
💜步骤五:测试网络
 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  # one-hot 编码
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'))

# 编译步骤 
# 无论你的问题是什么,rmsprop 优化器通常都是足够好的选择。这一点你无须担心
network.compile(optimizer='rmsprop', 
                loss='categorical_crossentropy', 
                metrics=['accuracy'])  ## 对于回归问题: metrics=['mae']

# 训练网络
history = network.fit(train_images, train_labels, epochs=5, batch_size=128, validation_split=0.1)
print(history.history.keys(), '--- history ---')
# history = model.fit(...)
# history_dict = history.history
# history_dict.keys()  ## dict_keys(['val_accuracy', 'accuracy', 'val_loss', 'loss']), 可用于可视化训练过程

# 保存网络
network.save("TEMP_MODEL.h5")

## 预测结果 (代码参考)
#label_predict = network.predict(test_images[0])

# 测试网络
test_loss, test_acc = network.evaluate(test_images, test_labels) 

print("Loss: {}, Acc: {}".format(test_loss, test_acc))

# 绘制训练及验证结果
import matplotlib.pyplot as plt 
acc = history.history['accuracy'] 
val_acc = history.history['val_accuracy'] 
loss = history.history['loss'] 
val_loss = history.history['val_loss'] 
epochs = range(1, len(acc) + 1) 
plt.plot(epochs, acc, 'bo', label='Training acc') 
plt.plot(epochs, val_acc, 'b', label='Validation acc') 
plt.title('Training and validation accuracy') 
plt.legend() 
plt.figure() 
plt.plot(epochs, loss, 'bo', label='Training loss') 
plt.plot(epochs, val_loss, 'b', label='Validation loss') 
plt.title('Training and validation loss') 
plt.legend() 
plt.show()

----- 结束后会得到类似如下结果:

Epoch 1/5
422/422 [==============================] - 3s 6ms/step - loss: 0.2695 - accuracy: 0.9225 - val_loss: 0.1180 - val_accuracy: 0.9653
Epoch 2/5
422/422 [==============================] - 3s 7ms/step - loss: 0.1108 - accuracy: 0.9672 - val_loss: 0.0795 - val_accuracy: 0.9772
Epoch 3/5
422/422 [==============================] - 3s 7ms/step - loss: 0.0732 - accuracy: 0.9781 - val_loss: 0.0748 - val_accuracy: 0.9790
Epoch 4/5
422/422 [==============================] - 3s 8ms/step - loss: 0.0522 - accuracy: 0.9839 - val_loss: 0.0723 - val_accuracy: 0.9780
Epoch 5/5
422/422 [==============================] - 3s 7ms/step - loss: 0.0397 - accuracy: 0.9879 - val_loss: 0.0623 - val_accuracy: 0.9822
dict_keys(['loss', 'accuracy', 'val_loss', 'val_accuracy']) --- history ---
313/313 [==============================] - 0s 1ms/step - loss: 0.0651 - accuracy: 0.9802
Loss: 0.06511897593736649, Acc: 0.9801999926567078

在这里插入图片描述


参考书籍:Python 深度学习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值