{ "cells": [ { "cell_type": "markdown", "id": "8467bf5b-8c23-4084-adb3-a0e0c3b3e7a6", "metadata": {}, "source": [ "## 目录\n", "\n", "- 感知机分类器\n", "- 逻辑回归分类器\n", "- 图像分类数据集\n", "- softmax回归的从零开始实现\n", "- softmax回归的简洁实现" ] }, { "cell_type": "markdown", "id": "412d121a-f1f9-4150-ad97-39c5b0edc498", "metadata": { "tags": [] }, "source": [ "## 感知机分类器" ] }, { "cell_type": "markdown", "id": "13474e54-4e85-4abf-abc0-bead50177068", "metadata": {}, "source": [ "Perceptron是一个线性分类器。它的工作方式与没有隐藏层的神经网络相同(只有输入和输出)。\n", "\n", "\n", "\n", "首先,它对给定的数据集进行权重训练,然后通过网络对一个新项目进行分类。\n", "\n", "> 注意,在分类问题中,每个节点代表一个类别。最终的分类是具有最大输出值的类/节点。" ] }, { "cell_type": "markdown", "id": "d05ceed2-8d2e-4bc6-ac1c-da2bb67ecb89", "metadata": { "tags": [] }, "source": [ "### 实现\n", "\n", "`PerceptionLinearLearner` 用于训练(计算)给定数据集的权重。\n", "\n", "函数`predict`用于对一个新的项目进行分类:该函数计算项目与外层每个节点的计算权重的(代数)点积。然后,它选择一个最大的值,将项目归入相应的类别。" ] }, { "cell_type": "code", "execution_count": 2, "id": "c3423455-3a8c-44e5-a9aa-592b7c6e285d", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "
\n", "class PerceptionLinearLearner(LinearClassifier):\n",
" """\n",
" Perception linear classifier: hard threshold\n",
" """\n",
" def __init__(self, dataset, learning_rate=0.01, epochs=100):\n",
" self.idx_i = dataset.inputs\n",
" self.idx_t = dataset.target\n",
" self.examples = dataset.examples\n",
" self.num_examples = len(self.examples)\n",
" # initialize random weights\n",
" self.w = random_weights(min_value=-0.5, max_value=0.5, num_weights=len(self.idx_i) + 1)\n",
" # learning loop\n",
" self.learn(learning_rate, epochs)\n",
" \n",
" def learn(self, learning_rate, epochs):\n",
" """ learning loop """\n",
" def loss(example, w, idx_i, idx_t):\n",
" """ error: difference between estimation and true value """\n",
" raise NotImplementedError\n",
" \n",
" def update(w, learning_rate, err, X_col, num_examples):\n",
" """ update weights """\n",
" raise NotImplementedError\n",
"\n",
" def homogeneous(num_examples):\n",
" """ build homogeneous coordinates """\n",
" raise NotImplementedError\n",
"\n",
" raise NotImplementedError\n",
"\n",
" def predict(self, x):\n",
" """ make prediction """\n",
" return int(np.dot(self.w, [1] + x))\n",
"class LogisticLinearLeaner(LinearClassifier):\n",
" def __init__(self, dataset, learning_rate=0.01, epochs=100):\n",
" self.idx_i = dataset.inputs\n",
" self.idx_t = dataset.target\n",
" self.examples = dataset.examples\n",
" self.num_examples = len(self.examples)\n",
" # initialize random weights\n",
" self.w = random_weights(min_value=-0.5, max_value=0.5, num_weights=len(self.idx_i) + 1)\n",
" # learning loop\n",
" self.learn(learning_rate, epochs)\n",
" \n",
" def learn(self, learning_rate, epochs):\n",
" """ learning loop """\n",
" def loss(example, w, idx_i, idx_t, h):\n",
" """ error: difference between estimation and true value """\n",
" raise NotImplementedError\n",
" \n",
" def update(w, learning_rate, err, h, X_col, num_examples):\n",
" """ update weights """\n",
" raise NotImplementedError\n",
"\n",
" def homogeneous(num_examples):\n",
" """ build homogeneous coordinates """\n",
" raise NotImplementedError\n",
"\n",
" raise NotImplementedError\n",
"\n",
" def predict(self, x):\n",
" """ make prediction """\n",
" return int(np.dot(self.w, [1] + x))\n",
"