-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
747 lines (508 loc) · 44.4 KB
/
index.html
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
<link rel="mask-icon" href="/images/logo.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
<script id="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
</script>
<meta property="og:type" content="website">
<meta property="og:title" content="花落闲谈">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="花落闲谈">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="chengc">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="http://yoursite.com/">
<script id="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : true,
isPost : false
};
</script>
<title>花落闲谈</title>
<noscript>
<style>
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-header { opacity: initial; }
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
</head>
<body itemscope itemtype="http://schema.org/WebPage">
<div class="container use-motion">
<div class="headband"></div>
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="切换导航栏">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
</div>
<div class="site-meta">
<div>
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">花落闲谈</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
</div>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>
</li>
</ul>
</nav>
</div>
</header>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
<main class="main">
<div class="main-inner">
<div class="content-wrap">
<div class="content">
<div class="posts-expand">
<article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/15/Ubuntu%E9%87%8D%E5%90%AF%E5%90%8E%E6%89%BE%E4%B8%8D%E5%88%B0NVIDIA-GPU%E9%A9%B1%E5%8A%A8/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/blog-logo.jpg">
<meta itemprop="name" content="chengc">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="花落闲谈">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2020/03/15/Ubuntu%E9%87%8D%E5%90%AF%E5%90%8E%E6%89%BE%E4%B8%8D%E5%88%B0NVIDIA-GPU%E9%A9%B1%E5%8A%A8/" class="post-title-link" itemprop="url">Ubuntu重启后找不到NVIDIA-GPU驱动</a>
</h1>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2020-03-15 01:32:51 / 修改时间:02:05:16" itemprop="dateCreated datePublished" datetime="2020-03-15T01:32:51+08:00">2020-03-15</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/%E7%B3%BB%E7%BB%9F%E7%BB%B4%E6%8A%A4/" itemprop="url" rel="index"><span itemprop="name">系统维护</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>最近一台机器(环境为:Ubuntu+NVIDIA-384.130)重启后发生了找不到GPU驱动的问题:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.</span><br></pre></td></tr></table></figure>
<p>这个问题的原因一般是Ubuntu的内核版本更新了,而显卡驱动是在低版本的内核时安装的,因此发生了不兼容的问题。以往的解决方法是修改Ubuntu默认开机启动的内核版本:需要找到之前使用的内核版本(查看系统已安装内核版本时发现有好几个,也忘记之前安装驱动时内核版本是哪个),并修改grub开机配置,之后便是删除无用内核并禁止内核更新(记得之前已做过这个步骤,但这次内核还是更新了?)</p>
<p>鉴于上述方法过于复杂,这次采用新的方法:基于新的内核重新生成GPU的驱动模块。</p>
<ol>
<li><p>安装DKMS<br>DKMS全称是Dynamic Kernel Module Support,它可以帮我们维护内核外的驱动程序,在内核版本变动之后可以自动重新生成新的模块。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install dkms</span><br></pre></td></tr></table></figure>
</li>
<li><p>查看安装的NVIDIA-GPU驱动版本</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ls /usr/src</span><br></pre></td></tr></table></figure>
<img src="/images/ls_usr_src.png" width="50%" height="50%">
</li>
<li><p>重新生成驱动模块</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo dkms install -m nvidia -v 384.130</span><br></pre></td></tr></table></figure>
</li>
<li><p>检验</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nvidia-smi</span><br></pre></td></tr></table></figure>
</li>
<li><p>重新设置内核禁止更新</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">uname -a # 查看正在使用的内核,e.g. linux-image-4.15.0-88-generic</span><br><span class="line">sudo apt-mark hold linux-image-4.15.0-88-generic</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>若成功,可以看到显卡信息。</p>
<hr>
<p><strong>插曲</strong></p>
<p>在安装dkms时出现了两个小问题:</p>
<p>1、当前源中找不到相应的安装包</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">1)使用 sudo vim /etc/apt/sources.list 修改镜像源</span><br><span class="line">2)然后执行 sudo apt-get update 更新</span><br><span class="line"></span><br><span class="line">## 阿里源</span><br><span class="line">deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse</span><br><span class="line">deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse</span><br><span class="line">deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse</span><br><span class="line">deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse</span><br><span class="line">deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse</span><br><span class="line">deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse</span><br><span class="line">deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse</span><br><span class="line">deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse</span><br><span class="line">deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse</span><br><span class="line">deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse</span><br></pre></td></tr></table></figure>
<p>2、该死的samba服务报错信息。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">dpkg: error processing package samba (--configure):</span><br><span class="line">dependency problems - leaving unconfigured</span><br><span class="line">Errors were encountered while processing:</span><br><span class="line">samba-common</span><br><span class="line">samba-common-bin</span><br><span class="line">samba</span><br><span class="line">E: Sub-process /usr/bin/dpkg returned an error code (1)</span><br><span class="line"></span><br><span class="line">## 解决方案:</span><br><span class="line">$ sudo mv /var/lib/dpkg/info /var/lib/dpkg/info_old //现将info文件夹更名</span><br><span class="line">$ sudo mkdir /var/lib/dpkg/info //再新建一个新的info文件夹</span><br><span class="line">$ sudo apt-get update</span><br><span class="line">$ sudo apt-get -f install</span><br><span class="line">$ sudo mv /var/lib/dpkg/info/* /var/lib/dpkg/info_old</span><br><span class="line">//执行完上一步操作后会在新的info文件夹下生成一些文件,现将这些文件全部移到info_old文件夹下</span><br><span class="line">$ sudo rm -rf /var/lib/dpkg/info //把自己新建的info文件夹删掉</span><br><span class="line">$ sudo mv /var/lib/dpkg/info_old /var/lib/dpkg/info //把以前的info文件夹重新改回名字</span><br></pre></td></tr></table></figure>
<hr>
<p><strong><em>Ref:</em></strong></p>
<p>CASE SOLVED:NVIDIA-SMI has failed because it couldnt communicate with the NVIDIA driverr_运维_Felaim的博客-CSDN博客<br><a href="https://blog.csdn.net/Felaim/article/details/100516282" target="_blank" rel="noopener">https://blog.csdn.net/Felaim/article/details/100516282</a></p>
<p>NVIDIA-SMI has failed because it couldnt communicate with the NVIDIA driver问题排查_运维_u014447845的博客-CSDN博客<br><a href="https://blog.csdn.net/u014447845/article/details/103012088" target="_blank" rel="noopener">https://blog.csdn.net/u014447845/article/details/103012088</a></p>
<p>ubuntu 禁止内核更新 - 天道酬勤、 - 博客园<br><a href="https://www.cnblogs.com/zxj9487/p/11386227.html" target="_blank" rel="noopener">https://www.cnblogs.com/zxj9487/p/11386227.html</a></p>
<p>ubuntu映射网络驱动器失败,以及samba服务 - 简书<br><a href="https://www.jianshu.com/p/89b7831181ab" target="_blank" rel="noopener">https://www.jianshu.com/p/89b7831181ab</a></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/12/Python%E5%8F%91%E9%80%81%E7%9F%AD%E4%BF%A1/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/blog-logo.jpg">
<meta itemprop="name" content="chengc">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="花落闲谈">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2020/03/12/Python%E5%8F%91%E9%80%81%E7%9F%AD%E4%BF%A1/" class="post-title-link" itemprop="url">Python发送短信</a>
</h1>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2020-03-12 18:54:00 / 修改时间:19:12:06" itemprop="dateCreated datePublished" datetime="2020-03-12T18:54:00+08:00">2020-03-12</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Python/" itemprop="url" rel="index"><span itemprop="name">Python</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>在进行深度学习炼金时,经常需要花费很长一段时间等待结果,因此想变主动为被动,让程序在运行结束时将结果通过短信主动发送到我的手机上,省得我每次都要通过ssh连接服务器进行查看。</p>
<p>搜索了一下教程,找到两个心仪的解决方案:Twilio、腾讯云短信,基本套路是通过调用Python接口进行短信转发。Twilio提供500条免费短信,腾讯云短信则提供100条,不过腾讯云在1万条内的价格是5分钱一条,尚可接受。目前的解决方案是先用完Twilio的500条后再转战腾讯云。</p>
<ul>
<li><p>Twilio</p>
<ol>
<li><p>注册</p>
<p>网址为:<a href="https://www.twilio.com/" target="_blank" rel="noopener">https://www.twilio.com</a>,教程见:<a href="https://www.cnblogs.com/pythoncircle/p/11790463.html" target="_blank" rel="noopener">https://www.cnblogs.com/pythoncircle/p/11790463.html</a></p>
</li>
<li><p>API调用模板(简单)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Download the helper library from https://www.twilio.com/docs/python/install</span></span><br><span class="line"><span class="keyword">from</span> twilio.rest <span class="keyword">import</span> Client</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># Your Account Sid and Auth Token from twilio.com/console</span></span><br><span class="line"><span class="comment"># DANGER! This is insecure. See http://twil.io/secure</span></span><br><span class="line">account_sid = <span class="string">'your_acco_sid'</span></span><br><span class="line">auth_token = <span class="string">'your_auth_token'</span></span><br><span class="line">client = Client(account_sid, auth_token)</span><br><span class="line"></span><br><span class="line">message = client.messages \</span><br><span class="line"> .create(</span><br><span class="line"> body=<span class="string">"Join Earth's mightiest heroes. Like Kevin Bacon."</span>,</span><br><span class="line"> from_=<span class="string">'+150XXXXXXXXX'</span>,</span><br><span class="line"> to=<span class="string">'+86XXXXXXXXXXX'</span></span><br><span class="line"> )</span><br><span class="line"></span><br><span class="line">print(message.sid)</span><br></pre></td></tr></table></figure>
<p>需替换自己的account_sid,auth_token,获得的虚拟号码(from_),发送的号码(to),信息(body),运行前安装twilio:<code>pip install twilio</code>。</p>
</li>
<li><p>实际上没有500条,因为原始赠送金额是15美元,获得虚拟号码及部署项目时会用掉1.056美元,不过可忽略不计,每条短信价格是0.28美元。</p>
</li>
</ol>
</li>
<li><p>腾讯云短信</p>
<ul>
<li><a href="https://cloud.tencent.com/document/product/382" target="_blank" rel="noopener">https://cloud.tencent.com/document/product/382</a></li>
<li>待测试</li>
</ul>
</li>
</ul>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/09/Tensorboard%E7%9A%84smooth%E6%95%88%E6%9E%9C/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/blog-logo.jpg">
<meta itemprop="name" content="chengc">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="花落闲谈">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2020/03/09/Tensorboard%E7%9A%84smooth%E6%95%88%E6%9E%9C/" class="post-title-link" itemprop="url">Tensorboard的smooth效果</a>
</h1>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2020-03-09 22:56:04 / 修改时间:23:22:29" itemprop="dateCreated datePublished" datetime="2020-03-09T22:56:04+08:00">2020-03-09</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Tensorflow%E4%BD%BF%E7%94%A8/" itemprop="url" rel="index"><span itemprop="name">Tensorflow使用</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>在TensorFlow的可视化工具Tensorboard中,有一个相当好用的选项:设置曲线的smooth参数。我们可以通过增大这个参数的设置,使得原本波动起伏很大的曲线变得平滑,从而得到更加清晰的变化趋势。</p>
<p>虽然Tensorboard提供了数据下载的接口(csv、json格式),但是只针对于原始数据,因此在进行绘图时有必要实现跟Tensorboard一样的平滑设置。参照Tensorboard中使用的smooth函数,编写数据处理脚本如下:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">smooth</span><span class="params">(csv_path, weight=<span class="number">0.85</span>)</span>:</span></span><br><span class="line"> data = pd.read_csv(filepath_or_buffer=csv_path, header=<span class="number">0</span>, names=[<span class="string">'Step'</span>,<span class="string">'Value'</span>], dtype={<span class="string">'Step'</span>:np.int, <span class="string">'Value'</span>:np.float})</span><br><span class="line"> scalar = data[<span class="string">'Value'</span>].values</span><br><span class="line"> last = scalar[<span class="number">0</span>]</span><br><span class="line"> smoothed = []</span><br><span class="line"> <span class="keyword">for</span> point <span class="keyword">in</span> scalar:</span><br><span class="line"> smoothed_val = last * weight + (<span class="number">1</span> - weight) * point</span><br><span class="line"> smoothed.append(smoothed_val)</span><br><span class="line"> last = smoothed_val</span><br><span class="line"></span><br><span class="line"> save = pd.DataFrame({<span class="string">'Step'</span>:data[<span class="string">'Step'</span>].values, <span class="string">'Value'</span>:smoothed})</span><br><span class="line"> save.to_csv(<span class="string">'smooth_'</span> + csv_path)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">smooth_and_plot</span><span class="params">(csv_path, weight=<span class="number">0.85</span>)</span>:</span></span><br><span class="line"> data = pd.read_csv(filepath_or_buffer=csv_path, header=<span class="number">0</span>, names=[<span class="string">'Step'</span>,<span class="string">'Value'</span>], dtype={<span class="string">'Step'</span>:np.int, <span class="string">'Value'</span>:np.float})</span><br><span class="line"> scalar = data[<span class="string">'Value'</span>].values</span><br><span class="line"> last = scalar[<span class="number">0</span>]</span><br><span class="line"> print(type(scalar))</span><br><span class="line"> smoothed = []</span><br><span class="line"> <span class="keyword">for</span> point <span class="keyword">in</span> scalar:</span><br><span class="line"> smoothed_val = last * weight + (<span class="number">1</span> - weight) * point</span><br><span class="line"> smoothed.append(smoothed_val)</span><br><span class="line"> last = smoothed_val</span><br><span class="line"></span><br><span class="line"> <span class="comment"># save = pd.DataFrame({'Step':data['Step'].values, 'Value':smoothed})</span></span><br><span class="line"> <span class="comment"># save.to_csv('smooth_' + csv_path)</span></span><br><span class="line"></span><br><span class="line"> steps = data[<span class="string">'Step'</span>].values</span><br><span class="line"> steps = steps.tolist()</span><br><span class="line"> origin = scalar.tolist()</span><br><span class="line"></span><br><span class="line"> fig = plt.figure(<span class="number">1</span>)</span><br><span class="line"> plt.plot(steps, origin, label=<span class="string">'origin'</span>)</span><br><span class="line"> plt.plot(steps, smoothed, label=<span class="string">'smoothed'</span>)</span><br><span class="line"> <span class="comment"># plt.ylim(0, 220) # Tensorboard中会滤除过大的数据,可通过设置坐标最值来实现</span></span><br><span class="line"> plt.legend()</span><br><span class="line"> plt.show()</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__==<span class="string">'__main__'</span>:</span><br><span class="line"> <span class="comment"># smooth('total_loss.csv')</span></span><br><span class="line"> smooth_and_plot(<span class="string">'total_loss.csv'</span>)</span><br></pre></td></tr></table></figure>
<p>可视化效果如下图:</p>
<img src="/images/smooth_test.png" width="50%" height="50%">
<p>扩展:</p>
<p>上述smooth函数旨在构建一个类似于IIR滤波器的结构以滤除高频部分保留低频部分,即让数据变化更加平缓。</p>
<p><em>Ref:</em></p>
<p>Tensorboard 下Smooth功能探究<br><a href="https://dingguanglei.com/tensorboard-xia-smoothgong-neng-tan-jiu/" target="_blank" rel="noopener">https://dingguanglei.com/tensorboard-xia-smoothgong-neng-tan-jiu/</a></p>
<p>tensorboard 平滑损失曲线代码_人工智能_Charel_CHEN的博客-CSDN博客<br><a href="https://blog.csdn.net/Charel_CHEN/article/details/80364841" target="_blank" rel="noopener">https://blog.csdn.net/Charel_CHEN/article/details/80364841</a></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block home" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/08/Pytorch%E7%9A%84%E5%A4%8D%E7%8E%B0%E6%80%A7/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/blog-logo.jpg">
<meta itemprop="name" content="chengc">
<meta itemprop="description" content="">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="花落闲谈">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2020/03/08/Pytorch%E7%9A%84%E5%A4%8D%E7%8E%B0%E6%80%A7/" class="post-title-link" itemprop="url">Pytorch的复现性</a>
</h1>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2020-03-08 01:39:11" itemprop="dateCreated datePublished" datetime="2020-03-08T01:39:11+08:00">2020-03-08</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2020-03-09 23:22:49" itemprop="dateModified" datetime="2020-03-09T23:22:49+08:00">2020-03-09</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Pytorch%E4%BD%BF%E7%94%A8/" itemprop="url" rel="index"><span itemprop="name">Pytorch使用</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>最近在使用YOLOv3模型来训练KITTI数据集,遇到一个不可避免的问题——可复现性。由于所参考的代码(<a href="https://github.com/DeNA/PyTorch_YOLOv3" target="_blank" rel="noopener">PyTorch_YLOv3</a>)没有做相关的设置,因此也费了些时间去了解和实践。</p>
<p>官方的指导文件见:<a href="https://pytorch.org/docs/master/notes/randomness.html" target="_blank" rel="noopener">https://pytorch.org/docs/master/notes/randomness.html</a> ,具体而言,需要考虑以下几个方面:</p>
<ol>
<li><p>随机种子的设定</p>
<ul>
<li><p>Pytorch的种子设置(CPU&GPU)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">torch.manual_seed(seed)</span><br><span class="line">torch.cuda.manual_seed_all(seed) <span class="comment"># if you are using multi-GPU</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>cuDNN的优化设置</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">torch.backends.cudnn.enabled = <span class="literal">False</span></span><br><span class="line">torch.backends.cudnn.benchmark = <span class="literal">False</span></span><br><span class="line">torch.backends.cudnn.deterministic = <span class="literal">True</span></span><br></pre></td></tr></table></figure>
<p>cuDNN使用非确定性算法,能够自动寻找最适合当前配置的高效算法,来达到优化运行效率的问题,可以使用<code>torch.backends.cudnn.enabled = False</code>来进行禁用。当然,禁用后会影响一定的效率。</p>
</li>
<li><p>Numpy的种子设置</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">np.random.seed(seed)</span><br></pre></td></tr></table></figure>
<p>对于目标检测等任务来说,经常需要进行数据增强,如随机翻转、多尺度训练等,可以通过设置Numpy的种子来去除非确定性。此外,Pytorch的底层实现中某些模块也调用了Numpy的随机性操作,所以不管是否进行了数据增强操作,都需要设置Numpy的种子。</p>
</li>
<li><p>DataLoader的多线程设置</p>
<p>当DataLoader采用多线程操作时(num_workers > 1),也需要进行随机种子的设置。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">_init_fn</span><span class="params">()</span>:</span></span><br><span class="line"> np.random.seed(<span class="number">0</span>)</span><br><span class="line">train_loader = DataLoader(data_sets, batch_size=<span class="number">8</span>, shuffle=<span class="literal">True</span>, </span><br><span class="line"> num_workers=<span class="number">8</span>, worker_init_fn=_init_fn)</span><br></pre></td></tr></table></figure>
</li>
<li><p>random模块设置</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">random.seed(seed)</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li><p>Pytorch底层实现代码对于非确定性的引入</p>
<p>在进行了上述种子设定后,代码基本上具备了可重复性,然而目前Pytorch的某些底层实现仍然存在着不确定性,暂时无法得到解决。比如,Pytorch的上采样操作在反向求导时会存在随机性;API所述,PyTorch使用的CUDA实现中,有一部分是原子操作,尤其是<code>atomicAdd</code>,使用这个操作就代表数据不能够并行处理,需要串行处理,使用到<code>atomicAdd</code>之后就会按照不确定的并行加法顺序执行,从而引入了不确定因素。PyTorch中使用到的<code>atomicAdd</code>的方法:</p>
<p><strong>前向传播时:</strong></p>
<ul>
<li>torch.Tensor.index_add_()_</li>
<li><em>torch.Tensor.scatter_add</em>()</li>
<li>torch.bincount()</li>
</ul>
<p><strong>反向传播时:</strong></p>
<ul>
<li>torch.nn.functional.embedding_bag()</li>
<li>torch.nn.functional.ctc_loss()</li>
<li>其他pooling,padding, sampling操作</li>
</ul>
</li>
</ol>
<p>这次在进行YOLOv3(Pytorch版)的训练时,采用的种子设定脚本为:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">setup_seed</span><span class="params">(seed=<span class="number">202003</span>)</span>:</span></span><br><span class="line"> random.seed(seed)</span><br><span class="line"> np.random.seed(seed)</span><br><span class="line"> <span class="comment"># if you are suing GPU</span></span><br><span class="line"> torch.manual_seed(seed)</span><br><span class="line"> torch.cuda.manual_seed_all(seed) <span class="comment"># if you are using multi-GPU</span></span><br><span class="line"> <span class="comment"># for cudnn</span></span><br><span class="line"> torch.backends.cudnn.enabled = <span class="literal">False</span></span><br><span class="line"> torch.backends.cudnn.benchmark = <span class="literal">False</span></span><br><span class="line"> torch.backends.cudnn.deterministic = <span class="literal">True</span></span><br><span class="line"> <span class="comment"># for hash</span></span><br><span class="line"> os.environ[<span class="string">'PYTHONHASHSEED'</span>] = str(seed)</span><br></pre></td></tr></table></figure>
<p>最后一行主要是为了禁止hash随机化,使得实验可复现。但是因为YOLOv3中含有上采样层,所以在进行实验时发现,在训练前期结果可以保持一致性,但随着epoch的增大,也会产生一定的不确定性,取两组训练过程的Loss可视化如下:</p>
<img src="/images/loss_vis_1.png" width="50%" height="50%">
<center>两组Loss值对比</center>
<img src="/images/loss_vis_2.png" width="50%" height="50%">
<center>两组Loss差值对比</center>
可见随着训练的进行,结果难以复现,但最终mAP差异保持在1%左右即可。
<hr>
<p><em>Ref:</em></p>
<p>PyTorch中模型的可复现性 - 知乎<br><a href="https://zhuanlan.zhihu.com/p/109166845" target="_blank" rel="noopener">https://zhuanlan.zhihu.com/p/109166845</a></p>
<p>Deterministic Pytorch: pytorch如何保证可重复性 - 知乎<br><a href="https://zhuanlan.zhihu.com/p/81039955" target="_blank" rel="noopener">https://zhuanlan.zhihu.com/p/81039955</a></p>
<p>Set All Seed But Result Is Non Deterministic - PyTorch Forums<br><a href="https://discuss.pytorch.org/t/set-all-seed-but-result-is-non-deterministic/27494" target="_blank" rel="noopener">https://discuss.pytorch.org/t/set-all-seed-but-result-is-non-deterministic/27494</a></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</div>
</div>
<script>
window.addEventListener('tabs:register', () => {
let { activeClass } = CONFIG.comments;
if (CONFIG.comments.storage) {
activeClass = localStorage.getItem('comments_active') || activeClass;
}
if (activeClass) {
let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
if (activeTab) {
activeTab.click();
}
}
});
if (CONFIG.comments.storage) {
window.addEventListener('tabs:click', event => {
if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
let commentClass = event.target.classList[1];
localStorage.setItem('comments_active', commentClass);
});
}
</script>
</div>
<div class="toggle sidebar-toggle">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
<aside class="sidebar">
<div class="sidebar-inner">
<ul class="sidebar-nav motion-element">
<li class="sidebar-nav-toc">
文章目录
</li>
<li class="sidebar-nav-overview">
站点概览
</li>
</ul>
<!--noindex-->
<div class="post-toc-wrap sidebar-panel">
</div>
<!--/noindex-->
<div class="site-overview-wrap sidebar-panel">
<div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image" alt="chengc"
src="/images/blog-logo.jpg">
<p class="site-author-name" itemprop="name">chengc</p>
<div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
<nav class="site-state">
<div class="site-state-item site-state-posts">
<a href="/archives/">
<span class="site-state-item-count">4</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
<div class="site-state-item site-state-categories">
<a href="/categories/">
<span class="site-state-item-count">4</span>
<span class="site-state-item-name">分类</span></a>
</div>
<div class="site-state-item site-state-tags">
<a href="/tags/">
<span class="site-state-item-count">6</span>
<span class="site-state-item-name">标签</span></a>
</div>
</nav>
</div>
<div class="links-of-author motion-element">
<span class="links-of-author-item">
<a href="https://github.com/chengcchn" title="GitHub → https://github.com/chengcchn" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
</span>
<span class="links-of-author-item">
<a href="mailto:[email protected]" title="E-Mail → mailto:[email protected]" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
</span>
</div>
</div>
</div>
</aside>
<div id="sidebar-dimmer"></div>
</div>
</main>
<footer class="footer">
<div class="footer-inner">
<div class="copyright">
©
<span itemprop="copyrightYear">2020</span>
<span class="with-love">
<i class="fa fa-user"></i>
</span>
<span class="author" itemprop="copyrightHolder">chengc</span>
</div>
<div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.2.0
</div>
<span class="post-meta-divider">|</span>
<div class="theme-info">主题 – <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.7.2
</div>
</div>
</footer>
</div>
<script src="/lib/anime.min.js"></script>
<script src="/lib/velocity/velocity.min.js"></script>
<script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script>
<script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>
</body>
</html>