Initial commit.
authorFrancois Fleuret <francois@fleuret.org>
Sun, 6 Jun 2021 12:22:08 +0000 (14:22 +0200)
committerFrancois Fleuret <francois@fleuret.org>
Sun, 6 Jun 2021 12:22:08 +0000 (14:22 +0200)
conv_chain.py [new file with mode: 0755]

diff --git a/conv_chain.py b/conv_chain.py
new file mode 100755 (executable)
index 0000000..04dfdfa
--- /dev/null
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+
+import torch
+from torch import nn
+
+######################################################################
+
+def conv_chain(input_size, output_size, depth, cond):
+    if depth == 0:
+        if input_size == output_size:
+            return [ [ ] ]
+        else:
+            return [ ]
+    else:
+        r = [ ]
+        for kernel_size in range(1, input_size + 1):
+            for stride in range(1, input_size + 1):
+                if cond(kernel_size, stride):
+                    n = (input_size - kernel_size) // stride
+                    if n * stride + kernel_size == input_size:
+                        q = conv_chain(n + 1, output_size, depth - 1, cond)
+                        r += [ [ (kernel_size, stride) ] + u for u in q ]
+        return r
+
+######################################################################
+
+# Example
+
+c = conv_chain(
+    input_size = 64, output_size = 8,
+    depth = 5,
+    cond = lambda k, s: k <= 4 and s <= 2 and s <= k//2
+)
+
+x = torch.rand(1, 1, 64)
+
+for m in c:
+    m = nn.Sequential(*[ nn.Conv1d(1, 1, l[0], l[1]) for l in m ])
+    print(m)
+    print(x.size(), m(x).size())
+
+######################################################################