Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

models.html 45 KB

You have to be logged in to leave a comment. Sign In
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
  6. <meta name="generator" content="pdoc 0.7.2" />
  7. <title>src.models API documentation</title>
  8. <meta name="description" content="" />
  9. <link href='https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.0/normalize.min.css' rel='stylesheet'>
  10. <link href='https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/8.0.0/sanitize.min.css' rel='stylesheet'>
  11. <link href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css" rel="stylesheet">
  12. <style>.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{font-weight:bold}#index h4 + ul{margin-bottom:.6em}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
  13. <style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
  14. <style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
  15. </head>
  16. <body>
  17. <main>
  18. <article id="content">
  19. <header>
  20. <h1 class="title">Module <code>src.models</code></h1>
  21. </header>
  22. <section id="section-intro">
  23. <details class="source">
  24. <summary>
  25. <span>Expand source code</span>
  26. </summary>
  27. <pre><code class="python">import numpy as np
  28. from torch import nn
  29. import torch.nn.functional as F
  30. import torch
  31. class VideoNet(nn.Module):
  32. def __init__(self):
  33. super(VideoNet, self).__init__()
  34. self.conv1 = nn.Conv2d(in_channels=1, out_channels=3, kernel_size=5)
  35. self.relu1 = nn.ReLU()
  36. self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=1)
  37. self.conv2 = nn.Conv2d(in_channels=3, out_channels=1, kernel_size=3)
  38. self.lstm = nn.LSTM(input_size=1, hidden_size=40, num_layers=1, batch_first=True)
  39. self.fc = nn.Linear(40, 1)
  40. # self.conv2 = nn.Conv1d(in_channels=H, out_channels=3, kernel_size=5)
  41. # self.maxpool2 = nn.MaxPool1d(kernel_size=2)
  42. # self.fc = nn.Linear(18 + p, 1) # this is hard-coded
  43. def forward(self, x):
  44. &#39;&#39;&#39;
  45. x: torch.Tensor
  46. (batch_size, time_steps, height, width)
  47. = (batch_size, 40, 10, 10)
  48. &#39;&#39;&#39;
  49. # print(&#39;in shape&#39;, x.shape)
  50. # extract features from each time_step separately
  51. # reshape time_steps and batch into same dim
  52. batch_size = x.shape[0]
  53. T = x.shape[1]
  54. x = x.reshape(batch_size * T, 1, x.shape[2], x.shape[3])
  55. x = self.conv1(x)
  56. x = self.relu1(x)
  57. x = self.maxpool1(x)
  58. x = self.conv2(x)
  59. x = torch.max(x, dim=3).values
  60. x = torch.max(x, dim=2).values
  61. # extract time_steps back out
  62. # run lstm on result 1D time series
  63. x = x.reshape(batch_size, T, 1)
  64. outputs, (h1, c1) = self.lstm(x) # get hidden vec
  65. h1 = h1.squeeze(0) # remove dimension corresponding to multiple layers / directions
  66. return self.fc(h1)
  67. class FCNN(nn.Module):
  68. &#34;&#34;&#34;
  69. customized (one hidden layer) fully connected neural network class
  70. &#34;&#34;&#34;
  71. def __init__(self, D_in, H, p):
  72. &#34;&#34;&#34;
  73. Parameters:
  74. ==========================================================
  75. D_in: int
  76. dimension of input track
  77. H: int
  78. hidden layer size
  79. p: int
  80. number of additional covariates (such as lifetime, msd, etc..., to be concatenated to the hidden layer)
  81. &#34;&#34;&#34;
  82. super(FCNN, self).__init__()
  83. self.fc1 = nn.Linear(D_in, H)
  84. #self.fc2 = nn.Linear(H, H)
  85. self.bn1 = nn.BatchNorm1d(H)
  86. self.fc2 = nn.Linear(H + p, 1)
  87. def forward(self, x1, x2):
  88. z1 = self.fc1(x1)
  89. z1 = self.bn1(z1)
  90. h1 = F.relu(z1)
  91. if x2 is not None:
  92. h1 = torch.cat((h1, x2), 1)
  93. z2 = self.fc2(h1)
  94. #h2 = F.relu(z2)
  95. #z3 = self.fc3(h2)
  96. return z2
  97. class LSTMNet(nn.Module):
  98. def __init__(self, D_in, H, p):
  99. &#34;&#34;&#34;
  100. Parameters:
  101. ==========================================================
  102. D_in: int
  103. dimension of input track (ignored, can be variable)
  104. H: int
  105. hidden layer size
  106. p: int
  107. number of additional covariates (such as lifetime, msd, etc..., to be concatenated to the hidden layer)
  108. &#34;&#34;&#34;
  109. super(LSTMNet, self).__init__()
  110. self.lstm = nn.LSTM(input_size=1, hidden_size=H, num_layers=1, batch_first=True)
  111. self.fc = nn.Linear(H + p, 1)
  112. def forward(self, x1, x2=None):
  113. x1 = x1.unsqueeze(2) # add input_size dimension (this is usually for the size of embedding vector)
  114. outputs, (h1, c1) = self.lstm(x1) # get hidden vec
  115. h1 = h1.squeeze(0) # remove dimension corresponding to multiple layers / directions
  116. if x2 is not None:
  117. h1 = torch.cat((h1, x2), 1)
  118. return self.fc(h1)
  119. class CNN(nn.Module):
  120. def __init__(self, D_in, H, p):
  121. &#34;&#34;&#34;
  122. Parameters:
  123. ==========================================================
  124. D_in: int
  125. dimension of input track (ignored, can be variable)
  126. H: int
  127. hidden layer size
  128. p: int
  129. number of additional covariates (such as lifetime, msd, etc..., to be concatenated to the hidden layer)
  130. &#34;&#34;&#34;
  131. super(CNN, self).__init__()
  132. self.conv1 = nn.Conv1d(in_channels=1, out_channels=H, kernel_size=7)
  133. self.maxpool1 = nn.MaxPool1d(kernel_size=2)
  134. self.conv2 = nn.Conv1d(in_channels=H, out_channels=3, kernel_size=5)
  135. self.maxpool2 = nn.MaxPool1d(kernel_size=2)
  136. self.fc = nn.Linear(18 + p, 1) # this is hard-coded
  137. def forward(self, x1, x2):
  138. x1 = x1.unsqueeze(1) # add channel dim
  139. x1 = self.conv1(x1)
  140. x1 = self.maxpool1(x1)
  141. x1 = self.conv2(x1)
  142. x1 = self.maxpool2(x1)
  143. x1 = x1.reshape(x1.shape[0], -1) # flatten channel dim
  144. if x2 is not None:
  145. x1 = torch.cat((x1, x2), 1)
  146. return self.fc(x1)
  147. class AttentionNet(nn.Module):
  148. &#34;&#34;&#34;
  149. customized (one hidden layer) fully connected neural network class
  150. &#34;&#34;&#34;
  151. def __init__(self, D_in, H, p):
  152. &#34;&#34;&#34;
  153. Parameters:
  154. ==========================================================
  155. D_in: int
  156. dimension of input track (ignored, can be variable)
  157. H: int
  158. hidden layer size
  159. p: int
  160. number of additional covariates (such as lifetime, msd, etc..., to be concatenated to the hidden layer)
  161. &#34;&#34;&#34;
  162. super(AttentionNet, self).__init__()
  163. self.att1 = nn.MultiheadAttention(embed_dim=18, num_heads=3)
  164. self.ln1 = nn.LayerNorm(D_in)
  165. self.fc1 = nn.Linear(D_in, 1)
  166. self.relu1 = nn.ReLU()
  167. self.att2 = nn.MultiheadAttention(embed_dim=18, num_heads=3)
  168. self.ln2 = nn.LayerNorm(D_in)
  169. self.fc2 = nn.Linear(D_in + p, 1)
  170. def forward(self, x1, x2):
  171. print(x1.shape)
  172. x1 = self.att1(x1, x1)
  173. x1 = self.ln1(x1)
  174. x1 = self.fc1(x1)
  175. x1 = self.relu1(x1)
  176. x1 = self.att2(x1, x1)
  177. x1 = self.ln2(x1)
  178. if x2 is not None:
  179. h1 = torch.cat((h1, x2), 1)
  180. return self.fc2(h1)
  181. class MaxLinear(nn.Module):
  182. &#39;&#39;&#39;Takes flattened input and predicts it using many linear units
  183. X: batch_size x num_timepoints
  184. &#39;&#39;&#39;
  185. def __init__(self, input_dim=24300, num_units=20, nonlin=F.relu, use_bias=False):
  186. super(MaxLinear, self).__init__()
  187. self.fc1 = nn.Linear(input_dim, num_units, bias=use_bias)
  188. # self.offset = nn.Parameter(torch.Tensor([0]))
  189. def forward(self, X, **kwargs):
  190. # print(&#39;in shape&#39;, X.shape, X.dtype)
  191. X = self.fc1(X) # .max(dim=-1)
  192. # print(&#39;out shape&#39;, X.shape, X.dtype)
  193. X = torch.max(X, dim=1)[0] # 0 because this returns max, indices
  194. # print(&#39;out2 shape&#39;, X.shape, X.dtype)
  195. return X # + self.offset
  196. class MaxConv(nn.Module):
  197. &#39;&#39;&#39;Takes flattened input and predicts it using many conv unit
  198. X: batch_size x 1 x num_timepoints
  199. OR
  200. X: list of size (num_timepoints,)
  201. &#39;&#39;&#39;
  202. def __init__(self, num_units=20, kernel_size=30, nonlin=F.relu, use_bias=False):
  203. super(MaxConv, self).__init__()
  204. self.conv1 = nn.Conv1d(in_channels=1, out_channels=num_units, kernel_size=kernel_size, bias=use_bias)
  205. # torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode=&#39;zeros&#39;)
  206. self.offset = nn.Parameter(torch.Tensor([0]))
  207. def forward(self, X, **kwargs):
  208. if type(X) == list:
  209. print(&#39;list&#39;)
  210. X = torch.tensor(np.array(X).astype(np.float32))
  211. X = X.unsqueeze(0)
  212. X = X.unsqueeze(0)
  213. print(X.shape)
  214. # print(&#39;in shape&#39;, X.shape, X.dtype)
  215. else:
  216. X = X.unsqueeze(1)
  217. X = self.conv1(X) # .max(dim=-1)
  218. # print(&#39;out shape&#39;, X.shape, X.dtype)
  219. # max over channels
  220. X = torch.max(X, dim=1)[0] # 0 because this returns max, indices
  221. # max over time step
  222. X = torch.max(X, dim=1)[0] + self.offset # 0 because this returns max, indices
  223. # print(&#39;out2 shape&#39;, X.shape, X.dtype)
  224. X = X.unsqueeze(1)
  225. # print(&#39;preds&#39;, X)
  226. return X
  227. class MaxConvLinear(nn.Module):
  228. &#39;&#39;&#39;Takes input patch, uses linear filter to convert it to time series, then runs temporal conv, then takes max
  229. X: batch_size x H_patch x W_patch x time
  230. &#39;&#39;&#39;
  231. def __init__(self, num_timepoints=300, num_linear_filts=1, num_conv_filts=3, patch_size=9,
  232. kernel_size=30, nonlin=F.relu, use_bias=False):
  233. super(MaxConvLinear, self).__init__()
  234. self.fc1 = nn.Linear(patch_size * patch_size, num_linear_filts, bias=use_bias)
  235. self.conv1 = nn.Conv1d(in_channels=num_linear_filts, out_channels=num_conv_filts, kernel_size=kernel_size,
  236. bias=use_bias)
  237. self.offset = nn.Parameter(torch.Tensor([0]))
  238. def forward(self, X, **kwargs):
  239. s = X.shape # batch_size x H_patch x W_patch x time
  240. X = X.reshape(s[0], s[1] * s[2], s[3])
  241. X = torch.transpose(X, 1, 2)
  242. # print(&#39;in shape&#39;, X.shape, X.dtype)
  243. X = self.fc1(X) # .max(dim=-1)
  244. X = torch.transpose(X, 1, 2)
  245. X = self.conv1(X) # .max(dim=-1)
  246. # print(&#39;out shape&#39;, X.shape, X.dtype)
  247. # max over channels
  248. X = torch.max(X, dim=1)[0] # 0 because this returns max, indices
  249. # max over time step
  250. X = torch.max(X, dim=1)[0] # + self.offset # 0 because this returns max, indices
  251. # print(&#39;out2 shape&#39;, X.shape, X.dtype)
  252. X = X.unsqueeze(1)
  253. return X</code></pre>
  254. </details>
  255. </section>
  256. <section>
  257. </section>
  258. <section>
  259. </section>
  260. <section>
  261. </section>
  262. <section>
  263. <h2 class="section-title" id="header-classes">Classes</h2>
  264. <dl>
  265. <dt id="src.models.AttentionNet"><code class="flex name class">
  266. <span>class <span class="ident">AttentionNet</span></span>
  267. <span>(</span><span>D_in, H, p)</span>
  268. </code></dt>
  269. <dd>
  270. <section class="desc"><p>customized (one hidden layer) fully connected neural network class</p>
  271. <h1 id="parameters">Parameters:</h1>
  272. <pre><code>D_in: int
  273. dimension of input track (ignored, can be variable)
  274. H: int
  275. hidden layer size
  276. p: int
  277. number of additional covariates (such as lifetime, msd, etc..., to be concatenated to the hidden layer)
  278. </code></pre></section>
  279. <details class="source">
  280. <summary>
  281. <span>Expand source code</span>
  282. </summary>
  283. <pre><code class="python">class AttentionNet(nn.Module):
  284. &#34;&#34;&#34;
  285. customized (one hidden layer) fully connected neural network class
  286. &#34;&#34;&#34;
  287. def __init__(self, D_in, H, p):
  288. &#34;&#34;&#34;
  289. Parameters:
  290. ==========================================================
  291. D_in: int
  292. dimension of input track (ignored, can be variable)
  293. H: int
  294. hidden layer size
  295. p: int
  296. number of additional covariates (such as lifetime, msd, etc..., to be concatenated to the hidden layer)
  297. &#34;&#34;&#34;
  298. super(AttentionNet, self).__init__()
  299. self.att1 = nn.MultiheadAttention(embed_dim=18, num_heads=3)
  300. self.ln1 = nn.LayerNorm(D_in)
  301. self.fc1 = nn.Linear(D_in, 1)
  302. self.relu1 = nn.ReLU()
  303. self.att2 = nn.MultiheadAttention(embed_dim=18, num_heads=3)
  304. self.ln2 = nn.LayerNorm(D_in)
  305. self.fc2 = nn.Linear(D_in + p, 1)
  306. def forward(self, x1, x2):
  307. print(x1.shape)
  308. x1 = self.att1(x1, x1)
  309. x1 = self.ln1(x1)
  310. x1 = self.fc1(x1)
  311. x1 = self.relu1(x1)
  312. x1 = self.att2(x1, x1)
  313. x1 = self.ln2(x1)
  314. if x2 is not None:
  315. h1 = torch.cat((h1, x2), 1)
  316. return self.fc2(h1)</code></pre>
  317. </details>
  318. <h3>Ancestors</h3>
  319. <ul class="hlist">
  320. <li>torch.nn.modules.module.Module</li>
  321. </ul>
  322. <h3>Methods</h3>
  323. <dl>
  324. <dt id="src.models.AttentionNet.forward"><code class="name flex">
  325. <span>def <span class="ident">forward</span></span>(<span>self, x1, x2)</span>
  326. </code></dt>
  327. <dd>
  328. <section class="desc"><p>Defines the computation performed at every call.</p>
  329. <p>Should be overridden by all subclasses.</p>
  330. <div class="admonition note">
  331. <p class="admonition-title">Note</p>
  332. <p>Although the recipe for forward pass needs to be defined within
  333. this function, one should call the :class:<code>Module</code> instance afterwards
  334. instead of this since the former takes care of running the
  335. registered hooks while the latter silently ignores them.</p>
  336. </div></section>
  337. <details class="source">
  338. <summary>
  339. <span>Expand source code</span>
  340. </summary>
  341. <pre><code class="python">def forward(self, x1, x2):
  342. print(x1.shape)
  343. x1 = self.att1(x1, x1)
  344. x1 = self.ln1(x1)
  345. x1 = self.fc1(x1)
  346. x1 = self.relu1(x1)
  347. x1 = self.att2(x1, x1)
  348. x1 = self.ln2(x1)
  349. if x2 is not None:
  350. h1 = torch.cat((h1, x2), 1)
  351. return self.fc2(h1)</code></pre>
  352. </details>
  353. </dd>
  354. </dl>
  355. </dd>
  356. <dt id="src.models.CNN"><code class="flex name class">
  357. <span>class <span class="ident">CNN</span></span>
  358. <span>(</span><span>D_in, H, p)</span>
  359. </code></dt>
  360. <dd>
  361. <section class="desc"><p>Base class for all neural network modules.</p>
  362. <p>Your models should also subclass this class.</p>
  363. <p>Modules can also contain other Modules, allowing to nest them in
  364. a tree structure. You can assign the submodules as regular attributes::</p>
  365. <pre><code>import torch.nn as nn
  366. import torch.nn.functional as F
  367. class Model(nn.Module):
  368. def __init__(self):
  369. super(Model, self).__init__()
  370. self.conv1 = nn.Conv2d(1, 20, 5)
  371. self.conv2 = nn.Conv2d(20, 20, 5)
  372. def forward(self, x):
  373. x = F.relu(self.conv1(x))
  374. return F.relu(self.conv2(x))
  375. </code></pre>
  376. <p>Submodules assigned in this way will be registered, and will have their
  377. parameters converted too when you call :meth:<code>to</code>, etc.</p>
  378. <p>:ivar training: Boolean represents whether this module is in training or
  379. evaluation mode.
  380. :vartype training: bool</p>
  381. <h1 id="parameters">Parameters:</h1>
  382. <pre><code>D_in: int
  383. dimension of input track (ignored, can be variable)
  384. H: int
  385. hidden layer size
  386. p: int
  387. number of additional covariates (such as lifetime, msd, etc..., to be concatenated to the hidden layer)
  388. </code></pre></section>
  389. <details class="source">
  390. <summary>
  391. <span>Expand source code</span>
  392. </summary>
  393. <pre><code class="python">class CNN(nn.Module):
  394. def __init__(self, D_in, H, p):
  395. &#34;&#34;&#34;
  396. Parameters:
  397. ==========================================================
  398. D_in: int
  399. dimension of input track (ignored, can be variable)
  400. H: int
  401. hidden layer size
  402. p: int
  403. number of additional covariates (such as lifetime, msd, etc..., to be concatenated to the hidden layer)
  404. &#34;&#34;&#34;
  405. super(CNN, self).__init__()
  406. self.conv1 = nn.Conv1d(in_channels=1, out_channels=H, kernel_size=7)
  407. self.maxpool1 = nn.MaxPool1d(kernel_size=2)
  408. self.conv2 = nn.Conv1d(in_channels=H, out_channels=3, kernel_size=5)
  409. self.maxpool2 = nn.MaxPool1d(kernel_size=2)
  410. self.fc = nn.Linear(18 + p, 1) # this is hard-coded
  411. def forward(self, x1, x2):
  412. x1 = x1.unsqueeze(1) # add channel dim
  413. x1 = self.conv1(x1)
  414. x1 = self.maxpool1(x1)
  415. x1 = self.conv2(x1)
  416. x1 = self.maxpool2(x1)
  417. x1 = x1.reshape(x1.shape[0], -1) # flatten channel dim
  418. if x2 is not None:
  419. x1 = torch.cat((x1, x2), 1)
  420. return self.fc(x1)</code></pre>
  421. </details>
  422. <h3>Ancestors</h3>
  423. <ul class="hlist">
  424. <li>torch.nn.modules.module.Module</li>
  425. </ul>
  426. <h3>Methods</h3>
  427. <dl>
  428. <dt id="src.models.CNN.forward"><code class="name flex">
  429. <span>def <span class="ident">forward</span></span>(<span>self, x1, x2)</span>
  430. </code></dt>
  431. <dd>
  432. <section class="desc"><p>Defines the computation performed at every call.</p>
  433. <p>Should be overridden by all subclasses.</p>
  434. <div class="admonition note">
  435. <p class="admonition-title">Note</p>
  436. <p>Although the recipe for forward pass needs to be defined within
  437. this function, one should call the :class:<code>Module</code> instance afterwards
  438. instead of this since the former takes care of running the
  439. registered hooks while the latter silently ignores them.</p>
  440. </div></section>
  441. <details class="source">
  442. <summary>
  443. <span>Expand source code</span>
  444. </summary>
  445. <pre><code class="python">def forward(self, x1, x2):
  446. x1 = x1.unsqueeze(1) # add channel dim
  447. x1 = self.conv1(x1)
  448. x1 = self.maxpool1(x1)
  449. x1 = self.conv2(x1)
  450. x1 = self.maxpool2(x1)
  451. x1 = x1.reshape(x1.shape[0], -1) # flatten channel dim
  452. if x2 is not None:
  453. x1 = torch.cat((x1, x2), 1)
  454. return self.fc(x1)</code></pre>
  455. </details>
  456. </dd>
  457. </dl>
  458. </dd>
  459. <dt id="src.models.FCNN"><code class="flex name class">
  460. <span>class <span class="ident">FCNN</span></span>
  461. <span>(</span><span>D_in, H, p)</span>
  462. </code></dt>
  463. <dd>
  464. <section class="desc"><p>customized (one hidden layer) fully connected neural network class</p>
  465. <h1 id="parameters">Parameters:</h1>
  466. <pre><code>D_in: int
  467. dimension of input track
  468. H: int
  469. hidden layer size
  470. p: int
  471. number of additional covariates (such as lifetime, msd, etc..., to be concatenated to the hidden layer)
  472. </code></pre></section>
  473. <details class="source">
  474. <summary>
  475. <span>Expand source code</span>
  476. </summary>
  477. <pre><code class="python">class FCNN(nn.Module):
  478. &#34;&#34;&#34;
  479. customized (one hidden layer) fully connected neural network class
  480. &#34;&#34;&#34;
  481. def __init__(self, D_in, H, p):
  482. &#34;&#34;&#34;
  483. Parameters:
  484. ==========================================================
  485. D_in: int
  486. dimension of input track
  487. H: int
  488. hidden layer size
  489. p: int
  490. number of additional covariates (such as lifetime, msd, etc..., to be concatenated to the hidden layer)
  491. &#34;&#34;&#34;
  492. super(FCNN, self).__init__()
  493. self.fc1 = nn.Linear(D_in, H)
  494. #self.fc2 = nn.Linear(H, H)
  495. self.bn1 = nn.BatchNorm1d(H)
  496. self.fc2 = nn.Linear(H + p, 1)
  497. def forward(self, x1, x2):
  498. z1 = self.fc1(x1)
  499. z1 = self.bn1(z1)
  500. h1 = F.relu(z1)
  501. if x2 is not None:
  502. h1 = torch.cat((h1, x2), 1)
  503. z2 = self.fc2(h1)
  504. #h2 = F.relu(z2)
  505. #z3 = self.fc3(h2)
  506. return z2</code></pre>
  507. </details>
  508. <h3>Ancestors</h3>
  509. <ul class="hlist">
  510. <li>torch.nn.modules.module.Module</li>
  511. </ul>
  512. <h3>Methods</h3>
  513. <dl>
  514. <dt id="src.models.FCNN.forward"><code class="name flex">
  515. <span>def <span class="ident">forward</span></span>(<span>self, x1, x2)</span>
  516. </code></dt>
  517. <dd>
  518. <section class="desc"><p>Defines the computation performed at every call.</p>
  519. <p>Should be overridden by all subclasses.</p>
  520. <div class="admonition note">
  521. <p class="admonition-title">Note</p>
  522. <p>Although the recipe for forward pass needs to be defined within
  523. this function, one should call the :class:<code>Module</code> instance afterwards
  524. instead of this since the former takes care of running the
  525. registered hooks while the latter silently ignores them.</p>
  526. </div></section>
  527. <details class="source">
  528. <summary>
  529. <span>Expand source code</span>
  530. </summary>
  531. <pre><code class="python">def forward(self, x1, x2):
  532. z1 = self.fc1(x1)
  533. z1 = self.bn1(z1)
  534. h1 = F.relu(z1)
  535. if x2 is not None:
  536. h1 = torch.cat((h1, x2), 1)
  537. z2 = self.fc2(h1)
  538. #h2 = F.relu(z2)
  539. #z3 = self.fc3(h2)
  540. return z2</code></pre>
  541. </details>
  542. </dd>
  543. </dl>
  544. </dd>
  545. <dt id="src.models.LSTMNet"><code class="flex name class">
  546. <span>class <span class="ident">LSTMNet</span></span>
  547. <span>(</span><span>D_in, H, p)</span>
  548. </code></dt>
  549. <dd>
  550. <section class="desc"><p>Base class for all neural network modules.</p>
  551. <p>Your models should also subclass this class.</p>
  552. <p>Modules can also contain other Modules, allowing to nest them in
  553. a tree structure. You can assign the submodules as regular attributes::</p>
  554. <pre><code>import torch.nn as nn
  555. import torch.nn.functional as F
  556. class Model(nn.Module):
  557. def __init__(self):
  558. super(Model, self).__init__()
  559. self.conv1 = nn.Conv2d(1, 20, 5)
  560. self.conv2 = nn.Conv2d(20, 20, 5)
  561. def forward(self, x):
  562. x = F.relu(self.conv1(x))
  563. return F.relu(self.conv2(x))
  564. </code></pre>
  565. <p>Submodules assigned in this way will be registered, and will have their
  566. parameters converted too when you call :meth:<code>to</code>, etc.</p>
  567. <p>:ivar training: Boolean represents whether this module is in training or
  568. evaluation mode.
  569. :vartype training: bool</p>
  570. <h1 id="parameters">Parameters:</h1>
  571. <pre><code>D_in: int
  572. dimension of input track (ignored, can be variable)
  573. H: int
  574. hidden layer size
  575. p: int
  576. number of additional covariates (such as lifetime, msd, etc..., to be concatenated to the hidden layer)
  577. </code></pre></section>
  578. <details class="source">
  579. <summary>
  580. <span>Expand source code</span>
  581. </summary>
  582. <pre><code class="python">class LSTMNet(nn.Module):
  583. def __init__(self, D_in, H, p):
  584. &#34;&#34;&#34;
  585. Parameters:
  586. ==========================================================
  587. D_in: int
  588. dimension of input track (ignored, can be variable)
  589. H: int
  590. hidden layer size
  591. p: int
  592. number of additional covariates (such as lifetime, msd, etc..., to be concatenated to the hidden layer)
  593. &#34;&#34;&#34;
  594. super(LSTMNet, self).__init__()
  595. self.lstm = nn.LSTM(input_size=1, hidden_size=H, num_layers=1, batch_first=True)
  596. self.fc = nn.Linear(H + p, 1)
  597. def forward(self, x1, x2=None):
  598. x1 = x1.unsqueeze(2) # add input_size dimension (this is usually for the size of embedding vector)
  599. outputs, (h1, c1) = self.lstm(x1) # get hidden vec
  600. h1 = h1.squeeze(0) # remove dimension corresponding to multiple layers / directions
  601. if x2 is not None:
  602. h1 = torch.cat((h1, x2), 1)
  603. return self.fc(h1)</code></pre>
  604. </details>
  605. <h3>Ancestors</h3>
  606. <ul class="hlist">
  607. <li>torch.nn.modules.module.Module</li>
  608. </ul>
  609. <h3>Methods</h3>
  610. <dl>
  611. <dt id="src.models.LSTMNet.forward"><code class="name flex">
  612. <span>def <span class="ident">forward</span></span>(<span>self, x1, x2=None)</span>
  613. </code></dt>
  614. <dd>
  615. <section class="desc"><p>Defines the computation performed at every call.</p>
  616. <p>Should be overridden by all subclasses.</p>
  617. <div class="admonition note">
  618. <p class="admonition-title">Note</p>
  619. <p>Although the recipe for forward pass needs to be defined within
  620. this function, one should call the :class:<code>Module</code> instance afterwards
  621. instead of this since the former takes care of running the
  622. registered hooks while the latter silently ignores them.</p>
  623. </div></section>
  624. <details class="source">
  625. <summary>
  626. <span>Expand source code</span>
  627. </summary>
  628. <pre><code class="python">def forward(self, x1, x2=None):
  629. x1 = x1.unsqueeze(2) # add input_size dimension (this is usually for the size of embedding vector)
  630. outputs, (h1, c1) = self.lstm(x1) # get hidden vec
  631. h1 = h1.squeeze(0) # remove dimension corresponding to multiple layers / directions
  632. if x2 is not None:
  633. h1 = torch.cat((h1, x2), 1)
  634. return self.fc(h1)</code></pre>
  635. </details>
  636. </dd>
  637. </dl>
  638. </dd>
  639. <dt id="src.models.MaxConv"><code class="flex name class">
  640. <span>class <span class="ident">MaxConv</span></span>
  641. <span>(</span><span>num_units=20, kernel_size=30, nonlin=&lt;function relu&gt;, use_bias=False)</span>
  642. </code></dt>
  643. <dd>
  644. <section class="desc"><p>Takes flattened input and predicts it using many conv unit
  645. X: batch_size x 1 x num_timepoints
  646. OR
  647. X: list of size (num_timepoints,)</p>
  648. <p>Initializes internal Module state, shared by both nn.Module and ScriptModule.</p></section>
  649. <details class="source">
  650. <summary>
  651. <span>Expand source code</span>
  652. </summary>
  653. <pre><code class="python">class MaxConv(nn.Module):
  654. &#39;&#39;&#39;Takes flattened input and predicts it using many conv unit
  655. X: batch_size x 1 x num_timepoints
  656. OR
  657. X: list of size (num_timepoints,)
  658. &#39;&#39;&#39;
  659. def __init__(self, num_units=20, kernel_size=30, nonlin=F.relu, use_bias=False):
  660. super(MaxConv, self).__init__()
  661. self.conv1 = nn.Conv1d(in_channels=1, out_channels=num_units, kernel_size=kernel_size, bias=use_bias)
  662. # torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode=&#39;zeros&#39;)
  663. self.offset = nn.Parameter(torch.Tensor([0]))
  664. def forward(self, X, **kwargs):
  665. if type(X) == list:
  666. print(&#39;list&#39;)
  667. X = torch.tensor(np.array(X).astype(np.float32))
  668. X = X.unsqueeze(0)
  669. X = X.unsqueeze(0)
  670. print(X.shape)
  671. # print(&#39;in shape&#39;, X.shape, X.dtype)
  672. else:
  673. X = X.unsqueeze(1)
  674. X = self.conv1(X) # .max(dim=-1)
  675. # print(&#39;out shape&#39;, X.shape, X.dtype)
  676. # max over channels
  677. X = torch.max(X, dim=1)[0] # 0 because this returns max, indices
  678. # max over time step
  679. X = torch.max(X, dim=1)[0] + self.offset # 0 because this returns max, indices
  680. # print(&#39;out2 shape&#39;, X.shape, X.dtype)
  681. X = X.unsqueeze(1)
  682. # print(&#39;preds&#39;, X)
  683. return X</code></pre>
  684. </details>
  685. <h3>Ancestors</h3>
  686. <ul class="hlist">
  687. <li>torch.nn.modules.module.Module</li>
  688. </ul>
  689. <h3>Methods</h3>
  690. <dl>
  691. <dt id="src.models.MaxConv.forward"><code class="name flex">
  692. <span>def <span class="ident">forward</span></span>(<span>self, X, **kwargs)</span>
  693. </code></dt>
  694. <dd>
  695. <section class="desc"><p>Defines the computation performed at every call.</p>
  696. <p>Should be overridden by all subclasses.</p>
  697. <div class="admonition note">
  698. <p class="admonition-title">Note</p>
  699. <p>Although the recipe for forward pass needs to be defined within
  700. this function, one should call the :class:<code>Module</code> instance afterwards
  701. instead of this since the former takes care of running the
  702. registered hooks while the latter silently ignores them.</p>
  703. </div></section>
  704. <details class="source">
  705. <summary>
  706. <span>Expand source code</span>
  707. </summary>
  708. <pre><code class="python">def forward(self, X, **kwargs):
  709. if type(X) == list:
  710. print(&#39;list&#39;)
  711. X = torch.tensor(np.array(X).astype(np.float32))
  712. X = X.unsqueeze(0)
  713. X = X.unsqueeze(0)
  714. print(X.shape)
  715. # print(&#39;in shape&#39;, X.shape, X.dtype)
  716. else:
  717. X = X.unsqueeze(1)
  718. X = self.conv1(X) # .max(dim=-1)
  719. # print(&#39;out shape&#39;, X.shape, X.dtype)
  720. # max over channels
  721. X = torch.max(X, dim=1)[0] # 0 because this returns max, indices
  722. # max over time step
  723. X = torch.max(X, dim=1)[0] + self.offset # 0 because this returns max, indices
  724. # print(&#39;out2 shape&#39;, X.shape, X.dtype)
  725. X = X.unsqueeze(1)
  726. # print(&#39;preds&#39;, X)
  727. return X</code></pre>
  728. </details>
  729. </dd>
  730. </dl>
  731. </dd>
  732. <dt id="src.models.MaxConvLinear"><code class="flex name class">
  733. <span>class <span class="ident">MaxConvLinear</span></span>
  734. <span>(</span><span>num_timepoints=300, num_linear_filts=1, num_conv_filts=3, patch_size=9, kernel_size=30, nonlin=&lt;function relu&gt;, use_bias=False)</span>
  735. </code></dt>
  736. <dd>
  737. <section class="desc"><p>Takes input patch, uses linear filter to convert it to time series, then runs temporal conv, then takes max
  738. X: batch_size x H_patch x W_patch x time</p>
  739. <p>Initializes internal Module state, shared by both nn.Module and ScriptModule.</p></section>
  740. <details class="source">
  741. <summary>
  742. <span>Expand source code</span>
  743. </summary>
  744. <pre><code class="python">class MaxConvLinear(nn.Module):
  745. &#39;&#39;&#39;Takes input patch, uses linear filter to convert it to time series, then runs temporal conv, then takes max
  746. X: batch_size x H_patch x W_patch x time
  747. &#39;&#39;&#39;
  748. def __init__(self, num_timepoints=300, num_linear_filts=1, num_conv_filts=3, patch_size=9,
  749. kernel_size=30, nonlin=F.relu, use_bias=False):
  750. super(MaxConvLinear, self).__init__()
  751. self.fc1 = nn.Linear(patch_size * patch_size, num_linear_filts, bias=use_bias)
  752. self.conv1 = nn.Conv1d(in_channels=num_linear_filts, out_channels=num_conv_filts, kernel_size=kernel_size,
  753. bias=use_bias)
  754. self.offset = nn.Parameter(torch.Tensor([0]))
  755. def forward(self, X, **kwargs):
  756. s = X.shape # batch_size x H_patch x W_patch x time
  757. X = X.reshape(s[0], s[1] * s[2], s[3])
  758. X = torch.transpose(X, 1, 2)
  759. # print(&#39;in shape&#39;, X.shape, X.dtype)
  760. X = self.fc1(X) # .max(dim=-1)
  761. X = torch.transpose(X, 1, 2)
  762. X = self.conv1(X) # .max(dim=-1)
  763. # print(&#39;out shape&#39;, X.shape, X.dtype)
  764. # max over channels
  765. X = torch.max(X, dim=1)[0] # 0 because this returns max, indices
  766. # max over time step
  767. X = torch.max(X, dim=1)[0] # + self.offset # 0 because this returns max, indices
  768. # print(&#39;out2 shape&#39;, X.shape, X.dtype)
  769. X = X.unsqueeze(1)
  770. return X</code></pre>
  771. </details>
  772. <h3>Ancestors</h3>
  773. <ul class="hlist">
  774. <li>torch.nn.modules.module.Module</li>
  775. </ul>
  776. <h3>Methods</h3>
  777. <dl>
  778. <dt id="src.models.MaxConvLinear.forward"><code class="name flex">
  779. <span>def <span class="ident">forward</span></span>(<span>self, X, **kwargs)</span>
  780. </code></dt>
  781. <dd>
  782. <section class="desc"><p>Defines the computation performed at every call.</p>
  783. <p>Should be overridden by all subclasses.</p>
  784. <div class="admonition note">
  785. <p class="admonition-title">Note</p>
  786. <p>Although the recipe for forward pass needs to be defined within
  787. this function, one should call the :class:<code>Module</code> instance afterwards
  788. instead of this since the former takes care of running the
  789. registered hooks while the latter silently ignores them.</p>
  790. </div></section>
  791. <details class="source">
  792. <summary>
  793. <span>Expand source code</span>
  794. </summary>
  795. <pre><code class="python">def forward(self, X, **kwargs):
  796. s = X.shape # batch_size x H_patch x W_patch x time
  797. X = X.reshape(s[0], s[1] * s[2], s[3])
  798. X = torch.transpose(X, 1, 2)
  799. # print(&#39;in shape&#39;, X.shape, X.dtype)
  800. X = self.fc1(X) # .max(dim=-1)
  801. X = torch.transpose(X, 1, 2)
  802. X = self.conv1(X) # .max(dim=-1)
  803. # print(&#39;out shape&#39;, X.shape, X.dtype)
  804. # max over channels
  805. X = torch.max(X, dim=1)[0] # 0 because this returns max, indices
  806. # max over time step
  807. X = torch.max(X, dim=1)[0] # + self.offset # 0 because this returns max, indices
  808. # print(&#39;out2 shape&#39;, X.shape, X.dtype)
  809. X = X.unsqueeze(1)
  810. return X</code></pre>
  811. </details>
  812. </dd>
  813. </dl>
  814. </dd>
  815. <dt id="src.models.MaxLinear"><code class="flex name class">
  816. <span>class <span class="ident">MaxLinear</span></span>
  817. <span>(</span><span>input_dim=24300, num_units=20, nonlin=&lt;function relu&gt;, use_bias=False)</span>
  818. </code></dt>
  819. <dd>
  820. <section class="desc"><p>Takes flattened input and predicts it using many linear units
  821. X: batch_size x num_timepoints</p>
  822. <p>Initializes internal Module state, shared by both nn.Module and ScriptModule.</p></section>
  823. <details class="source">
  824. <summary>
  825. <span>Expand source code</span>
  826. </summary>
  827. <pre><code class="python">class MaxLinear(nn.Module):
  828. &#39;&#39;&#39;Takes flattened input and predicts it using many linear units
  829. X: batch_size x num_timepoints
  830. &#39;&#39;&#39;
  831. def __init__(self, input_dim=24300, num_units=20, nonlin=F.relu, use_bias=False):
  832. super(MaxLinear, self).__init__()
  833. self.fc1 = nn.Linear(input_dim, num_units, bias=use_bias)
  834. # self.offset = nn.Parameter(torch.Tensor([0]))
  835. def forward(self, X, **kwargs):
  836. # print(&#39;in shape&#39;, X.shape, X.dtype)
  837. X = self.fc1(X) # .max(dim=-1)
  838. # print(&#39;out shape&#39;, X.shape, X.dtype)
  839. X = torch.max(X, dim=1)[0] # 0 because this returns max, indices
  840. # print(&#39;out2 shape&#39;, X.shape, X.dtype)
  841. return X # + self.offset</code></pre>
  842. </details>
  843. <h3>Ancestors</h3>
  844. <ul class="hlist">
  845. <li>torch.nn.modules.module.Module</li>
  846. </ul>
  847. <h3>Methods</h3>
  848. <dl>
  849. <dt id="src.models.MaxLinear.forward"><code class="name flex">
  850. <span>def <span class="ident">forward</span></span>(<span>self, X, **kwargs)</span>
  851. </code></dt>
  852. <dd>
  853. <section class="desc"><p>Defines the computation performed at every call.</p>
  854. <p>Should be overridden by all subclasses.</p>
  855. <div class="admonition note">
  856. <p class="admonition-title">Note</p>
  857. <p>Although the recipe for forward pass needs to be defined within
  858. this function, one should call the :class:<code>Module</code> instance afterwards
  859. instead of this since the former takes care of running the
  860. registered hooks while the latter silently ignores them.</p>
  861. </div></section>
  862. <details class="source">
  863. <summary>
  864. <span>Expand source code</span>
  865. </summary>
  866. <pre><code class="python">def forward(self, X, **kwargs):
  867. # print(&#39;in shape&#39;, X.shape, X.dtype)
  868. X = self.fc1(X) # .max(dim=-1)
  869. # print(&#39;out shape&#39;, X.shape, X.dtype)
  870. X = torch.max(X, dim=1)[0] # 0 because this returns max, indices
  871. # print(&#39;out2 shape&#39;, X.shape, X.dtype)
  872. return X # + self.offset</code></pre>
  873. </details>
  874. </dd>
  875. </dl>
  876. </dd>
  877. <dt id="src.models.VideoNet"><code class="flex name class">
  878. <span>class <span class="ident">VideoNet</span></span>
  879. </code></dt>
  880. <dd>
  881. <section class="desc"><p>Base class for all neural network modules.</p>
  882. <p>Your models should also subclass this class.</p>
  883. <p>Modules can also contain other Modules, allowing to nest them in
  884. a tree structure. You can assign the submodules as regular attributes::</p>
  885. <pre><code>import torch.nn as nn
  886. import torch.nn.functional as F
  887. class Model(nn.Module):
  888. def __init__(self):
  889. super(Model, self).__init__()
  890. self.conv1 = nn.Conv2d(1, 20, 5)
  891. self.conv2 = nn.Conv2d(20, 20, 5)
  892. def forward(self, x):
  893. x = F.relu(self.conv1(x))
  894. return F.relu(self.conv2(x))
  895. </code></pre>
  896. <p>Submodules assigned in this way will be registered, and will have their
  897. parameters converted too when you call :meth:<code>to</code>, etc.</p>
  898. <p>:ivar training: Boolean represents whether this module is in training or
  899. evaluation mode.
  900. :vartype training: bool</p>
  901. <p>Initializes internal Module state, shared by both nn.Module and ScriptModule.</p></section>
  902. <details class="source">
  903. <summary>
  904. <span>Expand source code</span>
  905. </summary>
  906. <pre><code class="python">class VideoNet(nn.Module):
  907. def __init__(self):
  908. super(VideoNet, self).__init__()
  909. self.conv1 = nn.Conv2d(in_channels=1, out_channels=3, kernel_size=5)
  910. self.relu1 = nn.ReLU()
  911. self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=1)
  912. self.conv2 = nn.Conv2d(in_channels=3, out_channels=1, kernel_size=3)
  913. self.lstm = nn.LSTM(input_size=1, hidden_size=40, num_layers=1, batch_first=True)
  914. self.fc = nn.Linear(40, 1)
  915. # self.conv2 = nn.Conv1d(in_channels=H, out_channels=3, kernel_size=5)
  916. # self.maxpool2 = nn.MaxPool1d(kernel_size=2)
  917. # self.fc = nn.Linear(18 + p, 1) # this is hard-coded
  918. def forward(self, x):
  919. &#39;&#39;&#39;
  920. x: torch.Tensor
  921. (batch_size, time_steps, height, width)
  922. = (batch_size, 40, 10, 10)
  923. &#39;&#39;&#39;
  924. # print(&#39;in shape&#39;, x.shape)
  925. # extract features from each time_step separately
  926. # reshape time_steps and batch into same dim
  927. batch_size = x.shape[0]
  928. T = x.shape[1]
  929. x = x.reshape(batch_size * T, 1, x.shape[2], x.shape[3])
  930. x = self.conv1(x)
  931. x = self.relu1(x)
  932. x = self.maxpool1(x)
  933. x = self.conv2(x)
  934. x = torch.max(x, dim=3).values
  935. x = torch.max(x, dim=2).values
  936. # extract time_steps back out
  937. # run lstm on result 1D time series
  938. x = x.reshape(batch_size, T, 1)
  939. outputs, (h1, c1) = self.lstm(x) # get hidden vec
  940. h1 = h1.squeeze(0) # remove dimension corresponding to multiple layers / directions
  941. return self.fc(h1)</code></pre>
  942. </details>
  943. <h3>Ancestors</h3>
  944. <ul class="hlist">
  945. <li>torch.nn.modules.module.Module</li>
  946. </ul>
  947. <h3>Methods</h3>
  948. <dl>
  949. <dt id="src.models.VideoNet.forward"><code class="name flex">
  950. <span>def <span class="ident">forward</span></span>(<span>self, x)</span>
  951. </code></dt>
  952. <dd>
  953. <section class="desc"><p>x: torch.Tensor
  954. (batch_size, time_steps, height, width)
  955. = (batch_size, 40, 10, 10)</p></section>
  956. <details class="source">
  957. <summary>
  958. <span>Expand source code</span>
  959. </summary>
  960. <pre><code class="python"> def forward(self, x):
  961. &#39;&#39;&#39;
  962. x: torch.Tensor
  963. (batch_size, time_steps, height, width)
  964. = (batch_size, 40, 10, 10)
  965. &#39;&#39;&#39;
  966. # print(&#39;in shape&#39;, x.shape)
  967. # extract features from each time_step separately
  968. # reshape time_steps and batch into same dim
  969. batch_size = x.shape[0]
  970. T = x.shape[1]
  971. x = x.reshape(batch_size * T, 1, x.shape[2], x.shape[3])
  972. x = self.conv1(x)
  973. x = self.relu1(x)
  974. x = self.maxpool1(x)
  975. x = self.conv2(x)
  976. x = torch.max(x, dim=3).values
  977. x = torch.max(x, dim=2).values
  978. # extract time_steps back out
  979. # run lstm on result 1D time series
  980. x = x.reshape(batch_size, T, 1)
  981. outputs, (h1, c1) = self.lstm(x) # get hidden vec
  982. h1 = h1.squeeze(0) # remove dimension corresponding to multiple layers / directions
  983. return self.fc(h1)</code></pre>
  984. </details>
  985. </dd>
  986. </dl>
  987. </dd>
  988. </dl>
  989. </section>
  990. </article>
  991. <nav id="sidebar">
  992. <h1>Index</h1>
  993. <div class="toc">
  994. <ul></ul>
  995. </div>
  996. <ul id="index">
  997. <li><h3>Super-module</h3>
  998. <ul>
  999. <li><code><a title="src" href="index.html">src</a></code></li>
  1000. </ul>
  1001. </li>
  1002. <li><h3><a href="#header-classes">Classes</a></h3>
  1003. <ul>
  1004. <li>
  1005. <h4><code><a title="src.models.AttentionNet" href="#src.models.AttentionNet">AttentionNet</a></code></h4>
  1006. <ul class="">
  1007. <li><code><a title="src.models.AttentionNet.forward" href="#src.models.AttentionNet.forward">forward</a></code></li>
  1008. </ul>
  1009. </li>
  1010. <li>
  1011. <h4><code><a title="src.models.CNN" href="#src.models.CNN">CNN</a></code></h4>
  1012. <ul class="">
  1013. <li><code><a title="src.models.CNN.forward" href="#src.models.CNN.forward">forward</a></code></li>
  1014. </ul>
  1015. </li>
  1016. <li>
  1017. <h4><code><a title="src.models.FCNN" href="#src.models.FCNN">FCNN</a></code></h4>
  1018. <ul class="">
  1019. <li><code><a title="src.models.FCNN.forward" href="#src.models.FCNN.forward">forward</a></code></li>
  1020. </ul>
  1021. </li>
  1022. <li>
  1023. <h4><code><a title="src.models.LSTMNet" href="#src.models.LSTMNet">LSTMNet</a></code></h4>
  1024. <ul class="">
  1025. <li><code><a title="src.models.LSTMNet.forward" href="#src.models.LSTMNet.forward">forward</a></code></li>
  1026. </ul>
  1027. </li>
  1028. <li>
  1029. <h4><code><a title="src.models.MaxConv" href="#src.models.MaxConv">MaxConv</a></code></h4>
  1030. <ul class="">
  1031. <li><code><a title="src.models.MaxConv.forward" href="#src.models.MaxConv.forward">forward</a></code></li>
  1032. </ul>
  1033. </li>
  1034. <li>
  1035. <h4><code><a title="src.models.MaxConvLinear" href="#src.models.MaxConvLinear">MaxConvLinear</a></code></h4>
  1036. <ul class="">
  1037. <li><code><a title="src.models.MaxConvLinear.forward" href="#src.models.MaxConvLinear.forward">forward</a></code></li>
  1038. </ul>
  1039. </li>
  1040. <li>
  1041. <h4><code><a title="src.models.MaxLinear" href="#src.models.MaxLinear">MaxLinear</a></code></h4>
  1042. <ul class="">
  1043. <li><code><a title="src.models.MaxLinear.forward" href="#src.models.MaxLinear.forward">forward</a></code></li>
  1044. </ul>
  1045. </li>
  1046. <li>
  1047. <h4><code><a title="src.models.VideoNet" href="#src.models.VideoNet">VideoNet</a></code></h4>
  1048. <ul class="">
  1049. <li><code><a title="src.models.VideoNet.forward" href="#src.models.VideoNet.forward">forward</a></code></li>
  1050. </ul>
  1051. </li>
  1052. </ul>
  1053. </li>
  1054. </ul>
  1055. </nav>
  1056. </main>
  1057. <footer id="footer">
  1058. <p>Generated by <a href="https://pdoc3.github.io/pdoc"><cite>pdoc</cite> 0.7.2</a>.</p>
  1059. </footer>
  1060. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  1061. <script>hljs.initHighlightingOnLoad()</script>
  1062. </body>
  1063. </html>
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...