课程简介

课程链接: https://www.bilibili.com/video/BV1B7411L7Qt
主讲老师: 北京大学软件与微电子学院曹健老师
源代码: https://pan.baidu.com/s/19XC28Hz_TwnSQeuVifg1UQ 提取码:mocm

0. 开篇----Tensorflow2.1.0 安装及测试

Tensorflow2.1.0安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
conda create --name TF2.1 python=3.7
# 创建虚拟环境,指定python版本3.7
conda activate TF2.1
# 激活TF2.1环境
conda install cudatoolkit=10.1
# 安装英伟达的SDK10.1版本(如果你的电脑有英伟达的显卡,否则跳过)
conda install cudnn=7.6
# 安装英伟达深度学习软件包7.6版本(如果你的电脑有英伟达的显卡,否则跳过)
pip install tensorflow==2.1
# 安装tensorflow2.1版本
python
# 进入python
>>> import tensorflow as tf
>>> tf.__version__
'2.1.0'
# 验证tensorflow是否安装成功

配置Pycharm

测试代码

1
2
3
4
5
6
7
8
9
10
11
import tensorflow as tf

tensorflow_version = tf.__version__
gpu_available = tf.test.is_gpu_available()

print("tensorflow version:", tensorflow_version, "\tGPU available:", gpu_available)

a = tf.constant([1.0, 2.0], name="a")
b = tf.constant([1.0, 2.0], name="b")
result = tf.add(a, b, name="add")
print(result)

若返回下面的代码, 则说明配置成功

1
tf.Tensor([2. 4.], shape=(2,), dtype=float32)

测试报错

1
RuntimeError: cudaGetDevice() failed. Status: CUDA driver version is insufficient for CUDA runtime version

报错原因

CUDA驱动版本不适合CUDA运行时版本

输入nvidia-smi查看驱动版本, 在虚拟环境中输入conda list cuda查看cudatoolkit的版本号

英伟达GPU驱动和CUDA的版本对应关系中查看对应的版本号

解决方案1——降低cuda的版本

参考博客tensorflow的CUDA driver version is insufficient for CUDA runtime version 问题解决方案

解决方案2——升级nvidia显卡驱动

查看显卡型号, Win+R, 输入dxdiag, 在display中查看显卡型号

此电脑的显卡型号为NVIDA GeForce GTX 1060

InVidia GeForce Drivers中下载对应版本的显卡驱动并安装

image-20220413130433311

1.1 人工智能三学派

  • 行为主义
  • 符号主义
  • 连接主义

1.2 神经网络设计过程

  • 梯度下降法的例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import tensorflow as tf

w = tf.Variable(tf.constant(5, dtype=tf.float32))
lr = 0.2
epoch = 40

for epoch in range(epoch): # for epoch 定义顶层循环,表示对数据集循环epoch次,此例数据集数据仅有1个w,初始化时候constant赋值为5,循环40次迭代。
with tf.GradientTape() as tape: # with结构到grads框起了梯度的计算过程。
loss = tf.square(w + 1)
grads = tape.gradient(loss, w) # .gradient函数告知谁对谁求导

w.assign_sub(lr * grads) # .assign_sub 对变量做自减 即:w -= lr*grads 即 w = w - lr*grads
print("After %s epoch,w is %f,loss is %f" % (epoch, w.numpy(), loss))

# lr初始值:0.2 请自改学习率 0.001 0.999 看收敛过程
# 最终目的:找到 loss 最小 即 w = -1 的最优参数w

1.3 张量的生成

Tensorflow的数据类型

tf.int: tf.int32

tf.float: tf.float32, tf.float64

tf.bool: tf.constant([True,False])

tf.string: tf.constant("Hello,world!")

创建一个Tensor

1
tf.constant(张量内容, dtype=数据类型(可选参数))
1
2
3
4
5
6
import tensorflow as tf

a = tf.constant([1, 5], dtype=tf.int64)
print(a)
print(a.dtype)
print(a.shape)
1
2
3
4
# 运行结果
tf.Tensor([1 5], shape=(2,), dtype=int64)
<dtype: 'int64'>
(2,)

将Numpy数据类型转换为Tensor数据类型

1
tf.convert_to_tensor(数据名, dtype=数据类型(可选参数))
1
2
3
4
5
6
import tensorflow as tf
import numpy as np
a = np.arange(0,5)
b = tf.convert_to_tensor(a, dtype=tf.int64)
print(a)
print(b)
1
2
3
# 运行结果
[0 1 2 3 4]
tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int64)

创建指定值的Tensor

  • 创建元素全为0的张量
1
tf.zeros(维度) #默认生成float32类型的数据
  • 创建元素全为1的张量
1
tf.ones(维度) #默认生成float32类型的数据
  • 创建元素为指定值的张量
1
tf.fill(维度, 指定值) #默认按照填充值类型填充
  • 实例
1
2
3
4
5
6
7
import tensorflow as tf
a = tf.zeros([2,3])
b = tf.ones(4)
c = tf.fill([2,2],9)
print(a)
print(b)
print(c)
1
2
3
4
5
6
7
8
9
10
# 运行结果
tf.Tensor(
[[0. 0. 0.]
[0. 0. 0.]], shape=(2, 3), dtype=float32)

tf.Tensor([1. 1. 1. 1.], shape=(4,), dtype=float32)

tf.Tensor(
[[9 9]
[9 9]], shape=(2, 2), dtype=int32)

生成正太分布的随机数

  • 创建正太分布的随机数, 默认均值为0, 标准差为1
1
tf.random.normal(维度, mean=均值, stddev=标准差, dtype=数据类型)
  • 创建截断式正太分布的随机数
1
tf.random.truncated_normal(维度, mean=均值, stddev=标准差)

生成的值服从具有指定平均值和标准偏差的正态分布, 如果生成的值大于平均值2个标准偏差的值则丢弃重新选择, 即落在(μ2σ,μ+2σ)(\mu-2\sigma,\mu+2\sigma)之外的数据会被丢弃, 保证生成的值在均值附近.

3σ3\sigma原则

横轴区间(μ-σ,μ+σ)内的概率为68.268949%。
横轴区间(μ-2σ,μ+2σ)内的概率为95.449974%。
横轴区间(μ-3σ,μ+3σ)内的概率为99.730020%。

  • 实例
1
2
d = tf.random.normal([2, 2], mean=0.5, stddev=1)
print(d)
1
2
3
4
# 运行结果
tf.Tensor(
[[ 0.30043095 0.7326505 ]
[-0.86194825 0.5580075 ]], shape=(2, 2), dtype=float32)
1
2
e = tf.random.truncated_normal([2, 2], mean=0.5, stddev=1)
print(e)
1
2
3
4
# 运行结果
tf.Tensor(
[[ 1.0384454 0.40930927]
[-0.28229052 -0.44121146]], shape=(2, 2), dtype=float32)

生成均匀分布的随机数

1
tf.random.uniform(维度, minval=最小值, maxval=最大值, dtype=数据类型)
  • 实例
1
2
f = tf.random.uniform([2,3], minval=0, maxval=1)
print(f)
1
2
3
4
# 运行结果
tf.Tensor(
[[0.78726804 0.81395364 0.9172931 ]
[0.7733567 0.35996556 0.23103678]], shape=(2, 3), dtype=float32)

1.4 常用函数1

强制类型转换和最大最小值

  • 强制tensor转换数据类型
1
tf.cast(张量名, dtype=数据类型)
  • 计算张量维度上元素的最小值
1
tf.reduce_min(张量名)
  • 计算张量维度上的最大值
1
tf.reduce_max(张量名)
  • 实例
1
2
3
4
5
6
7
8
x1 = tf.random.uniform([2, 3], minval=0, maxval=100, dtype=tf.float64)
print(x1)

x2 = tf.cast(x1, tf.int32)
print(x2)

print(tf.reduce_min(x2))
print(tf.reduce_max(x2))
1
2
3
4
5
6
7
8
9
10
11
# 运行结果
tf.Tensor(
[[64.38174971 13.3164515 77.12602053]
[70.67317952 83.79160795 52.05179418]], shape=(2, 3), dtype=float64)

tf.Tensor(
[[64 13 77]
[70 83 52]], shape=(2, 3), dtype=int32)

tf.Tensor(13, shape=(), dtype=int32)
tf.Tensor(83, shape=(), dtype=int32)

理解axis

axis = 0是对第一个维度进行操作

axis = 1是对第二个维度进行操作

不指定默认是对所有元素进行操作

  • 计算张量沿指定维度的平均值
1
tf.reduce_mean(张量名, axis=维度)
  • 计算张量沿指定维度的和
1
tf.reduce_sum(张量名, axis=维度)
  • 实例
1
2
3
4
5
6
7
8
9
10
11
x = tf.constant(
[[[1, 2, 3, 4],
[2, 2, 3, 5],
[3, 4, 5, 6]],

[[4, 5, 6, 7],
[5, 6, 7, 8],
[6, 7, 8, 9]]])
print(x)
print(tf.reduce_mean(x))
print(tf.reduce_sum(x, axis=1))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 运行结果
tf.Tensor(
[[[1 2 3 4]
[2 2 3 5]
[3 4 5 6]]

[[4 5 6 7]
[5 6 7 8]
[6 7 8 9]]], shape=(2, 3, 4), dtype=int32)

tf.Tensor(4, shape=(), dtype=int32)

tf.Tensor(
[[ 6 8 11 15]
[15 18 21 24]], shape=(2, 4), dtype=int32)

tf.Variable

tf.Variavle()将变量标记为"可训练", 被标记的变量会在反向传播中记录梯度信息. 神经网络训练中, 常用该函数标记待训练的参数.

1
tf.Variable(初始值)
1
2
w = tf.Variable(tf.random.normal([2, 2], mean=0, stddev=1))
# 首先生成正太分布的随机数, 再给生成的随机数标记为可训练, 这样在反向传播中就可以通过梯度下降更新参数w了

数学运算

  • 四则运算

注意: 只有维度相同的元素才能进行四则运算

1
2
3
4
5
6
7
8
# 加
tf.add(张量1, 张量2)
# 减
tf.subtract(张量1, 张量2)
# 乘
tf.multiply(张量1, 张量2)
# 除
tf.divide(张量1, 张量2)
  • 幂运算
1
2
3
4
5
6
# 平方
tf.square(张量名)
# 开方
tf.sqrt(张量名)
# 次方
tf.pow(张量名, n次方数)
  • 矩阵运算

注意: 参与运算的两个矩阵的维度要符合矩阵乘法的规则

1
2
# 矩阵乘法
tf.matmul(矩阵1, 矩阵2)

读入数据

切分传入张量的第一维度, 生成输入(特征,标签)(特征, 标签)对, 构建数据集

(Numpy和Tensor格式都可用该语句读入数据)

1
data = tf.data.Dataset.from_tensor_slices((输入特征, 标签))
  • 实例
1
2
3
4
5
6
7
features = tf.constant([12, 23, 10, 17]) # 特征
labels = tf.constant([0, 1, 1, 0]) # 标签
# 特征和标签配对
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
print(dataset)
for element in dataset:
print(element)
1
2
3
4
5
6
# 运行结果
<TensorSliceDataset shapes: ((), ()), types: (tf.int32, tf.int32)>
(<tf.Tensor: shape=(), dtype=int32, numpy=12>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)
(<tf.Tensor: shape=(), dtype=int32, numpy=23>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: shape=(), dtype=int32, numpy=10>, <tf.Tensor: shape=(), dtype=int32, numpy=1>)
(<tf.Tensor: shape=(), dtype=int32, numpy=17>, <tf.Tensor: shape=(), dtype=int32, numpy=0>)

1.5 常用函数2

tf.GradientTape()

with结构记录计算过程, gradient求出张量的梯度

1
2
3
with tf.GradientTape() as tape:
若干计算过程
grad=tape.gradient(函数, 对谁求导)

w2w=2w\frac{\partial w^2}{\partial w} = 2w

1
2
3
4
5
6
# 计算w^2在w=3处的导数值
with tf.GradientTape() as tape:
w = tf.Variable(tf.constant(3.0))
loss = tf.pow(w, 2)
grad = tape.gradient(loss, w)
print(grad)
1
2
# 运行结果
tf.Tensor(6.0, shape=(), dtype=float32)

enumerate()

enumerate是python的内建函数, 它可遍历每个元素(如列表, 元组, 或字符串), 组合为: 索引 元素, 常在for循环中使用.

1
enumerate(列表名, 索引开始值) #默认从0开始
1
2
3
seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
print(i, element)
1
2
3
4
# 运行结果
0 one
1 two
2 three

tf.one_hot()

独热编码(one-hot encoding): 在分类问题中, 常用独热码做标签, 标记类别1/0

tf.one_hot()将待转换数据转换为one-hot形式的数据输出.

1
tf.one_hot(待转换数据, depth=几分类)
1
2
3
4
5
6
# 将标签0标签1标签2转化为独热码
classes = 3
# 3分类的输入的元素值只能是0/1/2, 否则独热码为[0. 0. 0]
labels = tf.constant([1, 0, 2])
output = tf.one_hot(labels, depth=classes)
print(output)
1
2
3
4
5
# 运行结果
tf.Tensor(
[[0. 1. 0.]
[1. 0. 0.]
[0. 0. 1.]], shape=(3, 3), dtype=float32)

tf.nn.softmax()

tf.nn.softmax函数使输出符合概率分布

y={1.012.010.66softmax(y)={ey0ey0+ey1+ey2=2.7510.73=0.256ey1ey0+ey1+ey2=7.4610.73=0.695ey2ey0+ey1+ey2=0.5210.73=0.048y= \begin{cases} 1.01 \\ 2.01 \\ -0.66 \end{cases} \qquad softmax(y) = \begin{cases} \frac{e^{y_0}}{e^{y_0}+e^{y_1}+e^{y_2}} = \frac{2.75}{10.73} = 0.256\\ \frac{e^{y_1}}{e^{y_0}+e^{y_1}+e^{y_2}} = \frac{7.46}{10.73} = 0.695\\ \frac{e^{y_2}}{e^{y_0}+e^{y_1}+e^{y_2}} = \frac{0.52}{10.73} = 0.048\\ \end{cases}

1
2
3
y = tf.constant([1.01, 2.01, -0.66])
y_pro = tf.nn.softmax(y)
print("After softmax, y_pro is: ", y_pro)
1
2
# 运行结果
After softmax, y_pro is: tf.Tensor([0.25598174 0.69583046 0.0481878 ], shape=(3,), dtype=float32)

assign_sub()

赋值操作, 更新参数的值并返回.

调用assign_sub前, 先用tf.Variable定义变量w为可训练(可自更新)

1
w.assign_sub(w要自减的内容)
1
2
3
w = tf.Variable(4)
w.assign_sub(1) # 即w = w-1
print(w)
1
2
# 运行结果
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>

tf.argmax() & tf.argmin()

1
2
3
4
5
6
7
8
9
10
11
12
13
import tensorflow as tf
import numpy as np
test = np.array([
[1, 2, 3],
[2, 3, 4],
[5, 6, 7],
[8, 9, 2]
])
print(test)
print(tf.argmax(test, axis=0)) #返回每一列最大值的索引
print(tf.argmin(test, axis=0)) #返回每一列最小值的索引
print(tf.argmax(test, axis=1)) #返回每一行最大值的索引
print(tf.argmin(test, axis=1)) #返回每一行最小值的索引
1
2
3
4
5
6
7
8
9
[[1 2 3]
[2 3 4]
[5 6 7]
[8 9 2]]

tf.Tensor([3 3 2], shape=(3,), dtype=int64)
tf.Tensor([0 0 3], shape=(3,), dtype=int64)
tf.Tensor([2 2 2 1], shape=(4,), dtype=int64)
tf.Tensor([0 0 0 2], shape=(4,), dtype=int64)

1.6 鸢尾花数据集(Iris)

数据集介绍

共有150组数据, 每组包括花萼长,花萼宽,花瓣长,花瓣宽4个输入特征. 同时给出了这一组特征对应的鸢尾花类别. 类别包括Setosa Iris(狗尾草鸢尾), Versicolour Iris(杂色鸢尾), Virginica Iris(弗吉尼亚鸢尾)三种类别, 分别用数字0,1,2表示.

数据集的导入

  • 安装sklearn包
1
pip install sklearn
  • 从sklearn.datasets读入Iris数据集
1
2
3
from sklearn.datasets import load_iris
x_data = datasets.load_iris().data #返回iris数据集的所有输入特征
y_data = datasets.load_iris().target #返回iris数据集的所有标签
  • 实例1
1
2
3
4
5
6
from sklearn import datasets

x_data = datasets.load_iris().data #返回iris数据集的所有输入特征
y_data = datasets.load_iris().target #返回iris数据集的所有标签
print("x_data from datasets: \n", x_data)
print("y_data from datasets: \n", y_data)
1
2
3
4
5
6
7
8
9
# 运行结果
x_data from datasets:
[[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
......
[5.9 3. 5.1 1.8]]

y_data from datasets:
[0 0 ... 0 1 1 ... 1 2 2 ...2]
  • 安装pandas包
1
pip install pandas
  • 实例2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from sklearn import datasets
from pandas import DataFrame
import pandas as pd

x_data = datasets.load_iris().data #返回iris数据集的所有输入特征
y_data = datasets.load_iris().target #返回iris数据集的所有标签

x_data = DataFrame(x_data, columns=['花萼长度', '花萼宽度', '花瓣长度', '花瓣宽度'])
# 为表格增加行索引(左侧)和列标签(上方)
pd.set_option('display.unicode.east_asian_width', True)
# 设置列名对齐
print("x_data add index: \n", x_data)

x_data['类别'] = y_data
# 新加一列,列标签为‘类别’,数据为y_data
print("x_data add a column: \n", x_data)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 运行结果
x_data add index:
花萼长度 花萼宽度 花瓣长度 花瓣宽度
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
.. ... ... ... ...
149 5.9 3.0 5.1 1.8

[150 rows x 4 columns]
x_data add a column:
花萼长度 花萼宽度 花瓣长度 花瓣宽度 类别
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
.. ... ... ... ... ...
149 5.9 3.0 5.1 1.8 2

[150 rows x 5 columns]

1.7 神经网络实现鸢尾花分类

  • 安装matplotlib
1
pip install matplotlib
  • 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# -*- coding: UTF-8 -*-
# 利用鸢尾花数据集,实现前向传播、反向传播,可视化loss曲线

# 导入所需模块
import tensorflow as tf
from sklearn import datasets
from matplotlib import pyplot as plt
import numpy as np

# 导入数据,分别为输入特征和标签
x_data = datasets.load_iris().data
y_data = datasets.load_iris().target

# 随机打乱数据(因为原始数据是顺序的,顺序不打乱会影响准确率)
# seed: 随机数种子,是一个整数,当设置之后,每次生成的随机数都一样(为方便教学,以保每位同学结果一致)
np.random.seed(116) # 使用相同的seed,保证输入特征和标签一一对应
np.random.shuffle(x_data)
np.random.seed(116)
np.random.shuffle(y_data)
tf.random.set_seed(116)

# 将打乱后的数据集分割为训练集和测试集,训练集为前120行,测试集为后30行
x_train = x_data[:-30]
y_train = y_data[:-30]
x_test = x_data[-30:]
y_test = y_data[-30:]

# 转换x的数据类型,否则后面矩阵相乘时会因数据类型不一致报错
x_train = tf.cast(x_train, tf.float32)
x_test = tf.cast(x_test, tf.float32)

# from_tensor_slices函数使输入特征和标签值一一对应。(把数据集分批次,每个批次batch组数据)
train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32)
test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)

# 生成神经网络的参数,4个输入特征故,输入层为4个输入节点;因为3分类,故输出层为3个神经元
# 用tf.Variable()标记参数可训练
# 使用seed使每次生成的随机数相同(方便教学,使大家结果都一致,在现实使用时不写seed)
w1 = tf.Variable(tf.random.truncated_normal([4, 3], stddev=0.1, seed=1))
b1 = tf.Variable(tf.random.truncated_normal([3], stddev=0.1, seed=1))

lr = 0.1 # 学习率为0.1
train_loss_results = [] # 将每轮的loss记录在此列表中,为后续画loss曲线提供数据
test_acc = [] # 将每轮的acc记录在此列表中,为后续画acc曲线提供数据
epoch = 500 # 循环500轮
loss_all = 0 # 每轮分4个step,loss_all记录四个step生成的4个loss的和

# 训练部分
for epoch in range(epoch): #数据集级别的循环,每个epoch循环一次数据集
for step, (x_train, y_train) in enumerate(train_db): #batch级别的循环 ,每个step循环一个batch
with tf.GradientTape() as tape: # with结构记录梯度信息
y = tf.matmul(x_train, w1) + b1 # 神经网络乘加运算
y = tf.nn.softmax(y) # 使输出y符合概率分布(此操作后与独热码同量级,可相减求loss)
y_ = tf.one_hot(y_train, depth=3) # 将标签值转换为独热码格式,方便计算loss和accuracy
loss = tf.reduce_mean(tf.square(y_ - y)) # 采用均方误差损失函数mse = mean(sum(y-out)^2)
loss_all += loss.numpy() # 将每个step计算出的loss累加,为后续求loss平均值提供数据,这样计算的loss更准确
# 计算loss对各个参数的梯度
grads = tape.gradient(loss, [w1, b1])

# 实现梯度更新 w1 = w1 - lr * w1_grad b = b - lr * b_grad
w1.assign_sub(lr * grads[0]) # 参数w1自更新
b1.assign_sub(lr * grads[1]) # 参数b自更新

# 每个epoch,打印loss信息
print("Epoch {}, loss: {}".format(epoch, loss_all/4))
train_loss_results.append(loss_all / 4) # 将4个step的loss求平均记录在此变量中
loss_all = 0 # loss_all归零,为记录下一个epoch的loss做准备

# 测试部分
# total_correct为预测对的样本个数, total_number为测试的总样本数,将这两个变量都初始化为0
total_correct, total_number = 0, 0
for x_test, y_test in test_db:
# 使用更新后的参数进行预测
y = tf.matmul(x_test, w1) + b1
y = tf.nn.softmax(y)
pred = tf.argmax(y, axis=1) # 返回y中最大值的索引,即预测的分类
# 将pred转换为y_test的数据类型
pred = tf.cast(pred, dtype=y_test.dtype)
# 若分类正确,则correct=1,否则为0,将bool型的结果转换为int型
correct = tf.cast(tf.equal(pred, y_test), dtype=tf.int32)
# 将每个batch的correct数加起来
correct = tf.reduce_sum(correct)
# 将所有batch中的correct数加起来
total_correct += int(correct)
# total_number为测试的总样本数,也就是x_test的行数,shape[0]返回变量的行数
total_number += x_test.shape[0]
# 总的准确率等于total_correct/total_number
acc = total_correct / total_number
test_acc.append(acc)
print("Test_acc:", acc)
print("--------------------------")

# 绘制 loss 曲线
plt.title('Loss Function Curve') # 图片标题
plt.xlabel('Epoch') # x轴变量名称
plt.ylabel('Loss') # y轴变量名称
plt.plot(train_loss_results, label="$Loss$") # 逐点画出trian_loss_results值并连线,连线图标是Loss
plt.legend() # 画出曲线图标
plt.show() # 画出图像

# 绘制 Accuracy 曲线
plt.title('Acc Curve') # 图片标题
plt.xlabel('Epoch') # x轴变量名称
plt.ylabel('Acc') # y轴变量名称
plt.plot(test_acc, label="$Accuracy$") # 逐点画出test_acc值并连线,连线图标是Accuracy
plt.legend()
plt.show()