-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2596 lines (1451 loc) · 115 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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html class="theme-next pisces use-motion" lang="en">
<head>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<script>
(function(){
if (''){
if (prompt('Please input reading password','') !== ''){
alert('error password.');
history.back();
}
}
})();
</script>
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />
<meta name="keywords" content="blog" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />
<meta property="og:type" content="website">
<meta property="og:title" content="Klpek's Note Library">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Klpek's Note Library">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Klpek's Note Library">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Pisces',
sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
fancybox: true,
motion: true,
duoshuo: {
userId: '0',
author: 'Author'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
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"}
}
};
</script>
<link rel="canonical" href="http://yoursite.com/"/>
<title> Klpek's Note Library </title><!-- hexo-inject:begin --><!-- hexo-inject:end -->
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="en">
<!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="container sidebar-position-left
page-home
">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">Klpek's Note Library</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle"></p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
Home
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories" rel="section">
<i class="menu-item-icon fa fa-fw fa-th"></i> <br />
Categories
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
Archives
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
Tags
</a>
</li>
<li class="menu-item menu-item-search">
<a href="javascript:;" class="popup-trigger">
<i class="menu-item-icon fa fa-search fa-fw"></i> <br />
Search
</a>
</li>
</ul>
<div class="site-search">
<div class="popup search-popup local-search-popup">
<div class="local-search-header clearfix">
<span class="search-icon">
<i class="fa fa-search"></i>
</span>
<span class="popup-btn-close">
<i class="fa fa-times-circle"></i>
</span>
<div class="local-search-input-wrapper">
<input autocapitalize="off" autocomplete="off" autocorrect="off"
placeholder="Searching..." spellcheck="false"
type="text" id="local-search-input">
</div>
</div>
<div id="local-search-result"></div>
</div>
</div>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/09/07/evaluation-measure/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Klpek">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Klpek's Note Library">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/09/07/evaluation-measure/" itemprop="url">
evaluation-measure
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2017-09-07T11:26:27+08:00">
2017-09-07
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2017/09/07/evaluation-measure/#comments" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/09/07/evaluation-measure/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="分类问题"><a href="#分类问题" class="headerlink" title="分类问题"></a>分类问题</h3><h4 id="precision-精确率"><a href="#precision-精确率" class="headerlink" title="precision(精确率)"></a>precision(精确率)</h4><script type="math/tex; mode=display">P = \frac{TP}{TP+FP} \tag{1}</script><h4 id="recall-召回率"><a href="#recall-召回率" class="headerlink" title="recall(召回率)"></a>recall(召回率)</h4><script type="math/tex; mode=display">R = \frac{TP}{TP+FN} \tag{2}</script><h4 id="F-1"><a href="#F-1" class="headerlink" title="$F_1$"></a>$F_1$</h4><p>精确率和召回率的调和均值,</p>
<script type="math/tex; mode=display">
\begin{align*}
\frac{2}{F_1} & = \frac{1}{P} + \frac{1}{R}\\
F_1 & = \frac{2TP}{2TP + FP + FN} \tag{3}
\end{align*}</script><p>精确率和准确率都高的情况下,$F_1$值也会高。</p>
<h3 id="later"><a href="#later" class="headerlink" title="later"></a>later</h3><p><a href="http://charleshm.github.io/2016/03/Model-Performance/" target="_blank" rel="external">http://charleshm.github.io/2016/03/Model-Performance/</a></p>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/09/01/Alignment_Quality_Evaluation/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Klpek">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Klpek's Note Library">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/09/01/Alignment_Quality_Evaluation/" itemprop="url">
Untitled
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2017-09-01T17:21:36+08:00">
2017-09-01
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2017/09/01/Alignment_Quality_Evaluation/#comments" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/09/01/Alignment_Quality_Evaluation/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="sequences-alignment-tools"><a href="#sequences-alignment-tools" class="headerlink" title="sequences alignment tools"></a>sequences alignment tools</h2><h3 id="BLAST"><a href="#BLAST" class="headerlink" title="BLAST"></a>BLAST</h3><p>sequence-sequence alignment</p>
<h3 id="PSI-BLAST"><a href="#PSI-BLAST" class="headerlink" title="PSI-BLAST"></a>PSI-BLAST</h3><p>profile-sequence alignment</p>
<h3 id="HMMER"><a href="#HMMER" class="headerlink" title="HMMER"></a>HMMER</h3><p>HMM-sequence alignment</p>
<h3 id="PROF-SIM-and-COMPASS"><a href="#PROF-SIM-and-COMPASS" class="headerlink" title="PROF_SIM and COMPASS"></a>PROF_SIM and COMPASS</h3><p>profile-profile alignment</p>
<h3 id="HHSearch"><a href="#HHSearch" class="headerlink" title="HHSearch"></a>HHSearch</h3><h5 id="HHSearch2"><a href="#HHSearch2" class="headerlink" title="HHSearch2"></a>HHSearch2</h5><h2 id="Alignment-Quality-Evaluation"><a href="#Alignment-Quality-Evaluation" class="headerlink" title="Alignment Quality Evaluation"></a>Alignment Quality Evaluation</h2><h3 id="plain-MaxSub-score"><a href="#plain-MaxSub-score" class="headerlink" title="plain MaxSub score"></a>plain MaxSub score</h3><script type="math/tex; mode=display">S_{Dev}=N_{correct}/min(L_q,L_p)</script><h3 id="Modeler’s-score"><a href="#Modeler’s-score" class="headerlink" title="Modeler’s score"></a>Modeler’s score</h3><script type="math/tex; mode=display">S_{Mod}=N_{correct}/L_{ali}</script><h3 id="Balanced-Score"><a href="#Balanced-Score" class="headerlink" title="Balanced Score"></a>Balanced Score</h3><script type="math/tex; mode=display">S_{balanced}=(S_{Dev}+S_{Mod})</script>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/08/27/tf_train/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Klpek">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Klpek's Note Library">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/08/27/tf_train/" itemprop="url">
Untitled
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2017-08-27T16:07:06+08:00">
2017-08-27
</time>
</span>
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2017/08/27/tf_train/#comments" itemprop="discussionUrl">
<span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/08/27/tf_train/" itemprop="commentCount"></span>
</a>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>MonitoredSession<br><a href="https://www.tensorflow.org/api_docs/python/tf/train/MonitoredSession" target="_blank" rel="external">https://www.tensorflow.org/api_docs/python/tf/train/MonitoredSession</a><br>hook</p>
<h3 id="Experiment"><a href="#Experiment" class="headerlink" title="Experiment"></a>Experiment</h3><p><a href="https://www.tensorflow.org/api_docs/python/tf/contrib/learn/Experiment" target="_blank" rel="external">https://www.tensorflow.org/api_docs/python/tf/contrib/learn/Experiment</a><br>Experiment is a class containing all information needed to train a model.<br>by passing an Estimator and inputs for training and evaluation, an Experiment instance knows how to invoke training and eval loops in a sensible fashion for distributed training.</p>
<h4 id="method"><a href="#method" class="headerlink" title="method"></a>method</h4><h5 id="Constructor"><a href="#Constructor" class="headerlink" title="Constructor"></a>Constructor</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">__init__(</div><div class="line"> estimator,</div><div class="line"> train_input_fn,</div><div class="line"> eval_input_fn,</div><div class="line"> eval_metrics=<span class="keyword">None</span>,</div><div class="line"> train_steps=<span class="keyword">None</span>,</div><div class="line"> eval_steps=<span class="number">100</span>,</div><div class="line"> train_monitors=<span class="keyword">None</span>,</div><div class="line"> eval_hooks=<span class="keyword">None</span>,</div><div class="line"> local_eval_frequency=<span class="keyword">None</span>,</div><div class="line"> eval_delay_secs=<span class="number">120</span>,</div><div class="line"> continuous_eval_throttle_secs=<span class="number">60</span>,</div><div class="line"> min_eval_frequency=<span class="keyword">None</span>,</div><div class="line"> delay_workers_by_global_step=<span class="keyword">False</span>,</div><div class="line"> export_strategies=<span class="keyword">None</span>,</div><div class="line"> train_steps_per_iteration=<span class="keyword">None</span></div><div class="line">)</div></pre></td></tr></table></figure>
<p>Creates an Experiment instance.<br>None of the functions passed to this constructor are executed at construction time.<br>They are stored and used when a method is executed which requires it.</p>
<ul>
<li>estimator<ul>
<li>Object implementing Estimator interface,</li>
</ul>
</li>
<li>train_input_fn<ul>
<li>function, returns features and labels for training.</li>
</ul>
</li>
<li>eval_input_fn<ul>
<li>function, returns features and labels for evaluation.</li>
<li>If eval_steps is None, this should be configured only to produce for a finite number of batches (generally, 1 epoch over the evaluation data).</li>
</ul>
</li>
<li>eval_metrics<ul>
<li>dict of string, metric function.</li>
<li>If None, default set is used. This should be None if the estimator is ${tf.estimator.Estimator}$. If metrics are provided they will be appended to the default set.</li>
</ul>
</li>
<li>train_steps<ul>
<li>Perform this many steps of training. None, the default, means train forever.</li>
</ul>
</li>
<li>eval_steps<ul>
<li>evaluate runs until input is exhausted (or another exception is raised), or for eval_steps steps, if specified.</li>
</ul>
</li>
<li>train_monitors<ul>
<li>A list of monitors to pass to the Estimator’s fit function.</li>
</ul>
</li>
<li>eval_hooks<ul>
<li>A list of SessionRunHook hooks to pass to the Estimator’s evaluate function.</li>
</ul>
</li>
<li>eval_delay_secs<ul>
<li>Start evaluating after waiting for this many seconds.</li>
</ul>
</li>
<li>continuous_eval_throttle_secs<ul>
<li>when the last evaluation was started at least this many seconds ago for continuous_eval(), re-evaluate.</li>
</ul>
</li>
<li>min_eval_frequency<ul>
<li>(applies only to train_and_evaluate).</li>
<li>the minimum number of steps between evaluations.</li>
<li>Of course, evaluation does not occur if no new snapshot is available, hence, this is the minimum. If 0, the evaluation will only happen after training. If None, defaults to 1, unless model_dir is on GCS, in which case the default is 1000.</li>
</ul>
</li>
<li>delay_workers_by_global_step<ul>
<li>if True delays training workers based on global step instead of time.</li>
</ul>
</li>
<li>export_strategies<ul>
<li>Iterable of ExportStrategys, or a single one, or None.</li>
</ul>
</li>
<li>train_steps_per_iteration<ul>
<li>(applies only to continuous_train_and_eval).</li>
<li>Perform this many (integer) number of train steps for each training-evaluation iteration.</li>
<li>With a small value, the model will be evaluated more frequently with more checkpoints saved. </li>
<li>If None, will use a default value (which is smaller than train_steps if provided).</li>
</ul>
</li>
</ul>
<h3 id="supervisor"><a href="#supervisor" class="headerlink" title="supervisor"></a>supervisor</h3><h4 id="basic-use-process"><a href="#basic-use-process" class="headerlink" title="basic use process"></a>basic use process</h4><ol>
<li>Create a Supervisor object,<ul>
<li>parameter: logdir</li>
<li>the path to a directory where to save checkpoints and summaries.</li>
</ul>
</li>
<li>Ask the supervisor for a session with <code>tf.train.Supervisor.managed_session</code></li>
<li>Use the session to execute a train op, checking at each step if the supervisor requests that the training stops.</li>
</ol>
<h5 id="started-services"><a href="#started-services" class="headerlink" title="started services"></a>started services</h5><p>The managed_session() call starts a few services, which run in their own threads and use the managed session to run ops in your graph.<br>If your graph contains an integer variable named global_step, the services use its value to measure the number of training steps executed.</p>
<ul>
<li>Checkpointing service: Saves a copy of the graph variables in the logdir. <ul>
<li>The checkpoint filename uses the value of the global_step variable if one was added to your graph.</li>
<li>Runs every 10 minutes by default.</li>
</ul>
</li>
<li>Summary service: Runs all the summary ops and appends their output to an events file in the logdir.<ul>
<li>Runs every 2 minutes by default.</li>
</ul>
</li>
<li>Step counter: Counts how many steps have been executed, by looking at changes in the global_step variable.<ul>
<li>Appends a summary to the events file reporting the number of global steps per second.</li>
<li>The summary tag is “global_step/sec”. </li>
<li>This also runs every 2 minutes by default.</li>
</ul>
</li>
<li><em>Queue Runners</em>: If any <code>tf.train.QueueRunner</code> were added to the graph, the supervisor launches them in their own threads.</li>
</ul>
<h5 id="Checking-for-Stop"><a href="#Checking-for-Stop" class="headerlink" title="Checking for Stop"></a>Checking for Stop</h5><p>The check for stop in the main training loop is important and necessary.</p>
<ol>
<li>Exceptions raised in the service threads are reported to the supervisor which then sets its should_stop() condition to true.</li>
<li>Other service threads notice that condition and terminate properly. </li>
<li>The main training loop, within the managed_session() block, must also check for the stop condition and terminate.</li>
</ol>
<p><strong><em>Notice</em></strong>:<br><code>managed_session()</code> takes care of catching exceptions raised from the training loop to report them to the supervisor.<br>The main loop does not need to do anything special about exceptions. It only needs to check for the stop condition.</p>
<h5 id="Recovery"><a href="#Recovery" class="headerlink" title="Recovery"></a>Recovery</h5><p>If the training program shuts down or crashes, its most recent checkpoint and event files are left in the logdir.<br>When you restart the program, managed_session() restores the graph from the most recent checkpoint and resumes training where it stopped.<br>A new events file is created. If you start TensorBoard and point it to the logdir, it will know how to <strong>merge</strong> the contents of the two events files and will show the training resuming at the last global step from the checkpoint.</p>
<h4 id="Larger-Model-Scenario"><a href="#Larger-Model-Scenario" class="headerlink" title="Larger Model Scenario"></a>Larger Model Scenario</h4><p>Larger models may run out memory when the summary service runs:</p>
<ul>
<li>The summary ops are run in parallel with the main loop running the train op.</li>
<li>This can cause memory usage to peak to up to two times the normal use.</li>
</ul>
<p>For a larger model you can tell the supervisor to <strong>not run</strong> the summary service and instead run it yourself in your main training loop:</p>
<ul>
<li>pass <code>summary_op=None</code> when constructing the supervisor.</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">...create graph...</div><div class="line">my_train_op = ...</div><div class="line">my_summary_op = tf.summary.merge_all()</div><div class="line"></div><div class="line">sv = tf.train.Supervisor(logdir=<span class="string">"/my/training/directory"</span>,</div><div class="line"> summary_op=<span class="keyword">None</span>) <span class="comment"># Do not run the summary service</span></div><div class="line"><span class="keyword">with</span> sv.managed_session() <span class="keyword">as</span> sess:</div><div class="line"> <span class="keyword">for</span> step <span class="keyword">in</span> range(<span class="number">100000</span>):</div><div class="line"> <span class="keyword">if</span> sv.should_stop():</div><div class="line"> <span class="keyword">break</span></div><div class="line"> <span class="keyword">if</span> step % <span class="number">100</span> == <span class="number">0</span>:</div><div class="line"> _, summ = sess.run([my_train_op, my_summary_op])</div><div class="line"> sv.summary_computed(sess, summ)</div><div class="line"> <span class="keyword">else</span>:</div><div class="line"> sess.run(my_train_op)</div></pre></td></tr></table></figure>
<h4 id="Pre-trained-Model-Scenario"><a href="#Pre-trained-Model-Scenario" class="headerlink" title="Pre-trained Model Scenario"></a>Pre-trained Model Scenario</h4><p>The managed_session() call takes care of initializing the model in the session</p>
<ul>
<li>If model is available, it is restored from a checkpoint.</li>
<li>otherwise, initialized from scratch.</li>
</ul>
<p>One common scenario is to initialize the model by loading a “pre-trained” checkpoint that was saved while training a usually slightly different model using a different dataset.</p>
<h5 id="init-function"><a href="#init-function" class="headerlink" title="init function"></a>init function</h5><ul>
<li>is called only if the model needs to be initialized from scratch</li>
<li>not when the model can be recovered from a checkpoint from the logdir.</li>
</ul>
<p>To load the pre-trained model, the init function needs a <code>tf.train.Saver</code> object</p>
<ul>
<li>This saver must only restore the pre-trained variables</li>
<li>This is usually a good idea because the new model may contain variables that are not present in the pre-trained checkpoint</li>
<li>If you were using the default saver, you could get an error trying to restore all the variables of the new model from the pre-trained checkpoint.</li>
</ul>
<p>The process is below:<br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">...create graph...</div><div class="line"><span class="comment"># Create a saver that restores only the pre-trained variables.</span></div><div class="line">pre_train_saver = tf.train.Saver([pre_train_var1, pre_train_var2])</div><div class="line"></div><div class="line"><span class="comment"># Define an init function that loads the pretrained checkpoint.</span></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">load_pretrain</span><span class="params">(sess)</span>:</span></div><div class="line"> pre_train_saver.restore(sess, <span class="string">"<path to pre-trained-checkpoint>"</span>)</div><div class="line"></div><div class="line"><span class="comment"># Pass the init function to the supervisor.</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># The init function is called _after_ the variables have been initialized</span></div><div class="line"><span class="comment"># by running the init_op.</span></div><div class="line">sv = tf.train.Supervisor(logdir=<span class="string">"/my/training/directory"</span>,</div><div class="line"> init_fn=load_pretrain)</div><div class="line"><span class="keyword">with</span> sv.managed_session() <span class="keyword">as</span> sess:</div><div class="line"> <span class="comment"># Here sess was either initialized from the pre-trained-checkpoint or</span></div><div class="line"> <span class="comment"># recovered from a checkpoint saved in a previous run of this code.</span></div><div class="line"> ...</div></pre></td></tr></table></figure></p>
<h4 id="Running-Your-Own-Services"><a href="#Running-Your-Own-Services" class="headerlink" title="Running Your Own Services"></a>Running Your Own Services</h4><p>For example to fetch different sets of summaries on a different schedule than the usual summary service.<br>Use the <code>tf.train.Supervisor.loop</code>method</p>
<ul>
<li>It repeatedly calls a function of your choice on a timer until the supervisor stop condition becomes true</li>
<li>It plays nicely with the other services.</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">my_additional_summaries</span><span class="params">(sv, sess)</span>:</span></div><div class="line"> ...fetch <span class="keyword">and</span> write summaries, see below...</div><div class="line"></div><div class="line">...</div><div class="line"> sv = tf.train.Supervisor(logdir=<span class="string">"/my/training/directory"</span>)</div><div class="line"> <span class="keyword">with</span> sv.managed_session() <span class="keyword">as</span> sess:</div><div class="line"> <span class="comment"># Call my_additional_summaries() every 1200s, or 20mn,</span></div><div class="line"> <span class="comment"># passing (sv, sess) as arguments.</span></div><div class="line"> sv.loop(<span class="number">1200</span>, my_additional_summaries, args=(sv, sess))</div><div class="line"> ...main training loop...</div></pre></td></tr></table></figure>
<h4 id="Writing-Summaries"><a href="#Writing-Summaries" class="headerlink" title="Writing Summaries"></a>Writing Summaries</h4><p>The supervisor always creates an events file in its logdir, as well as a <code>tf.summary.FileWriter</code> to append events and summaries to that file.<br>If you want to write your own summaries it is a good idea to append them to that same events file</p>
<ul>
<li>TensorBoard likes it better when only one events file in a directory is being actively appended to.</li>
</ul>
<p>Method: <code>tf.train.Supervisor.summary_computed</code><br><figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">def</span> <span class="title">my_additional_summaries</span><span class="params">(sv, sess)</span>:</span></div><div class="line"> summaries = sess.run(my_additional_summary_op)</div><div class="line"> sv.summary_computed(sess, summaries)</div></pre></td></tr></table></figure></p>
<p>For more advanced usages:<br><code>tf.train.Supervisor.summary_writer</code><br><a href="https://www.tensorflow.org/api_docs/python/tf/train/Supervisor#summary_writer" target="_blank" rel="external">https://www.tensorflow.org/api_docs/python/tf/train/Supervisor#summary_writer</a></p>
<h4 id="Supervisor-Reference"><a href="#Supervisor-Reference" class="headerlink" title="Supervisor Reference"></a>Supervisor Reference</h4><h5 id="Checkpointing-Where-and-When"><a href="#Checkpointing-Where-and-When" class="headerlink" title="Checkpointing: Where and When."></a>Checkpointing: Where and When.</h5><p>checkpointing service can be configured by the following keyword arguments to the Supervisor() constructor:</p>
<ul>
<li>logdir:<ul>
<li>path where the checkpointing service creates <strong>checkpoints</strong>.</li>
<li>Passing None disables the checkpointing and the summary services.</li>
</ul>
</li>
<li>checkpoint_basename<ul>
<li>Name of the checkpoint files to create, defaults to “model.ckpt”.</li>
<li>If the model contains a scalar integer variable named global_step, the value of that variable is appended to the checkpoint filename.</li>
</ul>
</li>
<li>save_model_secs<ul>
<li>Number of seconds between each checkpoint. Defaults to 600, or 10 minutes.</li>
</ul>
</li>
<li>saver<ul>
<li>A <code>tf.train.Saver</code> object to use for checkpointing.</li>
<li>Default creates one for you by calling tf.train.Saver(), which add ops to save and restore all variables in your model.</li>
<li>customer Saver need create customized Saver.</li>
</ul>
</li>
</ul>
<h5 id="Summaries-Where-and-When"><a href="#Summaries-Where-and-When" class="headerlink" title="Summaries: Where and When"></a>Summaries: Where and When</h5><p>the summary service can be configured by the following keyword arguments to the Supervisor() constructor:</p>
<ul>
<li>logdir<ul>
<li>Path to a directory where the summary service creates <strong>event</strong> files.</li>
<li>Passing None disables the summary service as well as the checkpointing services.</li>
</ul>
</li>
<li>save_summaries_secs<ul>
<li>Number of seconds between each run of the summary service</li>
<li>Defaults to 120, or 2 minutes.</li>
<li>Pass 0 to disable the summary service.</li>
</ul>
</li>
<li>summary_op<ul>
<li>Op to use to fetch the summaries.</li>
<li>Default use the first op in the <code>tf.GraphKeys.SUMMARY_OP</code> graph collection.</li>
<li>If the collection is empty the supervisor creates an op that aggregates all summaries in the graph using <code>tf.summary.merge_all()</code></li>
<li>Passing None disables the summary service.</li>
</ul>
</li>
<li>global_step<ul>
<li>Tensor to use to count the global step.</li>
<li>Default uses the first tensor in the tf.GraphKeys.GLOBAL_STEP graph collection.</li>
<li>If the collection is empty, the supervisor looks for a scalar integer variable named <code>global_step</code> in the graph.</li>
<li>If found, the global step tensor is used to measure the number of training steps executed</li>
<li>Note that your training op is responsible for incrementing the global step value.</li>
</ul>
</li>
</ul>
<h5 id="Model-Initialization-and-Recovery"><a href="#Model-Initialization-and-Recovery" class="headerlink" title="Model Initialization and Recovery"></a>Model Initialization and Recovery</h5><h6 id="initialization"><a href="#initialization" class="headerlink" title="initialization"></a>initialization</h6><p>The <code>managed_session()</code> call takes care of initializing or recovering a session.<br>It returns a session with a fully initialized model, ready to run ops.<br>If a checkpoint <strong>exists</strong> in the logdir when <code>managed_session()</code> is called, the model is initialized by loading that <strong>checkpoint</strong><br><strong>otherwise</strong> it is initialized by calling an init op and optionally an init function.<br>When no checkpoint is available, model initialization is controlled by the following keyword arguments to the <code>Supervisor()</code> constructor:</p>
<ul>
<li>init_op<ul>
<li>Op to run to initialize the model.</li>
<li>Default uses the first op in the <code>tf.GraphKeys.INIT_OP</code> collection.</li>
<li>If the collection is empty, the supervisor adds an op to initialize all the variables in the graph by calling <code>tf.global_variables_initializer()</code>.</li>
<li>Pass None to not use an init op.</li>
</ul>
</li>
<li>init_fn<ul>
<li>Python function to call to initialize the model.</li>
<li>If specified, called as init_fn(sess) where sess is the managed session. </li>
<li>If an init op is also used, the init function is called after the init op.</li>
</ul>
</li>
<li>local_init_op<ul>
<li>An additional op to initialize parts of the graph that are not saved in checkpoints such as tables and local variables.</li>
<li>The local init op is run before the init op and the init function.</li>
<li>If not specified, the supervisor uses the first op in the <code>tf.GraphKeys.LOCAL_INIT_OP</code> collection.</li>
<li>If the collection is empty the supervisor adds an op to initialize all the tables and local variables in the graph by calling <code>tf.tables_initializer()</code> and <code>tf.local_variables_initializer()</code>.</li>
<li>Pass None to not use a local init op.</li>
</ul>
</li>
<li>ready_op<ul>
<li>Op to check if the model is initialized.</li>
<li>After running the local init op, the init op, and the init function, the supervisor verifies that the model is fully initialized by running the ready op. </li>
<li>This is an op that returns an empty string if the model is initialized, or a description of what parts of the model are not initialized if not.</li>
<li>This is an op that returns an empty string if the model is initialized, or a description of what parts of the model are not initialized if not.</li>
<li>If not specified, the supervisor uses the first op in the tf.GraphKeys.READY_OP collection.</li>
<li>f the collection is empty the supervisor creates a ready op that verifies that all variables are initialized by calling <code>tf.report_uninitialized_variables()</code>.</li>
<li>Pass None to disable the ready op. In that case the model is not checked after initialization.</li>
</ul>
</li>
</ul>
<h6 id="Recovery-1"><a href="#Recovery-1" class="headerlink" title="Recovery"></a>Recovery</h6><p>Checkpoint recovery is controlled by the following keyword arguments to the Supervisor() constructor:</p>
<ul>
<li>logdir<ul>
<li>Path to a directory in which to look for checkpoints.</li>
<li>The checkpoint service saves a metadata file, named “checkpoint”, in the checkpoint directory that indicates the path to the most recent checkpoint.</li>
<li>This file is in text format. When in a pinch, you can edit it manually to recover from a different checkpoint than the most recent one.</li>
</ul>
</li>
<li>ready_op: (see above).<ul>
<li>The ready op is run before and after loading the checkpoint. </li>
<li>The first run checks if the model needs to be initialized</li>
<li>the second run verifies that the model is fully initialized.</li>
</ul>
</li>
<li>local_init_op: (see above).<ul>
<li>The local init op is run before running the ready op the first time, to initialize local variables and tables.</li>
<li>saver: (see above). Saver object used to load the checkpoint.</li>
</ul>
</li>
</ul>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/08/23/tf_distributed/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Klpek">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Klpek's Note Library">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/08/23/tf_distributed/" itemprop="url">
Untitled
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">Posted on</span>
<time title="Post created" itemprop="dateCreated datePublished" datetime="2017-08-23T21:39:04+08:00">
2017-08-23
</time>