master
regulation lbjai 4 years ago
14 changed file(s) with 2175 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 {
1 "cells": [
2 {
3 "cell_type": "markdown",
4 "metadata": {
5 "toc-hr-collapsed": true
6 },
7 "source": [
8 "### 1.平台介绍"
9 ]
10 },
11 {
12 "cell_type": "markdown",
13 "metadata": {},
14 "source": [
15 "#### 1.1 平台网址 https://mo.zju.edu.cn\n",
16 "(请使用最新版本 Chrome 浏览器)\n",
17 "<img src=\"https://imgbed.momodel.cn/20210311155838.png\" width=800px/>"
18 ]
19 },
20 {
21 "cell_type": "markdown",
22 "metadata": {},
23 "source": [
24 "#### 1.2 平台特色——提供 AI 教学与开发的线上建模实训平台\n",
25 "<img src=\"https://imgbed.momodel.cn/20210311160007.png\" width=1200px/>"
26 ]
27 },
28 {
29 "cell_type": "markdown",
30 "metadata": {
31 "toc-hr-collapsed": true
32 },
33 "source": [
34 "### 2. 注册与登录\n",
35 "\n"
36 ]
37 },
38 {
39 "cell_type": "markdown",
40 "metadata": {},
41 "source": [
42 "#### 2.1 注册与登录入口 \n",
43 "\n",
44 "<img src=\"https://imgbed.momodel.cn/20210311160918.png\"/>\n"
45 ]
46 },
47 {
48 "cell_type": "markdown",
49 "metadata": {},
50 "source": [
51 "#### 2.2 注册\n",
52 "\n",
53 "<img src=\"https://imgbed.momodel.cn/20210310093512.png\"/>"
54 ]
55 },
56 {
57 "cell_type": "markdown",
58 "metadata": {},
59 "source": [
60 "#### 2.3 免密登录(**推荐大家使用**)\n",
61 "\n",
62 "<img src=\"https://imgbed.momodel.cn/20210310093719.png\"/>\n",
63 "\n"
64 ]
65 },
66 {
67 "cell_type": "markdown",
68 "metadata": {},
69 "source": [
70 "#### 2.4 密码登录\n",
71 "<img src=\"https://imgbed.momodel.cn/20210310093605.png\"/>"
72 ]
73 },
74 {
75 "cell_type": "markdown",
76 "metadata": {
77 "inputHidden": false
78 },
79 "source": [
80 "#### 2.5 第三方登录(微信登录)\n",
81 "\n",
82 "<img src=\"https://imgbed.momodel.cn/20210310093808.png\"/>"
83 ]
84 },
85 {
86 "cell_type": "markdown",
87 "metadata": {
88 "toc-hr-collapsed": true
89 },
90 "source": [
91 "### 3. 个人信息"
92 ]
93 },
94 {
95 "cell_type": "markdown",
96 "metadata": {},
97 "source": [
98 "#### 3.1 查看个人信息\n",
99 "\n",
100 "<img src=\"https://imgbed.momodel.cn/20210311142541.png\"/>"
101 ]
102 },
103 {
104 "cell_type": "markdown",
105 "metadata": {},
106 "source": [
107 "#### 3.2 **邮箱验证与绑定浙大通行证**\n",
108 "\n",
109 "<img src=\"https://imgbed.momodel.cn/20210310094017.png\"/>"
110 ]
111 },
112 {
113 "cell_type": "markdown",
114 "metadata": {},
115 "source": [
116 "#### 3.3 工作台介绍\n",
117 "\n",
118 "<img src=\"https://imgbed.momodel.cn/20210310173010.png\" />"
119 ]
120 },
121 {
122 "cell_type": "markdown",
123 "metadata": {
124 "toc-hr-collapsed": false
125 },
126 "source": [
127 "### 4.项目环境介绍"
128 ]
129 },
130 {
131 "cell_type": "markdown",
132 "metadata": {},
133 "source": [
134 "#### 4.1 JupyterLab\n",
135 "<img src=\"https://imgbed.momodel.cn/20210310094308.png\"/>"
136 ]
137 },
138 {
139 "cell_type": "markdown",
140 "metadata": {},
141 "source": [
142 "#### 4.2 新建 Notebook 并进行编辑\n",
143 "\n",
144 "<img src=\"https://imgbed.momodel.cn/20210310094944.png\"/>"
145 ]
146 },
147 {
148 "cell_type": "markdown",
149 "metadata": {
150 "slideshow": {
151 "slide_type": "slide"
152 }
153 },
154 "source": [
155 "#### 4.3 重启 JupyterLab kernel\n",
156 "\n",
157 "<img src=\"https://imgbed.momodel.cn/20210310101410.png\" />"
158 ]
159 },
160 {
161 "cell_type": "markdown",
162 "metadata": {},
163 "source": [
164 "#### 4.4 API 查询接口\n",
165 "\n",
166 "<img src=\"https://imgbed.momodel.cn/20210310094538.png\"/>"
167 ]
168 },
169 {
170 "cell_type": "markdown",
171 "metadata": {},
172 "source": [
173 "#### 4.5 添加协助成员\n",
174 "<img src=\"https://imgbed.momodel.cn/20210310102338.png\"/>"
175 ]
176 },
177 {
178 "cell_type": "markdown",
179 "metadata": {
180 "toc-hr-collapsed": false
181 },
182 "source": [
183 "### 5. 离线任务"
184 ]
185 },
186 {
187 "cell_type": "markdown",
188 "metadata": {
189 "slideshow": {
190 "slide_type": "slide"
191 }
192 },
193 "source": [
194 "#### 5.1 邮箱未验证不能进行离线训练\n",
195 "\n",
196 "<img src=\"https://imgbed.momodel.cn/20210310095057.png\"/>"
197 ]
198 },
199 {
200 "cell_type": "markdown",
201 "metadata": {},
202 "source": [
203 "#### 5.2 创建离线任务的流程\n",
204 "\n",
205 "<img src=\"https://imgbed.momodel.cn/20210310094627.png\"/>"
206 ]
207 },
208 {
209 "cell_type": "markdown",
210 "metadata": {
211 "toc-hr-collapsed": false
212 },
213 "source": [
214 "#### 5.3 离线任务的运行状态\n",
215 "\n",
216 "<img src=\"https://imgbed.momodel.cn/20210310094819.png\"/>"
217 ]
218 },
219 {
220 "cell_type": "markdown",
221 "metadata": {},
222 "source": [
223 "### 6.联系我们\n",
224 "<img src=\"https://imgbed.momodel.cn/20210312100200.png\" />\n"
225 ]
226 },
227 {
228 "cell_type": "markdown",
229 "metadata": {},
230 "source": [
231 "<img src=\"https://imgbed.momodel.cn/20210312100311.png\"/>"
232 ]
233 },
234 {
235 "cell_type": "markdown",
236 "metadata": {},
237 "source": [
238 "<img src=\"https://imgbed.momodel.cn/20210312100519.png\" />"
239 ]
240 }
241 ],
242 "metadata": {
243 "kernelspec": {
244 "display_name": "Python 3",
245 "language": "python",
246 "name": "python3"
247 },
248 "language_info": {
249 "codemirror_mode": {
250 "name": "ipython",
251 "version": 3
252 },
253 "file_extension": ".py",
254 "mimetype": "text/x-python",
255 "name": "python",
256 "nbconvert_exporter": "python",
257 "pygments_lexer": "ipython3",
258 "version": "3.7.5"
259 }
260 },
261 "nbformat": 4,
262 "nbformat_minor": 4
263 }
0 {
1 "cells": [
2 {
3 "cell_type": "markdown",
4 "metadata": {
5 "inputHidden": false
6 },
7 "source": [
8 "# 线性回归"
9 ]
10 },
11 {
12 "cell_type": "markdown",
13 "metadata": {},
14 "source": [
15 "## 1.线性回归介绍\n",
16 "\n",
17 "我们先来看一段视频,了解什么是线性回归,并了解一些监督学习相关的概念。"
18 ]
19 },
20 {
21 "cell_type": "markdown",
22 "metadata": {
23 "inputHidden": false
24 },
25 "source": [
26 "<video src=\"https://imgbed.momodel.cn/%E6%A8%A1%E5%9E%8B%E8%A1%A8%E7%A4%BA1.mp4\" width=700 controls>"
27 ]
28 },
29 {
30 "cell_type": "markdown",
31 "metadata": {
32 "inputHidden": false
33 },
34 "source": [
35 "我们刚才以房价预测例子,介绍了线性回归、监督学习、训练集等相关的概念。 \n",
36 "看看下面这张图,回忆一下刚才讲的内容,数据集中的输入输出分别是什么?"
37 ]
38 },
39 {
40 "cell_type": "markdown",
41 "metadata": {
42 "inputHidden": false
43 },
44 "source": [
45 "<img src=\"https://imgbed.momodel.cn/%E6%A8%A1%E5%9E%8B%E8%A1%A8%E7%A4%BA1.png\" width=700>"
46 ]
47 },
48 {
49 "cell_type": "markdown",
50 "metadata": {
51 "inputHidden": false
52 },
53 "source": [
54 "如果现在有一个房子 H1,面积是 S,监督学习如何估算它的价格?\n",
55 "\n",
56 "1. 监督学习从训练集中找到面积最接近 S 的房子 H2,预测 H1 的价格等于 H2 的价格\n",
57 "2. 监督学习根据训练集,找到一个数学表达式,对任意的面积的房子都可以估算出其价格\n",
58 "\n",
59 "监督学习会采用上面的哪一种方案呢?"
60 ]
61 },
62 {
63 "cell_type": "markdown",
64 "metadata": {},
65 "source": [
66 "## 2.监督学习的一般流程"
67 ]
68 },
69 {
70 "cell_type": "markdown",
71 "metadata": {
72 "inputHidden": false
73 },
74 "source": [
75 "<video src=\"https://imgbed.momodel.cn/%E6%A8%A1%E5%9E%8B%E8%A1%A8%E7%A4%BA2.mp4\" width=700 controls>"
76 ]
77 },
78 {
79 "cell_type": "markdown",
80 "metadata": {},
81 "source": [
82 "我们刚才了解了监督学习算法是怎么工作的,并且理解了代价函数的含义。"
83 ]
84 },
85 {
86 "cell_type": "markdown",
87 "metadata": {
88 "inputHidden": false
89 },
90 "source": [
91 "## 3.模型假设\n",
92 "\n",
93 "看看下面这张图,回忆一下刚才讲的内容,图中的 h 代表假设函数,我们怎么构建一个合理的假设函数?\n",
94 "\n",
95 "在设计假设函数的时候需要考虑那些因素,本章节将带领大家解决这些问题。"
96 ]
97 },
98 {
99 "cell_type": "markdown",
100 "metadata": {
101 "inputHidden": false
102 },
103 "source": [
104 "<img src=\"https://imgbed.momodel.cn/%E6%A8%A1%E5%9E%8B%E8%A1%A8%E7%A4%BA2.png\" width=700>"
105 ]
106 },
107 {
108 "cell_type": "markdown",
109 "metadata": {
110 "inputHidden": false
111 },
112 "source": [
113 "下面,我们对这三个表达式进行绘图,你可以尝试修改 a,b,c 的值来观察绘图的变化情况。"
114 ]
115 },
116 {
117 "cell_type": "code",
118 "execution_count": null,
119 "metadata": {},
120 "outputs": [],
121 "source": [
122 "# 导入绘图需要的包\n",
123 "import numpy as np\n",
124 "import matplotlib.pyplot as plt\n",
125 "%matplotlib inline\n",
126 "\n",
127 "def plot_lines(a, b, c):\n",
128 " x = np.linspace(0, 5, 50)\n",
129 " # 进行绘图\n",
130 " plt.figure(dpi=100)\n",
131 " plt.plot(x, b*x+c, label='bx + c')\n",
132 " plt.plot(x, b*x, label='bx')\n",
133 " plt.plot(x, a*x*x+b*x+c, label='ax^2 + bx + c')\n",
134 " plt.legend()\n",
135 " plt.show()"
136 ]
137 },
138 {
139 "cell_type": "code",
140 "execution_count": null,
141 "metadata": {},
142 "outputs": [],
143 "source": [
144 "from ipywidgets import interact, interactive, fixed, interact_manual\n",
145 "import ipywidgets as widgets\n",
146 "\n",
147 "# 拖拽修改参数 a, b, c 的值来观察绘图的变化情况\n",
148 "interact(plot_lines,\n",
149 " a=widgets.FloatSlider(min=-10, max=10, step=0.5, value=1),\n",
150 " b=widgets.FloatSlider(min=-10, max=10, step=0.5, value=1),\n",
151 " c=widgets.FloatSlider(min=-10, max=10, step=0.5, value=1),\n",
152 " );"
153 ]
154 },
155 {
156 "cell_type": "code",
157 "execution_count": null,
158 "metadata": {},
159 "outputs": [],
160 "source": [
161 "# 运行下面的代码,查看问题并回答\n",
162 "from widget_help import question1\n",
163 "question1()"
164 ]
165 },
166 {
167 "cell_type": "markdown",
168 "metadata": {
169 "inputHidden": false
170 },
171 "source": [
172 "<video src=\"https://imgbed.momodel.cn/%E6%A8%A1%E5%9E%8B%E8%A1%A8%E7%A4%BA3.mp4\" width=700 controls>"
173 ]
174 },
175 {
176 "cell_type": "markdown",
177 "metadata": {
178 "inputHidden": false
179 },
180 "source": [
181 "我们刚才了解了线性回归模型中的模型假设,所以得到的线性回归的假设函数是:\n",
182 "\n",
183 "$h_{\\theta}(x)=\\theta_{0}+\\theta_{1} x$"
184 ]
185 },
186 {
187 "cell_type": "markdown",
188 "metadata": {},
189 "source": [
190 "## 模型求解"
191 ]
192 },
193 {
194 "cell_type": "markdown",
195 "metadata": {},
196 "source": [
197 "那么我们如何得到合适的 $\\theta_{0}$ 和 $\\theta_{1}$ 呢?\n",
198 "\n",
199 "我们有两种解决思路:\n",
200 "\n",
201 "1. 尝试一些 $\\theta_{0}$ 和 $\\theta_{1}$ 的组合,选择能使得画出的直线正好穿过训练集的 $\\theta_{0}$ 和 $\\theta_{1}$。\n",
202 "2. 尝试一些 $\\theta_{0}$ 和 $\\theta_{1}$ 的组合,然后在训练集上进行预测,选能使得预测值与真实的房子价格最接近的 $\\theta_{0}$ 和 $\\theta_{1}$。"
203 ]
204 },
205 {
206 "cell_type": "markdown",
207 "metadata": {
208 "inputHidden": false
209 },
210 "source": [
211 "<video src=\"https://imgbed.momodel.cn/%E4%BB%A3%E4%BB%B7%E5%87%BD%E6%95%B01.mp4\" width=700 controls>"
212 ]
213 },
214 {
215 "cell_type": "markdown",
216 "metadata": {
217 "inputHidden": false
218 },
219 "source": [
220 "刚才我们看到了根据不同的 $\\theta_{0}$ 和 $\\theta_{1}$ 绘制出的直线是不同的。\n"
221 ]
222 },
223 {
224 "cell_type": "markdown",
225 "metadata": {
226 "inputHidden": false
227 },
228 "source": [
229 "<img src=\"http://imgbed.momodel.cn/20201209163921.png\" width=600>"
230 ]
231 },
232 {
233 "cell_type": "markdown",
234 "metadata": {
235 "inputHidden": false
236 },
237 "source": [
238 "我们现在希望能选择最佳的 $\\theta_{0}$ 和 $\\theta_{1}$,使得 $h_{\\theta}(x)$ 对所有的训练样本 $(x, y)$ 来说,尽可能的接近 $y$。 \n",
239 "\n",
240 "我们该如何将其转化为数学表达呢?你认为它是下面三种表达式中的哪一种呢?\n",
241 "\n",
242 "1. $minimize \\frac{1}{2 m} \\sum_{i=1}^{m}\\left(h_{\\theta}\\left(x^{(i)}\\right)-y^{(i)}\\right)^{2}$\n",
243 "\n",
244 "2. $minimize \\frac{1}{m} \\sum_{i=1}^{m}\\left(h_{\\theta}\\left(x^{(i)}\\right)-y^{(i)}\\right)$\n",
245 "\n",
246 "3. $minimize \\frac{1}{m} \\sum_{i=1}^{m}\\left|h_{\\theta}\\left(x^{(i)}\\right)-y^{(i)}\\right|$"
247 ]
248 },
249 {
250 "cell_type": "markdown",
251 "metadata": {
252 "inputHidden": false
253 },
254 "source": [
255 "<video src=\"https://imgbed.momodel.cn/%E4%BB%A3%E4%BB%B7%E5%87%BD%E6%95%B02.mp4\" width=700 controls>"
256 ]
257 },
258 {
259 "cell_type": "markdown",
260 "metadata": {
261 "inputHidden": false
262 },
263 "source": [
264 "刚才我们了解选择最小化 $ \\frac{1}{2 m} \\sum_{i=1}^{m}\\left(h_{\\theta}\\left(x^{(i)}\\right)-y^{(i)}\\right)^{2}$ 得到的 $\\theta_{0}$ 和 $\\theta_{1}$ 是最佳的。"
265 ]
266 },
267 {
268 "cell_type": "markdown",
269 "metadata": {
270 "inputHidden": false
271 },
272 "source": [
273 "<img src=\"https://imgbed.momodel.cn/%E4%BB%A3%E4%BB%B7%E5%87%BD%E6%95%B02.png\" width=700>"
274 ]
275 },
276 {
277 "cell_type": "markdown",
278 "metadata": {
279 "inputHidden": false
280 },
281 "source": [
282 "现有 4 个房屋的面积和价格数据,和一个线性回归模型 $h_{\\theta}(x)=\\theta_{0}+\\theta_{1} x$。\n",
283 "\n",
284 "现在让我们调整参数 $\\theta_{0}$,$\\theta_{1}$ ,观察线性回归模型的绘图情况,及对应的“损失”是多少。\n"
285 ]
286 },
287 {
288 "cell_type": "code",
289 "execution_count": null,
290 "metadata": {},
291 "outputs": [],
292 "source": [
293 "# 房屋的价格和面积数据\n",
294 "import numpy as np\n",
295 "data = np.array([[2104, 460], [1416, 232], [1534, 315], [852,178]])"
296 ]
297 },
298 {
299 "cell_type": "markdown",
300 "metadata": {
301 "inputHidden": false
302 },
303 "source": [
304 "我们现在知道了线性回归的模型就是 \n",
305 "\n",
306 "$h_{\\theta}(x)=\\theta_{0}+\\theta_{1} x$\n",
307 "\n",
308 "试试看按照公式补全下方代码"
309 ]
310 },
311 {
312 "cell_type": "code",
313 "execution_count": null,
314 "metadata": {},
315 "outputs": [],
316 "source": [
317 "# 使用线性回归模型计算预测值\n",
318 "def get_predict(x, theta0, theta1):\n",
319 " #todo \n",
320 " return "
321 ]
322 },
323 {
324 "cell_type": "markdown",
325 "metadata": {
326 "inputHidden": false
327 },
328 "source": [
329 "<span class='md-answer-link pop 2'>问题提示</span> <span class='md-answer-link insert 2'>插入答案</span>"
330 ]
331 },
332 {
333 "cell_type": "markdown",
334 "metadata": {},
335 "source": [
336 "编程实现计算损失函数值"
337 ]
338 },
339 {
340 "cell_type": "code",
341 "execution_count": null,
342 "metadata": {},
343 "outputs": [],
344 "source": [
345 "# 计算损失值\n",
346 "def cal_cost(theta0, theta1):\n",
347 " cost = 0\n",
348 " for x, y in data:\n",
349 " y_pred = get_predict(x, theta0, theta1)\n",
350 " cost += (y_pred - y)**2\n",
351 " return round(cost/len(data)/2, 2)\n"
352 ]
353 },
354 {
355 "cell_type": "markdown",
356 "metadata": {},
357 "source": [
358 "利用 matplotlib 绘图工具包,绘制不同参数值下,房屋面积和房屋价格的关系图。"
359 ]
360 },
361 {
362 "cell_type": "code",
363 "execution_count": null,
364 "metadata": {},
365 "outputs": [],
366 "source": [
367 "# 导入绘图需要的包\n",
368 "import numpy as np\n",
369 "import matplotlib.pyplot as plt\n",
370 "%matplotlib inline\n",
371 "\n",
372 "def plot_cost(theta0, theta1):\n",
373 " x = np.linspace(0, 2500, 50)\n",
374 " # 进行绘图\n",
375 " plt.figure(dpi=100)\n",
376 " plt.plot(x, get_predict(x, theta0, theta1),\n",
377 " label='theta0=' + str(theta0) + ',theta1='+ str(theta1)\n",
378 " + ',cost='+ str(cal_cost(theta0=theta0, theta1=theta1)))\n",
379 " plt.plot(data[:,0],data[:,1], 'bo')\n",
380 " plt.legend()\n",
381 " plt.xlabel(\"面积\")\n",
382 " plt.ylabel(\"价格\")\n",
383 " plt.show()"
384 ]
385 },
386 {
387 "cell_type": "code",
388 "execution_count": null,
389 "metadata": {},
390 "outputs": [],
391 "source": [
392 "from ipywidgets import interact, interactive, fixed, interact_manual\n",
393 "import ipywidgets as widgets\n",
394 "\n",
395 "# 拖拽修改参数 theta0,theta1 的值来观察绘图的变化情况\n",
396 "interact(plot_cost,\n",
397 " theta0=widgets.IntSlider(min=-200, max=200, step=10, value=10),\n",
398 " theta1=widgets.FloatSlider(min=-1.0, max=1.0, step=0.05, value=0.3));\n"
399 ]
400 },
401 {
402 "cell_type": "markdown",
403 "metadata": {},
404 "source": [
405 "## 代价函数与梯度下降算法"
406 ]
407 },
408 {
409 "cell_type": "markdown",
410 "metadata": {
411 "inputHidden": false
412 },
413 "source": [
414 "<video src=\"https://imgbed.momodel.cn/cost_function_1.mp4\" width=700 controls>"
415 ]
416 },
417 {
418 "cell_type": "markdown",
419 "metadata": {},
420 "source": [
421 "根据房价预测例子,我们抽象出假设函数(Hypothesis),模型参数(Parameters),代价函数(Cost Function),优化目标(Goal)。"
422 ]
423 },
424 {
425 "cell_type": "markdown",
426 "metadata": {},
427 "source": [
428 "<img src=\"https://imgbed.momodel.cn/%E4%BB%A3%E4%BB%B7%E5%87%BD%E6%95%B0%E7%90%86%E8%A7%A31.jpg\" width=600>"
429 ]
430 },
431 {
432 "cell_type": "markdown",
433 "metadata": {},
434 "source": [
435 "<video src=\"https://imgbed.momodel.cn/cost_function_2.mp4\" width=700 controls>"
436 ]
437 },
438 {
439 "cell_type": "markdown",
440 "metadata": {},
441 "source": [
442 "为理解方便,下面讲解中将 h 函数简化为 $h_{0}(x) =\\theta_{1}x$,即设 $\\theta_{0}=0$ 。由此我们可以画出假设 h 和优化函数 $J(\\theta)$ 对应的函数图像。\n",
443 "\n",
444 "通过图像,我们可以很清楚的理解假设函数和代价函数之间的对应关系\n",
445 "\n",
446 "<img src='https://imgbed.momodel.cn/5c0652fe1637f.png' width=617 height=345 >"
447 ]
448 },
449 {
450 "cell_type": "markdown",
451 "metadata": {},
452 "source": [
453 "我们假设 $\\theta_0=0$,仅考虑了 $\\theta_1$, 得到的图像是一个弓形曲线。如果我们考虑 $[\\theta_0, \\theta_1]$ 两个参数,得到的图像是什么样子呢?"
454 ]
455 },
456 {
457 "cell_type": "markdown",
458 "metadata": {},
459 "source": [
460 "<video src=\"https://imgbed.momodel.cn/cost_function_3.mp4\" width=700 controls>"
461 ]
462 },
463 {
464 "cell_type": "markdown",
465 "metadata": {},
466 "source": [
467 "我们刚才通过三维可视化来理解有两个参数情况下,假设函数和代价函数的关系。\n",
468 "\n",
469 "从代价函数的可视化图像(下图所示)中可以看出在三维空间中存在一个使得 $J(\\theta_{0}, \\theta_{1})$最小的点。 "
470 ]
471 },
472 {
473 "cell_type": "markdown",
474 "metadata": {},
475 "source": [
476 "<img src=\"https://imgbed.momodel.cn/%E4%BB%A3%E4%BB%B7%E5%87%BD%E6%95%B0%E7%90%86%E8%A7%A32.jpg\" width=700>"
477 ]
478 },
479 {
480 "cell_type": "markdown",
481 "metadata": {},
482 "source": [
483 "通过可视化方法,希望你能更好地理解这些代价函数 $J$ 所表达的值是什么样的,它们对应的假设是什么样的,以及什么样的假设对应的点,更接近于代价函数 $J$ 的最小值。 \n",
484 "\n",
485 "当然,我们真正需要的是一种有效的算法,能够自动地找出这些使代价函数 $J$ 取最小值的参数 $\\theta_{0}$ 和 $\\theta_{1}$。 \n",
486 "\n",
487 "我们也不希望编程序把这些点画出来,然后人工的方法来读出这些点的数值,这很明显不是一个好办法。 \n",
488 "我们会遇到更复杂、更高维度、更多参数的情况,而这些情况是很难画出图的,也就是很难将其可视化。 \n",
489 "因此我们真正需要的是编写程序来找出这些最小化代价函数的 $\\theta_{0}$ 和 $\\theta_{1}$ 的值。 "
490 ]
491 },
492 {
493 "cell_type": "markdown",
494 "metadata": {
495 "inputHidden": false
496 },
497 "source": [
498 "<video src=\"https://imgbed.momodel.cn/grad_des.mp4\" width=700 controls>"
499 ]
500 },
501 {
502 "cell_type": "markdown",
503 "metadata": {
504 "inputHidden": false
505 },
506 "source": [
507 "梯度下降是一个用来求函数最小值的算法,我们将使用梯度下降算法来求出代价函数$J(\\theta_{0}, \\theta_{1})$的最小值。\n",
508 "\n",
509 "通过前面的视频,我们先来理解一下使用梯度下降算法求解代价函数最小值的问题概述和具体过程。\n",
510 "\n",
511 "<img src=\"https://imgbed.momodel.cn/%E6%A2%AF%E5%BA%A6%E4%B8%8B%E9%99%8D1.jpg\" width=700>"
512 ]
513 },
514 {
515 "cell_type": "markdown",
516 "metadata": {},
517 "source": [
518 "我们先来通过视频直观的理解梯度下降求解最小值的思想。"
519 ]
520 },
521 {
522 "cell_type": "markdown",
523 "metadata": {
524 "inputHidden": false
525 },
526 "source": [
527 "<video src=\"https://imgbed.momodel.cn/grad_des2.mp4\" width=700 controls>"
528 ]
529 },
530 {
531 "cell_type": "markdown",
532 "metadata": {
533 "inputHidden": false
534 },
535 "source": [
536 "梯度下降背后的思想是:开始时我们随机选择一个参数的组合 $(\\theta_{0},\\theta_{1},......,\\theta_{n})$ , \n",
537 "计算代价函数,然后我们寻找下一个能让代价函数值下降最多的参数组合。\n",
538 "\n",
539 "我们持续这么做直到抵达一个局部最小值(local minimum),因为我们并没有尝试完所有的参数组合, \n",
540 "所以不能确定我们得到的局部最小值是否便是全局最小值(global minimum),选择不同的初始参数组合, \n",
541 "可能会找到不同的局部最小值。 \n",
542 " \n",
543 " <img src='https://imgbed.momodel.cn/5c00c262c5885.png' width=600 >"
544 ]
545 },
546 {
547 "cell_type": "markdown",
548 "metadata": {},
549 "source": [
550 "现在我们来看一下梯度下降算法背后的数学原理,这部分的理解需要一些简单[微积分知识](http://open.163.com/newview/movie/courseintro?newurl=%2Fspecial%2Fopencourse%2Fweijifen.html), \n",
551 "如果对微积分的知识不了解也不用担心,跟着视频的思路也能很好的理解梯度下降算法。"
552 ]
553 },
554 {
555 "cell_type": "markdown",
556 "metadata": {},
557 "source": [
558 "<video src=\"https://imgbed.momodel.cn/grad_des3.mp4\" width=700 controls>"
559 ]
560 },
561 {
562 "cell_type": "markdown",
563 "metadata": {},
564 "source": [
565 "**细节注意:更新$\\theta_{0}$和 $\\theta_{1}$。**\n",
566 "\n",
567 "实现梯度下降算法的微妙之处是,在这个表达式中,如果你要更新这个等式,需要同时更新 $\\theta_{0}$和 $\\theta_{1}$, \n",
568 "实现方法是:你应该计算公式右边的部分,通过那一部分计算出$\\theta_{0}$和 $\\theta_{1}$的值,然后同时更新$\\theta_{0}$和$\\theta_{1}$。 \n",
569 "\n",
570 "<div class='insertContainerBox row'>\n",
571 "<div class='insertItem' align=left><img src=\"https://imgbed.momodel.cn/5c00c5c1e5ac9.png\" width=\"200px\"/></div>\n",
572 "</div>\n",
573 "\n",
574 "\n",
575 "在梯度下降算法中,这是正确实现同时更新的方法。注意区别不正确的写法: \n",
576 "\n",
577 "<div class='insertContainerBox row'>\n",
578 "<div class='insertItem' align=left><img src=\"https://imgbed.momodel.cn/5c00c5a2e91fd.png\" width=\"200px\"/></div>\n",
579 "</div>"
580 ]
581 },
582 {
583 "cell_type": "markdown",
584 "metadata": {},
585 "source": [
586 "接下来我们来更深入的理解梯度下降算法是干什么的以及梯度下降算法更新过程有什么意义。\n",
587 "\n",
588 "** 高能预警!开启烧脑模式!**"
589 ]
590 },
591 {
592 "cell_type": "markdown",
593 "metadata": {},
594 "source": [
595 "<video src=\"https://imgbed.momodel.cn/grad_des4.mp4\" width=700 controls>"
596 ]
597 },
598 {
599 "cell_type": "markdown",
600 "metadata": {},
601 "source": [
602 "这里面需要注意的是,学习率 $\\alpha$ 如何选择。\n",
603 "\n",
604 "让我们来看看如果 $\\alpha$ 太小或 $\\alpha$ 太大会出现什么情况: \n",
605 "\n",
606 "<img src='https://imgbed.momodel.cn/5c00c82c21491.png' width=500 >\n",
607 "\n"
608 ]
609 },
610 {
611 "cell_type": "markdown",
612 "metadata": {},
613 "source": [
614 "\n",
615 "如果 $\\alpha$ 太小或 $\\alpha$ 太大会出现什么情况\n",
616 "\n",
617 "\n",
618 "+ 如果 $\\alpha$ 太小了,即我的学习速率太小,可能会很慢,因为它会一点点挪动,它会需要很多步才能到达全局最低点。 \n",
619 "+ 如果 $\\alpha$ 太大,那么梯度下降法可能会越过最低点,下一次迭代又移动了一大步,越过一次,又越过一次,一次次越过最低点,直到你发现实际上离最低点越来越远,最终会导致无法收敛,甚至发散。 "
620 ]
621 },
622 {
623 "cell_type": "markdown",
624 "metadata": {},
625 "source": [
626 "前面我们讲解了代价函数的含义,线性回归模型和梯度下降算法。\n",
627 "\n",
628 "接下来我们要结合梯度下降和代价函数,完成线性回归算法建模,得到预测房屋价格的模型。"
629 ]
630 },
631 {
632 "cell_type": "markdown",
633 "metadata": {},
634 "source": [
635 "<video src=\"https://imgbed.momodel.cn/grad_des5.mp4\" width=700 controls>"
636 ]
637 },
638 {
639 "cell_type": "markdown",
640 "metadata": {},
641 "source": [
642 "在上面这段视频中,我们要将梯度下降应用于具体的拟合直线的线性回归算法里。 \n",
643 "\n",
644 "梯度下降算法和线性回归算法,如图:\n",
645 "\n",
646 "<img src='https://imgbed.momodel.cn/5c00cf2af35aa.png' width=600 >\n",
647 " \n",
648 "对我们之前的线性回归问题运用梯度下降法,关键在于求出代价函数的导数,即: \n",
649 " $$\\frac{\\partial}{\\partial{\\theta_{j}}}J(\\theta_{0}, \\theta_{1}) = \\frac{\\partial}{\\partial{\\theta_{j}}}\\frac{1}{2m}\\sum_{i=1}^m(h_{\\theta}(x^{(i)})-y^{(i)})^{2}$$\n",
650 " \n",
651 " $$j=0 时: \\frac{\\partial}{\\partial{\\theta_{0}}}J(\\theta_{0}, \\theta_{1}) = \\frac{1}{m}\\sum_{i=1}^m(h_{\\theta}(x^{(i)})-y^{(i)})$$\n",
652 " \n",
653 " \n",
654 "$$j=1 时:\\frac{\\partial}{\\partial{\\theta_{1}}}J(\\theta_{0}, \\theta_{1}) = \\frac{1}{m}\\sum_{i=1}^m((h_{\\theta}(x^{(i)})-y^{(i)})*x^{(i)})$$\n",
655 " \n",
656 "则算法改写成: \n",
657 "\n",
658 "<img src='https://imgbed.momodel.cn/5c00d3aacf68e.png' width=600 >"
659 ]
660 },
661 {
662 "cell_type": "markdown",
663 "metadata": {},
664 "source": [
665 "<video src=\"https://imgbed.momodel.cn/grad_des6.mp4\" width=700 controls>"
666 ]
667 },
668 {
669 "cell_type": "markdown",
670 "metadata": {},
671 "source": [
672 "以上视频讲解了梯度下降算法在线性回归中的应用及假设函数的拟合效果和代价函数的对应关系。"
673 ]
674 },
675 {
676 "cell_type": "markdown",
677 "metadata": {},
678 "source": [
679 "<video src=\"https://imgbed.momodel.cn/grad_des7.mp4\" width=700 controls>"
680 ]
681 },
682 {
683 "cell_type": "markdown",
684 "metadata": {},
685 "source": [
686 "我们刚刚使用的算法,被称为**批量梯度下降**。它指的是在梯度下降的每一步中,都用到了所有的训练样本, \n",
687 "在计算微分求导项时,需要进行求和运算。 \n",
688 "所以,在每一个单独的梯度下降中,都需要对所有m个训练样本求和。 \n",
689 "而事实上,也有其他类型的梯度下降法每次只关注训练集中的一些小的子集,后面的课程中会详细介绍。"
690 ]
691 },
692 {
693 "cell_type": "markdown",
694 "metadata": {},
695 "source": [
696 "<div align=left>\n",
697 "<video src=\"https://files.momodel.cn/线性回归_结尾.mp4\" controls=\"controls\" width=500/>\n",
698 "</div>"
699 ]
700 }
701 ],
702 "metadata": {
703 "kernelspec": {
704 "display_name": "Python 3",
705 "language": "python",
706 "name": "python3"
707 },
708 "language_info": {
709 "codemirror_mode": {
710 "name": "ipython",
711 "version": 3
712 },
713 "file_extension": ".py",
714 "mimetype": "text/x-python",
715 "name": "python",
716 "nbconvert_exporter": "python",
717 "pygments_lexer": "ipython3",
718 "version": "3.7.5"
719 }
720 },
721 "nbformat": 4,
722 "nbformat_minor": 4
723 }
0 {
1 "cells": [
2 {
3 "cell_type": "markdown",
4 "metadata": {},
5 "source": [
6 "<img src=\"https://imgbed.momodel.cn/20210311162712.png\" />"
7 ]
8 },
9 {
10 "cell_type": "markdown",
11 "metadata": {},
12 "source": [
13 "\n",
14 "<div class='insertContainerBox column'>\n",
15 "<div class='insertItem' align=center><img src=\"https://imgbed.momodel.cn/20210311162712.png\" width=\"200px\"/></div>\n",
16 "</div>\n"
17 ]
18 },
19 {
20 "cell_type": "markdown",
21 "metadata": {
22 "toc-hr-collapsed": true
23 },
24 "source": [
25 "### 1.平台介绍"
26 ]
27 },
28 {
29 "cell_type": "markdown",
30 "metadata": {},
31 "source": [
32 "#### 1.1 平台网址 https://mo.zju.edu.cn\n",
33 "(请使用最新版本 Chrome 浏览器)\n",
34 "<img src=\"https://imgbed.momodel.cn/20210311155838.png\" width=800px/>"
35 ]
36 },
37 {
38 "cell_type": "markdown",
39 "metadata": {},
40 "source": [
41 "#### 1.2 平台特色——提供 AI 教学与开发的线上建模实训平台\n",
42 "<img src=\"https://imgbed.momodel.cn/20210311160007.png\" width=1200px/>"
43 ]
44 },
45 {
46 "cell_type": "markdown",
47 "metadata": {
48 "toc-hr-collapsed": true
49 },
50 "source": [
51 "### 2. 注册与登录\n",
52 "\n"
53 ]
54 },
55 {
56 "cell_type": "markdown",
57 "metadata": {},
58 "source": [
59 "#### 2.1 注册与登录入口 \n",
60 "\n",
61 "<img src=\"https://imgbed.momodel.cn/20210311160918.png\"/>\n"
62 ]
63 },
64 {
65 "cell_type": "markdown",
66 "metadata": {},
67 "source": [
68 "#### 2.2 注册\n",
69 "\n",
70 "<img src=\"https://imgbed.momodel.cn/20210310093512.png\"/>"
71 ]
72 },
73 {
74 "cell_type": "markdown",
75 "metadata": {},
76 "source": [
77 "#### 2.3 免密登录(**推荐大家使用**)\n",
78 "\n",
79 "<img src=\"https://imgbed.momodel.cn/20210310093719.png\"/>\n",
80 "\n"
81 ]
82 },
83 {
84 "cell_type": "markdown",
85 "metadata": {},
86 "source": [
87 "#### 2.4 密码登录\n",
88 "<img src=\"https://imgbed.momodel.cn/20210310093605.png\"/>"
89 ]
90 },
91 {
92 "cell_type": "markdown",
93 "metadata": {
94 "inputHidden": false
95 },
96 "source": [
97 "#### 2.5 第三方登录(微信登录)\n",
98 "\n",
99 "<img src=\"https://imgbed.momodel.cn/20210310093808.png\"/>"
100 ]
101 },
102 {
103 "cell_type": "markdown",
104 "metadata": {
105 "toc-hr-collapsed": true
106 },
107 "source": [
108 "### 3. 个人信息"
109 ]
110 },
111 {
112 "cell_type": "markdown",
113 "metadata": {},
114 "source": [
115 "#### 3.1 查看个人信息\n",
116 "\n",
117 "<img src=\"https://imgbed.momodel.cn/20210311142541.png\"/>"
118 ]
119 },
120 {
121 "cell_type": "markdown",
122 "metadata": {},
123 "source": [
124 "#### 3.2 **邮箱验证与绑定浙大通行证**\n",
125 "\n",
126 "<img src=\"https://imgbed.momodel.cn/20210310094017.png\"/>"
127 ]
128 },
129 {
130 "cell_type": "markdown",
131 "metadata": {},
132 "source": [
133 "#### 3.3 工作台介绍\n",
134 "\n",
135 "<img src=\"https://imgbed.momodel.cn/20210310173010.png\" />"
136 ]
137 },
138 {
139 "cell_type": "markdown",
140 "metadata": {
141 "toc-hr-collapsed": false
142 },
143 "source": [
144 "### 4.项目环境介绍"
145 ]
146 },
147 {
148 "cell_type": "markdown",
149 "metadata": {},
150 "source": [
151 "#### 4.1 JupyterLab\n",
152 "<img src=\"https://imgbed.momodel.cn/20210310094308.png\"/>"
153 ]
154 },
155 {
156 "cell_type": "markdown",
157 "metadata": {},
158 "source": [
159 "#### 4.2 新建 Notebook 并进行编辑\n",
160 "\n",
161 "<img src=\"https://imgbed.momodel.cn/20210310094944.png\"/>"
162 ]
163 },
164 {
165 "cell_type": "markdown",
166 "metadata": {},
167 "source": [
168 "#### 4.3 API 查询接口\n",
169 "\n",
170 "<img src=\"https://imgbed.momodel.cn/20210310094538.png\"/>"
171 ]
172 },
173 {
174 "cell_type": "markdown",
175 "metadata": {
176 "toc-hr-collapsed": false
177 },
178 "source": [
179 "### 5. 离线任务"
180 ]
181 },
182 {
183 "cell_type": "markdown",
184 "metadata": {
185 "slideshow": {
186 "slide_type": "slide"
187 }
188 },
189 "source": [
190 "#### 5.1 邮箱未验证不能进行离线训练\n",
191 "\n",
192 "<img src=\"https://imgbed.momodel.cn/20210310095057.png\"/>"
193 ]
194 },
195 {
196 "cell_type": "markdown",
197 "metadata": {},
198 "source": [
199 "#### 5.2 创建离线任务的流程\n",
200 "\n",
201 "<img src=\"https://imgbed.momodel.cn/20210310094627.png\"/>"
202 ]
203 },
204 {
205 "cell_type": "markdown",
206 "metadata": {
207 "toc-hr-collapsed": false
208 },
209 "source": [
210 "#### 5.3 离线任务的运行状态\n",
211 "\n",
212 "<img src=\"https://imgbed.momodel.cn/20210310094819.png\"/>"
213 ]
214 },
215 {
216 "cell_type": "markdown",
217 "metadata": {
218 "toc-hr-collapsed": true
219 },
220 "source": [
221 "### 6. 课程介绍"
222 ]
223 },
224 {
225 "cell_type": "markdown",
226 "metadata": {
227 "slideshow": {
228 "slide_type": "slide"
229 }
230 },
231 "source": [
232 "#### 6.1 课程在训练营的位置\n",
233 "\n",
234 "路径:【训练营】-> 【官方课程】-> 【2021 春夏 - 人工智能】\n",
235 "\n",
236 "<img src=\"https://imgbed.momodel.cn/20210310095302.png\" />"
237 ]
238 },
239 {
240 "cell_type": "markdown",
241 "metadata": {
242 "toc-hr-collapsed": false
243 },
244 "source": [
245 "#### 6.2 章节目录介绍\n",
246 "\n",
247 "**https://mo.zju.edu.cn/classroom/class/zju_ai_2021**\n",
248 "\n",
249 "<img src=\"https://imgbed.momodel.cn/20210310095720.png\" />"
250 ]
251 },
252 {
253 "cell_type": "markdown",
254 "metadata": {},
255 "source": [
256 "#### 6.3 课程作业介绍\n",
257 "\n",
258 "<img src=\"https://imgbed.momodel.cn/20210310095831.png\" />"
259 ]
260 },
261 {
262 "cell_type": "markdown",
263 "metadata": {},
264 "source": [
265 "#### 6.4 课程作业开始时间和截止时间\n",
266 "\n",
267 "<img src=\"https://imgbed.momodel.cn/20210311134318.png\" />\n",
268 "\n",
269 "基本上每个作业开放时间是 4 周,每次作业开放后请大家尽早完成!\n"
270 ]
271 },
272 {
273 "cell_type": "markdown",
274 "metadata": {},
275 "source": [
276 "#### 6.5 实训作业常见问题解决方式\n",
277 "\n",
278 "<img src=\"https://imgbed.momodel.cn/20210310101741.png\"/>"
279 ]
280 },
281 {
282 "cell_type": "markdown",
283 "metadata": {
284 "toc-hr-collapsed": false
285 },
286 "source": [
287 "### 7.实训作业"
288 ]
289 },
290 {
291 "cell_type": "markdown",
292 "metadata": {},
293 "source": [
294 "#### 7.1 实训作业内容介绍(以八皇后问题为例)\n",
295 "<img src=\"https://imgbed.momodel.cn/20210310102907.png\"/>"
296 ]
297 },
298 {
299 "cell_type": "markdown",
300 "metadata": {},
301 "source": [
302 "<img src=\"https://imgbed.momodel.cn/20210310102933.png\"/>"
303 ]
304 },
305 {
306 "cell_type": "markdown",
307 "metadata": {},
308 "source": [
309 "<img src=\"https://imgbed.momodel.cn/20210310102959.png\"/>"
310 ]
311 },
312 {
313 "cell_type": "markdown",
314 "metadata": {},
315 "source": [
316 "#### 7.2 重置课程(防止修改或者代码丢失,重置之前请做好备份)\n",
317 "<img src=\"https://imgbed.momodel.cn/20210310101024.png\"/>"
318 ]
319 },
320 {
321 "cell_type": "markdown",
322 "metadata": {
323 "slideshow": {
324 "slide_type": "slide"
325 }
326 },
327 "source": [
328 "#### 7.3 重启 JupyterLab kernel\n",
329 "\n",
330 "<img src=\"https://imgbed.momodel.cn/20210310101410.png\" />"
331 ]
332 },
333 {
334 "cell_type": "markdown",
335 "metadata": {},
336 "source": [
337 "#### 7.4 重启课程\n",
338 "\n",
339 "<img src=\"https://imgbed.momodel.cn/20210310101548.png\" />\n"
340 ]
341 },
342 {
343 "cell_type": "markdown",
344 "metadata": {},
345 "source": [
346 "#### 7.5 联系我们\n",
347 "<img src=\"https://imgbed.momodel.cn/20210312100200.png\" />\n"
348 ]
349 },
350 {
351 "cell_type": "markdown",
352 "metadata": {},
353 "source": [
354 "<img src=\"https://imgbed.momodel.cn/20210312100311.png\"/>"
355 ]
356 },
357 {
358 "cell_type": "markdown",
359 "metadata": {},
360 "source": [
361 "<img src=\"https://imgbed.momodel.cn/20210312100519.png\" />"
362 ]
363 },
364 {
365 "cell_type": "markdown",
366 "metadata": {},
367 "source": [
368 "#### 7.6 添加协助成员\n",
369 "<img src=\"https://imgbed.momodel.cn/20210310102338.png\"/>"
370 ]
371 },
372 {
373 "cell_type": "markdown",
374 "metadata": {
375 "toc-hr-collapsed": true
376 },
377 "source": [
378 "### 8.测试提交"
379 ]
380 },
381 {
382 "cell_type": "markdown",
383 "metadata": {
384 "slideshow": {
385 "slide_type": "slide"
386 }
387 },
388 "source": [
389 "#### 8.1 生成 main.py 文件\n",
390 "<img src=\"https://imgbed.momodel.cn/20210310103125.png\" />"
391 ]
392 },
393 {
394 "cell_type": "markdown",
395 "metadata": {},
396 "source": [
397 "\n",
398 "<img src=\"https://imgbed.momodel.cn/20210310103152.png\"/>"
399 ]
400 },
401 {
402 "cell_type": "markdown",
403 "metadata": {},
404 "source": [
405 "<img src=\"https://imgbed.momodel.cn/20210310103216.png\"/>"
406 ]
407 },
408 {
409 "cell_type": "markdown",
410 "metadata": {},
411 "source": [
412 "<img src=\"https://imgbed.momodel.cn/20210310103237.png\"/>"
413 ]
414 },
415 {
416 "cell_type": "markdown",
417 "metadata": {},
418 "source": [
419 "#### 8.2 开始测试\n",
420 "<img src=\"https://imgbed.momodel.cn/20210310103306.png\"/>"
421 ]
422 },
423 {
424 "cell_type": "markdown",
425 "metadata": {},
426 "source": [
427 "#### 8.3 测试等待\n",
428 "<img src=\"https://imgbed.momodel.cn/20210310103332.png\"/>"
429 ]
430 },
431 {
432 "cell_type": "markdown",
433 "metadata": {},
434 "source": [
435 "#### 8.4 测试结果 \n",
436 "<img src=\"https://imgbed.momodel.cn/20210310173458.png\"/>"
437 ]
438 },
439 {
440 "cell_type": "markdown",
441 "metadata": {},
442 "source": [
443 "#### 8.5 不同测试结果简单说明\n",
444 "<img src=\"https://imgbed.momodel.cn/20210310173653.png\"/>"
445 ]
446 },
447 {
448 "cell_type": "markdown",
449 "metadata": {},
450 "source": [
451 "#### 8.6 未上传【程序报告.pdf】文件时提交结果\n",
452 "<img src=\"https://imgbed.momodel.cn/20210310173804.png\" />"
453 ]
454 },
455 {
456 "cell_type": "markdown",
457 "metadata": {},
458 "source": [
459 "#### 8.7 上传【程序报告.pdf】文件时提交结果\n",
460 "<img src=\"https://imgbed.momodel.cn/20210310173948.png\" />"
461 ]
462 },
463 {
464 "cell_type": "markdown",
465 "metadata": {},
466 "source": [
467 "#### 8.8 提交结果后展示\n",
468 "<img src=\"https://imgbed.momodel.cn/20210310174021.png\" />"
469 ]
470 },
471 {
472 "cell_type": "markdown",
473 "metadata": {},
474 "source": [
475 "**注意事项**: \n",
476 "1.请使用最新版本的 chrome 浏览器 \n",
477 "2.邮箱需要验证,请使用真实邮箱,不要使用临时邮箱 \n",
478 "3.离线任务训练时保存的模型、日志请保存在 results 文件夹 \n",
479 "4.离线任务训练时勾选的文件夹大小尽可能的小 \n",
480 "5.重置文件时请备份好自己的文件 \n",
481 "6.重启整个项目时请保存自己已经修改的文件 \n",
482 "7.main.py 文件不支持 jupyterlab 魔法写法 \n",
483 "\n",
484 "谢谢大家!"
485 ]
486 }
487 ],
488 "metadata": {
489 "kernelspec": {
490 "display_name": "Python 3",
491 "language": "python",
492 "name": "python3"
493 },
494 "language_info": {
495 "codemirror_mode": {
496 "name": "ipython",
497 "version": 3
498 },
499 "file_extension": ".py",
500 "mimetype": "text/x-python",
501 "name": "python",
502 "nbconvert_exporter": "python",
503 "pygments_lexer": "ipython3",
504 "version": "3.7.5"
505 }
506 },
507 "nbformat": 4,
508 "nbformat_minor": 4
509 }
0 {
1 "cells": [
2 {
3 "cell_type": "code",
4 "execution_count": 1,
5 "metadata": {},
6 "outputs": [
7 {
8 "name": "stdout",
9 "output_type": "stream",
10 "text": [
11 "hell world!\n"
12 ]
13 }
14 ],
15 "source": [
16 "print('hell world!')"
17 ]
18 },
19 {
20 "cell_type": "markdown",
21 "metadata": {},
22 "source": [
23 "### 人工智能\n",
24 "- 机器学习\n",
25 " - 线性回归\n",
26 " - 逻辑回归\n",
27 " - 聚类\n",
28 "- 深度学习\n",
29 " - 人工神经网络\n",
30 " - CNN\n",
31 " - RNN\n",
32 " - TensorFlow"
33 ]
34 },
35 {
36 "cell_type": "markdown",
37 "metadata": {},
38 "source": [
39 "#### 1.3 主要功能\n",
40 "<img src=\"https://imgbed.momodel.cn/20210311160548.png\" width=1000px/>"
41 ]
42 },
43 {
44 "cell_type": "code",
45 "execution_count": null,
46 "metadata": {},
47 "outputs": [],
48 "source": []
49 }
50 ],
51 "metadata": {
52 "kernelspec": {
53 "display_name": "Python 3",
54 "language": "python",
55 "name": "python3"
56 },
57 "language_info": {
58 "codemirror_mode": {
59 "name": "ipython",
60 "version": 3
61 },
62 "file_extension": ".py",
63 "mimetype": "text/x-python",
64 "name": "python",
65 "nbconvert_exporter": "python",
66 "pygments_lexer": "ipython3",
67 "version": "3.7.5"
68 }
69 },
70 "nbformat": 4,
71 "nbformat_minor": 4
72 }
0 ###############################################################################
1 # 重要: 请务必把任务(jobs)中需要保存的文件存放在 results 文件夹内
2 # Important : Please make sure your files are saved to the 'results' folder
3 # in your jobs
4 ###############################################################################
5 import torch
6 import torch.nn as nn
7 import torch.utils.data as Data
8 import torchvision
9 import time
10 import matplotlib.pyplot as plt
11
12
13 class MyCNN(nn.Module):
14 """
15 网络模型
16 """
17 def __init__(self, image_size, num_classes):
18 super(MyCNN, self).__init__()
19 # conv1: Conv2d -> BN -> ReLU -> MaxPool
20 self.conv1 = nn.Sequential(
21 nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1),
22 nn.BatchNorm2d(16),
23 nn.ReLU(),
24 nn.MaxPool2d(kernel_size=2, stride=2),
25 )
26 # conv2: Conv2d -> BN -> ReLU -> MaxPool
27 self.conv2 = nn.Sequential(
28 nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1),
29 nn.BatchNorm2d(32),
30 nn.ReLU(),
31 nn.MaxPool2d(kernel_size=2, stride=2),
32 )
33 # fully connected layer
34 self.fc = nn.Linear(32 * (image_size // 4) * (image_size // 4), num_classes)
35
36
37 def forward(self, x):
38 """
39 input: N * 3 * image_size * image_size
40 output: N * num_classes
41 """
42 x = self.conv1(x)
43 x = self.conv2(x)
44 # view(x.size(0), -1): change tensor size from (N ,H , W) to (N, H*W)
45 x = x.view(x.size(0), -1)
46 output = self.fc(x)
47 return output
48
49
50 def train(model, train_loader, loss_func, optimizer, device):
51 """
52 训练模型
53 train model using loss_fn and optimizer in an epoch.
54 model: CNN networks
55 train_loader: a Dataloader object with training data
56 loss_func: loss function
57 device: train on cpu or gpu device
58 """
59 total_loss = 0
60 # train the model using minibatch
61 for i, (images, targets) in enumerate(train_loader):
62 images = images.to(device)
63 targets = targets.to(device)
64
65 # forward
66 outputs = model(images)
67 _,preds = torch.max(outputs.data,1)
68 loss = loss_func(outputs, targets)
69
70 # backward and optimize
71 optimizer.zero_grad()
72 loss.backward()
73 optimizer.step()
74
75 total_loss += loss.item()
76 if (i + 1) % 100 == 0:
77 print ("Step [{}/{}] Train Loss: {:.4f} Train acc".format(i+1, len(train_loader), loss.item()))
78 save_model(model, save_path="results/cnn.pth")
79 return total_loss / len(train_loader)
80
81
82 def evaluate(model, val_loader, device):
83 """
84 评估模型
85 model: CNN networks
86 val_loader: a Dataloader object with validation data
87 device: evaluate on cpu or gpu device
88 return classification accuracy of the model on val dataset
89 """
90 # evaluate the model
91 model.eval()
92 # context-manager that disabled gradient computation
93 with torch.no_grad():
94 correct = 0
95 total = 0
96
97 for i, (images, targets) in enumerate(val_loader):
98 # device: cpu or gpu
99 images = images.to(device)
100 targets = targets.to(device)
101 outputs = model(images)
102
103 # return the maximum value of each row of the input tensor in the
104 # given dimension dim, the second return vale is the index location
105 # of each maxium value found(argmax)
106 _, predicted = torch.max(outputs.data, dim=1)
107 correct += (predicted == targets).sum().item()
108
109 total += targets.size(0)
110
111 accuracy = correct / total
112 print('Accuracy on Test Set: {:.4f} %'.format(100 * accuracy))
113 return accuracy
114
115
116 def save_model(model, save_path="results/cnn.pth"):
117 '''保存模型'''
118 # save model
119 torch.save(model.state_dict(), save_path)
120
121
122 def show_curve(ys, title):
123 """
124 plot curlve for Loss and Accuacy
125 Args:
126 ys: loss or acc list
127 title: loss or accuracy
128 """
129 x = np.array(range(len(ys)))
130 y = np.array(ys)
131 plt.plot(x, y, c='b')
132 plt.axis()
133 plt.title('{} curve'.format(title))
134 plt.xlabel('epoch')
135 plt.ylabel('{}'.format(title))
136 plt.show()
137
138
139 def fit(model, num_epochs, optimizer, device):
140 """
141 train and evaluate an classifier num_epochs times.
142 We use optimizer and cross entropy loss to train the model.
143 Args:
144 model: CNN network
145 num_epochs: the number of training epochs
146 optimizer: optimize the loss function
147 """
148
149 # loss and optimizer
150 loss_func = nn.CrossEntropyLoss()
151 model.to(device)
152 loss_func.to(device)
153
154 # log train loss and test accuracy
155 losses = []
156 accs = []
157
158 for epoch in range(num_epochs):
159
160 print('Epoch {}/{}:'.format(epoch + 1, num_epochs))
161 # train step
162 loss = train(model, train_loader, loss_func, optimizer, device)
163 losses.append(loss)
164
165 # evaluate step
166 accuracy = evaluate(model, test_loader, device)
167 accs.append(accuracy)
168 time.sleep(1)
169
170
171 # show curve
172 show_curve(losses, "train loss")
173 show_curve(accs, "test accuracy")
174
175
176
177 import numpy as np
178 import torch
179 import torchvision
180 import torchvision.transforms as transforms
181 import matplotlib.pyplot as plt
182 import torch.nn.functional as F
183 import torch
184 from torchvision import datasets, transforms
185
186 # #hyper parameter
187 batch_size = 5
188 num_epochs = 20
189 lr = 0.01
190 num_classes = 6
191 image_size = 32
192
193 path = "datasets/la1ji1fe1nle4ishu4ju4ji22-momodel/dataset-resized"
194 transform = transforms.Compose([
195 transforms.Resize((32,32)),
196 transforms.ToTensor(),
197 transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])
198
199 dataset = datasets.ImageFolder(path, transform=transform)
200
201 print("dataset.classes",dataset.classes)
202 print("dataset.class_to_idx",dataset.class_to_idx)
203 idx_to_class = dict((v, k) for k, v in dataset.class_to_idx.items())
204 print("idx_to_class",idx_to_class)
205 print('len(dataset)', len(dataset))
206
207 """将训练集划分为训练集和验证集"""
208 train_db, val_db = torch.utils.data.random_split(dataset, [2076, 231])
209 print('train:', len(train_db), 'validation:', len(val_db))
210
211 # 训练集
212 train_loader = torch.utils.data.DataLoader(
213 train_db,
214 batch_size=batch_size,
215 shuffle=True,
216 drop_last=False)
217 test_loader = torch.utils.data.DataLoader(
218 val_db,
219 batch_size=batch_size,
220 shuffle=True)
221
222 classes = set(['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash'])
223
224 # declare and define an objet of MyCNN
225 mycnn = MyCNN(image_size, num_classes)
226 print(mycnn)
227 # device = torch.device('cuda:0')
228 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
229
230 optimizer = torch.optim.Adam(mycnn.parameters(), lr=lr)
231
232 # start training on cifar10 dataset
233 fit(mycnn, num_epochs, optimizer, device)
0 Please store your training checkpoints or results here
1 请在此处存储 checkpoints 和结果文件
Binary diff not shown
0 Please store your tensorboard results here
1 请在此处存储 tensorboard 结果
(New empty file)
0 {
1 "cells": [
2 {
3 "cell_type": "code",
4 "execution_count": 1,
5 "metadata": {},
6 "outputs": [
7 {
8 "name": "stdout",
9 "output_type": "stream",
10 "text": [
11 "hell world!\n"
12 ]
13 }
14 ],
15 "source": [
16 "print('hell world!')"
17 ]
18 },
19 {
20 "cell_type": "markdown",
21 "metadata": {},
22 "source": [
23 "### 人工智能\n",
24 "- 机器学习\n",
25 " - 线性回归\n",
26 " - 逻辑回归\n",
27 " - 聚类\n",
28 "- 深度学习\n",
29 " - 人工神经网络\n",
30 " - CNN\n",
31 " - RNN\n",
32 " - TensorFlow"
33 ]
34 },
35 {
36 "cell_type": "markdown",
37 "metadata": {},
38 "source": [
39 "#### 1.3 主要功能\n",
40 "<img src=\"https://imgbed.momodel.cn/20210311160548.png\" width=1000px/>"
41 ]
42 },
43 {
44 "cell_type": "code",
45 "execution_count": null,
46 "metadata": {},
47 "outputs": [],
48 "source": []
49 }
50 ],
51 "metadata": {
52 "kernelspec": {
53 "display_name": "Python 3",
54 "language": "python",
55 "name": "python3"
56 },
57 "language_info": {
58 "codemirror_mode": {
59 "name": "ipython",
60 "version": 3
61 },
62 "file_extension": ".py",
63 "mimetype": "text/x-python",
64 "name": "python",
65 "nbconvert_exporter": "python",
66 "pygments_lexer": "ipython3",
67 "version": "3.7.5"
68 }
69 },
70 "nbformat": 4,
71 "nbformat_minor": 4
72 }
0 ###############################################################################
1 # 重要: 请务必把任务(jobs)中需要保存的文件存放在 results 文件夹内
2 # Important : Please make sure your files are saved to the 'results' folder
3 # in your jobs
4 ###############################################################################
5 import torch
6 import torch.nn as nn
7 import torch.utils.data as Data
8 import torchvision
9 import time
10 import matplotlib.pyplot as plt
11
12
13 class MyCNN(nn.Module):
14 """
15 网络模型
16 """
17 def __init__(self, image_size, num_classes):
18 super(MyCNN, self).__init__()
19 # conv1: Conv2d -> BN -> ReLU -> MaxPool
20 self.conv1 = nn.Sequential(
21 nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1),
22 nn.BatchNorm2d(16),
23 nn.ReLU(),
24 nn.MaxPool2d(kernel_size=2, stride=2),
25 )
26 # conv2: Conv2d -> BN -> ReLU -> MaxPool
27 self.conv2 = nn.Sequential(
28 nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1),
29 nn.BatchNorm2d(32),
30 nn.ReLU(),
31 nn.MaxPool2d(kernel_size=2, stride=2),
32 )
33 # fully connected layer
34 self.fc = nn.Linear(32 * (image_size // 4) * (image_size // 4), num_classes)
35
36
37 def forward(self, x):
38 """
39 input: N * 3 * image_size * image_size
40 output: N * num_classes
41 """
42 x = self.conv1(x)
43 x = self.conv2(x)
44 # view(x.size(0), -1): change tensor size from (N ,H , W) to (N, H*W)
45 x = x.view(x.size(0), -1)
46 output = self.fc(x)
47 return output
48
49
50 def train(model, train_loader, loss_func, optimizer, device):
51 """
52 训练模型
53 train model using loss_fn and optimizer in an epoch.
54 model: CNN networks
55 train_loader: a Dataloader object with training data
56 loss_func: loss function
57 device: train on cpu or gpu device
58 """
59 total_loss = 0
60 # train the model using minibatch
61 for i, (images, targets) in enumerate(train_loader):
62 images = images.to(device)
63 targets = targets.to(device)
64
65 # forward
66 outputs = model(images)
67 _,preds = torch.max(outputs.data,1)
68 loss = loss_func(outputs, targets)
69
70 # backward and optimize
71 optimizer.zero_grad()
72 loss.backward()
73 optimizer.step()
74
75 total_loss += loss.item()
76 if (i + 1) % 100 == 0:
77 print ("Step [{}/{}] Train Loss: {:.4f} Train acc".format(i+1, len(train_loader), loss.item()))
78 save_model(model, save_path="results/cnn.pth")
79 return total_loss / len(train_loader)
80
81
82 def evaluate(model, val_loader, device):
83 """
84 评估模型
85 model: CNN networks
86 val_loader: a Dataloader object with validation data
87 device: evaluate on cpu or gpu device
88 return classification accuracy of the model on val dataset
89 """
90 # evaluate the model
91 model.eval()
92 # context-manager that disabled gradient computation
93 with torch.no_grad():
94 correct = 0
95 total = 0
96
97 for i, (images, targets) in enumerate(val_loader):
98 # device: cpu or gpu
99 images = images.to(device)
100 targets = targets.to(device)
101 outputs = model(images)
102
103 # return the maximum value of each row of the input tensor in the
104 # given dimension dim, the second return vale is the index location
105 # of each maxium value found(argmax)
106 _, predicted = torch.max(outputs.data, dim=1)
107 correct += (predicted == targets).sum().item()
108
109 total += targets.size(0)
110
111 accuracy = correct / total
112 print('Accuracy on Test Set: {:.4f} %'.format(100 * accuracy))
113 return accuracy
114
115
116 def save_model(model, save_path="results/cnn.pth"):
117 '''保存模型'''
118 # save model
119 torch.save(model.state_dict(), save_path)
120
121
122 def show_curve(ys, title):
123 """
124 plot curlve for Loss and Accuacy
125 Args:
126 ys: loss or acc list
127 title: loss or accuracy
128 """
129 x = np.array(range(len(ys)))
130 y = np.array(ys)
131 plt.plot(x, y, c='b')
132 plt.axis()
133 plt.title('{} curve'.format(title))
134 plt.xlabel('epoch')
135 plt.ylabel('{}'.format(title))
136 plt.show()
137
138
139 def fit(model, num_epochs, optimizer, device):
140 """
141 train and evaluate an classifier num_epochs times.
142 We use optimizer and cross entropy loss to train the model.
143 Args:
144 model: CNN network
145 num_epochs: the number of training epochs
146 optimizer: optimize the loss function
147 """
148
149 # loss and optimizer
150 loss_func = nn.CrossEntropyLoss()
151 model.to(device)
152 loss_func.to(device)
153
154 # log train loss and test accuracy
155 losses = []
156 accs = []
157
158 for epoch in range(num_epochs):
159
160 print('Epoch {}/{}:'.format(epoch + 1, num_epochs))
161 # train step
162 loss = train(model, train_loader, loss_func, optimizer, device)
163 losses.append(loss)
164
165 # evaluate step
166 accuracy = evaluate(model, test_loader, device)
167 accs.append(accuracy)
168 time.sleep(1)
169
170
171 # show curve
172 show_curve(losses, "train loss")
173 show_curve(accs, "test accuracy")
174
175
176
177 import numpy as np
178 import torch
179 import torchvision
180 import torchvision.transforms as transforms
181 import matplotlib.pyplot as plt
182 import torch.nn.functional as F
183 import torch
184 from torchvision import datasets, transforms
185
186 # #hyper parameter
187 batch_size = 5
188 num_epochs = 20
189 lr = 0.01
190 num_classes = 6
191 image_size = 32
192
193 path = "datasets/la1ji1fe1nle4ishu4ju4ji22-momodel/dataset-resized"
194 transform = transforms.Compose([
195 transforms.Resize((32,32)),
196 transforms.ToTensor(),
197 transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])
198
199 dataset = datasets.ImageFolder(path, transform=transform)
200
201 print("dataset.classes",dataset.classes)
202 print("dataset.class_to_idx",dataset.class_to_idx)
203 idx_to_class = dict((v, k) for k, v in dataset.class_to_idx.items())
204 print("idx_to_class",idx_to_class)
205 print('len(dataset)', len(dataset))
206
207 """将训练集划分为训练集和验证集"""
208 train_db, val_db = torch.utils.data.random_split(dataset, [2076, 231])
209 print('train:', len(train_db), 'validation:', len(val_db))
210
211 # 训练集
212 train_loader = torch.utils.data.DataLoader(
213 train_db,
214 batch_size=batch_size,
215 shuffle=True,
216 drop_last=False)
217 test_loader = torch.utils.data.DataLoader(
218 val_db,
219 batch_size=batch_size,
220 shuffle=True)
221
222 classes = set(['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash'])
223
224 # declare and define an objet of MyCNN
225 mycnn = MyCNN(image_size, num_classes)
226 print(mycnn)
227 # device = torch.device('cuda:0')
228 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
229
230 optimizer = torch.optim.Adam(mycnn.parameters(), lr=lr)
231
232 # start training on cifar10 dataset
233 fit(mycnn, num_epochs, optimizer, device)
Binary diff not shown
(New empty file)
0 import ipywidgets as widgets
1 import sys
2 from IPython.display import display
3 from IPython.display import clear_output
4
5 out = widgets.Output()
6
7
8 def create_multipleChoice_widget(description, options, correct_answer):
9 if correct_answer not in options:
10 options.append(correct_answer)
11
12 correct_answer_index = options.index(correct_answer)
13
14 radio_options = [(words, i) for i, words in enumerate(options)]
15 alternativ = widgets.RadioButtons(
16 options = radio_options,
17 description = '',
18 disabled = False
19
20 )
21
22 description_out = widgets.Output()
23
24 with description_out:
25 print(description)
26
27 feedback_out = widgets.Output()
28
29 def check_selection(b):
30 a = int(alternativ.value)
31 if a==correct_answer_index:
32 s = '\x1b[6;30;42m' + "回答正确." + '\x1b[0m' +"\n" #green color
33 else:
34 s = '\x1b[5;30;41m' + "回答错误. " + '\x1b[0m' +"\n" #red color
35 with feedback_out:
36 clear_output()
37 print(s)
38 return
39
40 check = widgets.Button(description="提交")
41 check.on_click(check_selection)
42
43
44 return widgets.VBox([description_out, alternativ, check, feedback_out])
45
46 def question1():
47 return create_multipleChoice_widget(
48 "在观察了这三个表达的图像后,你认为假设函数 ℎ 在线性回归模型中是什么样的呢?",
49 ["𝑦=𝑏𝑥+𝑐", "𝑦=𝑏𝑥", "𝑦=𝑎𝑥^2+𝑏𝑥+𝑐", ],
50 "𝑦=𝑏𝑥+𝑐")
51
52 def question2():
53 return create_multipleChoice_widget(
54 "",
55 ["0, 0", "0, 1", "1, 0", "1, 1"],
56 "1, 0")
57
58