Python深度学习精华笔记1:深度学习中的数学基础和张量操作

公众号:尤而小屋

作者:Peter

编辑:Peter

持续更新《Python深度学习》一书的精华内容,仅作为学习笔记分享。

本文是第一篇:深度学习中的数学基础和张量操作

In [1]:

javascript 复制代码
import pandas as pd
import numpy as np

import tensorflow as tf

加载MNIST数据集

MNIST数据集是一个大型的手写数字识别数据集,由美国国家标准技术研究所(NIST)收集并公开提供。该数据集包含约70000张手写数字图像,每张图像都是28x28像素大小的,灰度模式。

这些图像分为两个部分:训练集和测试集。训练集包含60000张图像,用于训练和调整模型参数;测试集包含10000张图像,用于评估模型的性能。

MNIST数据集是机器学习领域中非常常用的的一种数据集,特别是对于初学者来说。它是一个很好的起点,可以用来了解和比较各种机器学习算法的性能,例如神经网络、支持向量机、决策树等。通过训练和测试,可以评估各种算法在手写数字识别任务上的性能,以及它们的泛化能力。

In [2]:

scss 复制代码
from keras.datasets import mnist

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

查看数据基本信息:

In [3]:

train_images.shape

Out[3]:

scss 复制代码
(60000, 28, 28)

In [4]:

train_labels.shape

Out[4]:

scss 复制代码
(60000,)

In [5]:

test_images.shape

Out[5]:

scss 复制代码
(10000, 28, 28)

In [6]:

test_labels.shape

Out[6]:

scss 复制代码
(10000,)

显示数字图像

In [7]:

ini 复制代码
digit = train_images[9]
digit[:5]

Out[7]:

ini 复制代码
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]:

dart 复制代码
import matplotlib.pyplot as plt

plt.imshow(digit, cmap=plt.cm.binary)
plt.show()

准备图像数据

数据缩放(除以255)和数据类型转化,神经网络只能处理浮点数类型数据float32

In [9]:

ini 复制代码
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]:

ini 复制代码
# 标签实施独热码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

Out[11]:

ini 复制代码
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]:

ini 复制代码
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]:

ini 复制代码
network.compile(optimizer="rmsprop",   # 优化器
                loss="categorical_crossentropy",  #  损失函数
                metrics=["accuracy"]   # 评价指标:分类精度
               )

模型拟合fit

In [14]:

ini 复制代码
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

Out[14]:

xml 复制代码
<keras.callbacks.History at 0x24660809d60>

显示了网络在训练数据上的损失loss和精度accuracy

模型评价

In [15]:

ini 复制代码
test_loss, test_acc = network.evaluate(test_images, test_labels)
313/313 [==============================] - 0s 863us/step - loss: 0.0685 - accuracy: 0.9788

模型在训练集上表现得很好,但是在测试集上性能表现得要差些,这种现象称之为过拟合

神经网络的数据张量

标量-0D张量

In [16]:

ini 复制代码
# 仅仅包含单个数字,包含0个轴(ndim)
import numpy as np

x = np.array(12)
x

Out[16]:

scss 复制代码
array(12)

In [17]:

bash 复制代码
x.ndim     # 查看轴的个数,称之为rank

Out[17]:

0

In [18]:

arduino 复制代码
x.size   # 表示张量中的元素个数

Out[18]:

1

向量-1D张量

In [19]:

ini 复制代码
x = np.array([9,8,1,12])
x

Out[19]:

scss 复制代码
array([ 9,  8,  1, 12])

In [20]:

x.ndim

Out[20]:

1

In [21]:

arduino 复制代码
x.size

Out[21]:

4

矩阵-2D张量

In [22]:

css 复制代码
x = np.array([[9,8,1,12],
              [2,3,4,5],
              [10,5,2,7]
             ])
x

Out[22]:

lua 复制代码
array([[ 9,  8,  1, 12],
       [ 2,  3,  4,  5],
       [10,  5,  2,  7]])

In [23]:

x.ndim

Out[23]:

2

In [24]:

arduino 复制代码
x.size

Out[24]:

12

关键属性

In [25]:

bash 复制代码
# 1、轴的个数ndim

x.ndim

Out[25]:

2

In [26]:

bash 复制代码
# 2、形状shape

x.shape

Out[26]:

scss 复制代码
(3, 4)

In [27]:

bash 复制代码
# 3、数据类型

x.dtype

Out[27]:

scss 复制代码
dtype('int32')

In [28]:

arduino 复制代码
# 4、元素个数size

x.size

Out[28]:

12

现实数据中的张量

  • 向量:2D, (samples, features)
  • 时间序列数据:3D, (samples, timesteps, features)
  • 图像:4D,(smaples, height, width, channels) or (samples, channels, height, width)
  • 视频: 5D,(smaples, frames,height, width, channels) or (samples, frames,channels, height, width)
相关推荐
中科微星22 分钟前
相位型SLM硬件产品面型性能提升
图像处理·人工智能·深度学习
叫我:松哥1 小时前
基于python flask的高血压疾病预测分析与可视化系统的设计与实现,使用随机森林、决策树、逻辑回归、xgboost等机器学习库预测
python·决策树·随机森林·机器学习·数据分析·flask·逻辑回归
生信宝典1 小时前
ROC和AUC也不是评估机器学习性能的金标准
人工智能·qt·机器学习
friklogff2 小时前
【C#生态园】从数据分析到机器学习:掌握C#统计学库的核心功能
机器学习·数据分析·c#
Yorelee.2 小时前
保研面试问题总结
深度学习·动态规划
魔力之心2 小时前
人工智能与机器学习原理精解【24】
人工智能·机器学习·概率论
浊酒南街2 小时前
吴恩达深度学习笔记:卷积神经网络(Foundations of Convolutional Neural Networks)2.1-2.2
人工智能·深度学习·机器学习
#include<菜鸡>2 小时前
深度学习-图像处理篇1.1-1.2神经网络
图像处理·深度学习·神经网络
AI完全体2 小时前
【AI战略思考1】如何更高效地了解AI行业的最新动态和商业应用以及我的时间分配
人工智能·机器学习·ai·商业应用·ai行业动态·技术趋势·信息渠道
#include<菜鸡>2 小时前
动手学深度学习(pytorch土堆)-06损失函数与反向传播、模型训练、GPU训练
人工智能·pytorch·深度学习