From 1a84fd05f268cc3ee1050e840234e76efa782549 Mon Sep 17 00:00:00 2001 From: bluedwave <424555320@qq.com> Date: Mon, 1 Nov 2021 23:11:35 +0800 Subject: [PATCH 1/3] update Memos/Memo-Summary/32-summary.md --- Memos/Memo-Summary/32-summary.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Memos/Memo-Summary/32-summary.md b/Memos/Memo-Summary/32-summary.md index 2b86d1e..20a408d 100644 --- a/Memos/Memo-Summary/32-summary.md +++ b/Memos/Memo-Summary/32-summary.md @@ -28,3 +28,5 @@ - [第15周小结](https://gitee.com/zhenchen3419/BDMI-2021A/blob/master/Memos/Study-Memo/32-day15.md) +- [第16周小结](https://gitee.com/zhenchen3419/BDMI-2021A/blob/master/Memos/Study-Memo/32-day15.md) + -- Gitee From 434571f77472a816e44297dfc662157422a43fb7 Mon Sep 17 00:00:00 2001 From: bluedwave <424555320@qq.com> Date: Wed, 3 Nov 2021 17:35:58 +0800 Subject: [PATCH 2/3] add Memos/Study-Memo/32-day8.md --- Memos/Study-Memo/32-day8.md | 92 +++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Memos/Study-Memo/32-day8.md diff --git a/Memos/Study-Memo/32-day8.md b/Memos/Study-Memo/32-day8.md new file mode 100644 index 0000000..b93a372 --- /dev/null +++ b/Memos/Study-Memo/32-day8.md @@ -0,0 +1,92 @@ +## 学习小结-1103-day8 +--- + +#### 1. Python pandas + +- 进行数据处理的一个库,有三种基本结构 + + - 一维数据`Series` + + 可以存储任意类型数据,索引叫做`index`,调用方法为 + + ```python + s = pd.Series(data, index=index) + ``` + + `data`可以是字典、`ndarray`、标量等,`index`是一维坐标轴的索引列表 + + - 二维数据`DataFrame` + + 从`Series`字典中构造 + + ```python + d = {'one' : pd.Series([1.,2.,3.],index=['a','b','c']), + 'two' : pd.Series([1.,2.,3.,4.],index=['a','b','c','d'])} + df = pd.DataFrame(d) + ``` + + - 三维数据`Panel` + +#### 2. 多层神经网络 + +- 自动化的权重确定的过程,也称为神经网络的训练,或称为网络权重的学习过程 + +- 一般流程: + + - 确定损失函数(Loss):定义损失函数 + + 引入度量函数:度量`y`与`y'`的差异,计算出差异值`Delta`,比如绝对值求和、平均绝对值求和、平方和误差、均方差(Mean Squared Error,MSE)、交叉熵(Cross Entropy)等 + + 对于回归任务,通过MSE的公式来计算损失 + + 对于分类任务,通过CE的公式来计算损失 + + 交叉熵是**负对数似然损失函数**,交叉熵损失函数表示如下 + $$ + H_{y'}(y)=-\Sigma_i y'_i log(y_i) + $$ + `y'`表示训练样本对应的标签,`y`表示神经网络的输出 + + - 权重初始化(Initialization):随机初始化 + + - 反向传播(Back Propagation):计算损失函数对权重的梯度 + + 损失函数是权重值的函数 + + 最优化算法:梯度下降法,也称为最速下降法 + + 反向传播算法 + + 正则化:有助于防止出现过拟合,包含L1正则化、L2正则化、丢弃正则化 + + - 权重修正(Weights Adjusting):随机梯度下降法 + + 随机梯度下降方法(Stochastic Gradient Descent,SGD)是最常用的权重调节方法,可通过权重调整,最小化损失函数 + + 步骤:1、随机初始化每个神经元输入权重和偏差;2、选取一个随机样本;3、根据网络的输出结果,从最后一层开始后,逐层计算每层权重的偏导数;4、逐层调整每层的权重,产生新的权重值;返回步骤2,继续随机选取下一个样本 + +- 神经网络运用的一般流程 + + - 准备数据集 + + - 搭建网络模型 + + - 训练模型 + + - 测试模型 + + 模型是秩多层神经网络的结构(Architecture)和相应的每层的权重数值(Weights) + +#### 3. 逻辑斯提回归实践(Logistic Regression) + +- 二元分类演示实验 + - 根据个体的身体体征,猜测性别 + - 数据集:取40人的数据,输入每个个体的身体特征 + - 效果:给出一个体的身体体征,得出是女生的可能性 +- 自动化训练流程 + - 获取训练数据 + - 搭建模型 + - 定义损失函数 + - 运行训练数据,从目标值计算损失 + - 计算损失的梯度,并使用优化器来调整变量以适应数据 + - 结果评估 -- Gitee From 3d3cddda38e7a8a20f1ab82b254128484d7dc3ab Mon Sep 17 00:00:00 2001 From: bluedwave <424555320@qq.com> Date: Wed, 17 Nov 2021 11:16:03 +0800 Subject: [PATCH 3/3] add 32-day9.md --- Memos/Study-Memo/32-day9.md | 219 ++++++++++++++++++++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 Memos/Study-Memo/32-day9.md diff --git a/Memos/Study-Memo/32-day9.md b/Memos/Study-Memo/32-day9.md new file mode 100644 index 0000000..78a676c --- /dev/null +++ b/Memos/Study-Memo/32-day9.md @@ -0,0 +1,219 @@ +## 学习小结-1110-day9 +--- + +#### 1. TensorFlow2基础 + +- 张量 + - 基础知识: + + 张量是具有统一类型(称为`dtype`)的多维数组,可以在`tf.dtypes.DType`中查看所有支持的`dtypes` + + 标量也称0秩张量:`rank_0_tensor = tf.constant(4)` + + 向量也称1秩张量:`rank_1_tensor = tf.constant([2.0, 3.0, 4.0])` + + 矩阵也称2秩张量: + + ```python + rank_2_tensor = tf.constant([[1, 2], + [3, 4], + [5, 6]], dtype=tf.float16) + ``` + + - 3秩张量: + + ```python + rank_3_tensor = tf.constant([ + [[0, 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]],]) + ``` + + - 通过使用`np.array`或`tensor.numpy`方法,可以将张量转换为`NumPy`数组 + - 张量支持基本数学运算,包括加法、逐元素乘法和矩阵乘法 + + ```python + a = tf.constant([[1, 2], + [3, 4]]) + b = tf.constant([[1, 1], + [1, 1]]) + print(tf.add(a, b), "\n") + print(tf.multiply(a, b), "\n") + print(tf.matmul(a, b), "\n") + print(a + b, "\n") # element-wise addition + print(a * b, "\n") # element-wise multiplication + print(a @ b, "\n") # matrix multiplication + ``` + + - 同时还支持其他更多的运算 + + ```python + c = tf.constant([[4.0, 5.0], [10.0, 1.0]]) + # Find the largest value + print(tf.reduce_max(c)) + # Find the index of the largest value + print(tf.argmax(c)) + # Compute the softmax + print(tf.nn.softmax(c)) + ``` + + - 形状: + + ```python + rank_4_tensor = tf.zeros([3, 2, 4, 5]) + print("Type of every element:", rank_4_tensor.dtype) + print("Number of dimensions:", rank_4_tensor.ndim) + print("Shape of tensor:", rank_4_tensor.shape) + print("Elements along axis 0 of tensor:", rank_4_tensor.shape[0]) + print("Elements along the last axis of tensor:", rank_4_tensor.shape[-1]) + print("Total number of elements (3*2*4*5): ", tf.size(rank_4_tensor).numpy()) + ``` + + - 如果要操作形状,可以使用`tf.reshape()` + + ```python + print(tf.reshape(rank_3_tensor, [3*2, 5]), "\n") + print(tf.reshape(rank_3_tensor, [3, -1])) + ``` + + - 可以使用索引和切片来访问张量 + + ```python + rank_1_tensor = tf.constant([0, 1, 1, 2, 3, 5, 8, 13, 21, 34]) + print("First:", rank_1_tensor[0].numpy()) + print("Second:", rank_1_tensor[1].numpy()) + print("Last:", rank_1_tensor[-1].numpy()) + print("Everything:", rank_1_tensor[:].numpy()) + print("Before 4:", rank_1_tensor[:4].numpy()) + print("From 4 to the end:", rank_1_tensor[4:].numpy()) + print("From 2, before 7:", rank_1_tensor[2:7].numpy()) + print("Every other item:", rank_1_tensor[::2].numpy()) + print("Reversed:", rank_1_tensor[::-1].numpy()) + ``` + + - 不规则张量 + + ```python + ragged_list = [ + [0, 1, 2, 3], + [4, 5], + [6, 7, 8], + [9]] + ragged_tensor = tf.ragged.constant(ragged_list) + ``` + + - 稀疏张量 + + ```python + sparse_tensor = tf.sparse.SparseTensor(indices=[[0, 0], [1, 2]], + values=[1, 2], + dense_shape=[3, 4]) + # We can convert sparse tensors to dense + print(tf.sparse.to_dense(sparse_tensor)) + ``` + +- 变量 + + - 变量通过`tf.Variable`类进行创建和跟踪 + - 创建变量,需要提供初始值 + + ```python + my_tensor = tf.constant([[1.0, 2.0], [3.0, 4.0]]) + my_variable = tf.Variable(my_tensor) + # Variables can be all kinds of types, just like tensors + bool_variable = tf.Variable([False, False, False, True]) + complex_variable = tf.Variable([5 + 4j, 6 + 1j]) + ``` + + ​ 大部分张量运算在变量上也可以按预期运行,不过变量无法重构形状,会新建一个张量 + + ```python + print("A variable:",my_variable) + print("\nViewed as a tensor:", tf.convert_to_tensor(my_variable)) + print("\nIndex of highest value:", tf.argmax(my_variable)) + # This creates a new tensor; it does not reshape the variable. + print("\nCopying and reshaping: ", tf.reshape(my_variable, ([1,4]))) + ``` + + - 变量支持的运算 + + ```python + a = tf.Variable([2.0, 3.0]) + # Create b based on the value of a + b = tf.Variable(a) + a.assign([5, 6]) + + # a and b are different + print(a.numpy()) + print(b.numpy()) + + # There are other versions of assign + print(a.assign_add([2,3]).numpy()) # [7. 9.] + print(a.assign_sub([7,9]).numpy()) # [0. 0.] + ``` + + - 还可以为变量命名,两个变量可以使用相同的名称,但是值不一定相等 + + ```python + a = tf.Variable(my_tensor, name="Mark") + b = tf.Variable(my_tensor + 1, name="Mark") + print(a == b) + ``` + +- 自动微分 + + - 梯度带 + + TensorFlow 为自动微分提供了 `tf.GradientTape` API;即计算某个计算相对于某些输入(通常是 `tf.Variable`)的梯度。TensorFlow 会将在 `tf.GradientTape` 上下文内执行的相关运算“记录”到“条带”上。TensorFlow 随后会该使用条带通过反向模式微分计算“记录的”计算的梯度。 + + - 默认情况下,调用`GradientTape.gradient()`方法,`GradientTape`占用的资源会立即释放,可以通过设置`persistent=True`来创建一个持久的梯度带 + + ```python + x = tf.constant(3.0) + with tf.GradientTape(persistent=True) as t: + t.watch(x) + y = x * x + z = y * y + dz_dx = t.gradient(z, x) # 108.0 (4*x^3 at x = 3) + dy_dx = t.gradient(y, x) # 6.0 + print(dz_dx) + print(dy_dx) + del t # Drop the reference to the tape + ``` + + - 一阶导数 + + ```python + def f(x): + return x ** 2 + tf.exp(x) + + def grad(x): + with tf.GradientTape() as t: + t.watch(x) + out = f(x) + return t.gradient(out, x) + + x = tf.constant(1.0) + print(grad(x).numpy()) + ``` + + - 高阶导数 + + ```python + x = tf.Variable(1.0) # Create a Tensorflow variable initialized to 1.0 + with tf.GradientTape() as t: + with tf.GradientTape() as t2: + y = x * x * x + # Compute the gradient inside the 't' context manager + # which means the gradient computation is differentiable as well. + dy_dx = t2.gradient(y, x) + d2y_dx2 = t.gradient(dy_dx, x) + print(dy_dx) + print(d2y_dx2) + ``` + + + -- Gitee