公众号:机器学习杂货店 作者:Peter 编辑:Peter
持续更新《Python深度学习》一书的精华内容,仅作为学习笔记分享。
本文是第一篇:深度学习中的数学基础和张量操作
In 1:
import pandas as pd
import numpy as np
import tensorflow as tf
MNIST数据集是一个大型的手写数字识别数据集,由美国国家标准技术研究所(NIST)收集并公开提供。该数据集包含约70000张手写数字图像,每张图像都是28x28像素大小的,灰度模式。
这些图像分为两个部分:训练集和测试集。训练集包含60000张图像,用于训练和调整模型参数;测试集包含10000张图像,用于评估模型的性能。
MNIST数据集是机器学习领域中非常常用的的一种数据集,特别是对于初学者来说。它是一个很好的起点,可以用来了解和比较各种机器学习算法的性能,例如神经网络、支持向量机、决策树等。通过训练和测试,可以评估各种算法在手写数字识别任务上的性能,以及它们的泛化能力。
In 2:
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
查看数据基本信息:
In 3:
train_images.shape
Out3:
(60000, 28, 28)
In 4:
train_labels.shape
Out4:
(60000,)
In 5:
test_images.shape
Out5:
(10000, 28, 28)
In 6:
test_labels.shape
Out6:
(10000,)
In 7:
digit = train_images[9]
digit[:5]
Out7:
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 189, 190, 0, 0,
0, 0]], dtype=uint8)
In 8:
import matplotlib.pyplot as plt
plt.imshow(digit, cmap=plt.cm.binary)
plt.show()
数据缩放(除以255)和数据类型转化,神经网络只能处理浮点数类型数据float32
In 9:
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
In 10:
# 标签实施独热码to_categorical
# 原文
# from keras.utils import to_categorical
# 修改
from tensorflow.keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
In 11:
train_labels
Out11:
array([[0., 0., 0., ..., 0., 0., 0.],
[1., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 1., 0.]], dtype=float32)
In 12:
from keras import models
from keras import layers
network = models.Sequential() # 实例化
# 添加两个全连接层(密集连接层):Dense层
network.add(layers.Dense(512,
activation='relu',
input_shape=(28*28, ))) # input_shape的第一个参数如何确定
# 第二个全连接层:softmax层,返回10个概率值,总和为1
network.add(layers.Dense(10,activation='softmax'))
In 13:
network.compile(optimizer="rmsprop", # 优化器
loss="categorical_crossentropy", # 损失函数
metrics=["accuracy"] # 评价指标:分类精度
)
In 14:
network.fit(train_images,
train_labels,
epochs=5,
batch_size=128)
Epoch 1/5
469/469 [==============================] - 2s 4ms/step - loss: 0.2650 - accuracy: 0.9227
Epoch 2/5
469/469 [==============================] - 2s 4ms/step - loss: 0.1075 - accuracy: 0.9688
Epoch 3/5
469/469 [==============================] - 2s 4ms/step - loss: 0.0714 - accuracy: 0.9780
Epoch 4/5
469/469 [==============================] - 2s 4ms/step - loss: 0.0524 - accuracy: 0.9844
Epoch 5/5
469/469 [==============================] - 2s 4ms/step - loss: 0.0403 - accuracy: 0.9881
Out14:
<keras.callbacks.History at 0x24660809d60>
显示了网络在训练数据上的损失loss和精度accuracy
In 15:
test_loss, test_acc = network.evaluate(test_images, test_labels)
313/313 [==============================] - 0s 863us/step - loss: 0.0685 - accuracy: 0.9788
模型在训练集上表现得很好,但是在测试集上性能表现得要差些,这种现象称之为过拟合
In 16:
# 仅仅包含单个数字,包含0个轴(ndim)
import numpy as np
x = np.array(12)
x
Out16:
array(12)
In 17:
x.ndim # 查看轴的个数,称之为rank
Out17:
0
In 18:
x.size # 表示张量中的元素个数
Out18:
1
In 19:
x = np.array([9,8,1,12])
x
Out19:
array([ 9, 8, 1, 12])
In 20:
x.ndim
Out20:
1
In 21:
x.size
Out21:
4
In 22:
x = np.array([[9,8,1,12],
[2,3,4,5],
[10,5,2,7]
])
x
Out22:
array([[ 9, 8, 1, 12],
[ 2, 3, 4, 5],
[10, 5, 2, 7]])
In 23:
x.ndim
Out23:
2
In 24:
x.size
Out24:
12
In 25:
# 1、轴的个数ndim
x.ndim
Out25:
2
In 26:
# 2、形状shape
x.shape
Out26:
(3, 4)
In 27:
# 3、数据类型
x.dtype
Out27:
dtype('int32')
In 28:
# 4、元素个数size
x.size
Out28:
12
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。