not-lain commited on
Commit
9c7dc56
1 Parent(s): bbd4f95

add step 2 and 3

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +83 -31
.gitignore CHANGED
@@ -1,2 +1,3 @@
1
  flagged/
2
  __pycache__/
 
 
1
  flagged/
2
  __pycache__/
3
+ test.ipynb
app.py CHANGED
@@ -32,7 +32,11 @@ def generate_example(dim1: list, dim2: list):
32
  except RuntimeError:
33
  out_shape = "error"
34
 
35
- return n1,dim1,n2,dim2,out_shape
 
 
 
 
36
 
37
 
38
  def sanitize_dimention(dim):
@@ -53,45 +57,93 @@ def sanitize_dimention(dim):
53
  )
54
  return out
55
 
56
- def generate_table(n1,dim1,n2,dim2,out_shape,n_dim):
57
- makdown_table = ""
58
- makdown_table += "| <!-- --> "*n_dim + "|\n" # header
59
- makdown_table += "|---"*n_dim + "|\n" # line
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  # tensor 1
62
- for index in range(n_dim):
63
- if index < (n_dim - n1):
64
- makdown_table += "| "
65
- else:
66
- makdown_table += f"| {dim1[index - (n_dim- n1)]}"
67
- makdown_table +="|\n"
68
  # tensor 2
69
- for index in range(n_dim):
70
- if index < (n_dim - n2):
71
- makdown_table += "| "
72
- else:
73
- makdown_table += f"| {dim2[index - (n_dim- n2)]}"
74
- makdown_table +="|\n"
75
- return makdown_table
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  def predict(dim1, dim2):
78
  dim1 = sanitize_dimention(dim1)
79
  dim2 = sanitize_dimention(dim2)
80
- n1,dim1,n2,dim2,out_shape = generate_example(dim1, dim2)
81
- code = EXAMPLE_MD.format(
82
- n1=str(n1), dim1=str(dim1), n2=str(n2), dim2=str(dim2), out_shape=str(out_shape)
83
- )
84
- n1 = len(dim1)
85
- n2 = len(dim2)
86
- n_dim = max(n1,n2)
87
- table1 = generate_table(n1,dim1,n2,dim2,out_shape,n_dim)
88
-
89
  # TODO
90
- # add code exmplanation here
91
-
92
-
 
 
 
 
 
 
 
 
93
  out = code
94
- out+= "\n# Step1 (alignment)" + "\n" + table1
 
 
 
 
95
  return out
96
 
97
 
 
32
  except RuntimeError:
33
  out_shape = "error"
34
 
35
+ code = EXAMPLE_MD.format(
36
+ n1=str(n1), dim1=str(dim1), n2=str(n2), dim2=str(dim2), out_shape=str(out_shape)
37
+ )
38
+
39
+ return dim1, dim2, code
40
 
41
 
42
  def sanitize_dimention(dim):
 
57
  )
58
  return out
59
 
 
 
 
 
60
 
61
+ def create_row(dim):
62
+ out = "| "
63
+ for i in dim:
64
+ out = out + str(i) + " | "
65
+ return out + "\n"
66
+
67
+
68
+ def create_header(n_dim, checks=None):
69
+ checks = ["<!-- -->"] * n_dim if checks is None else checks
70
+ out = "| "
71
+ for i in checks:
72
+ out = out + i + " | "
73
+ out += "\n" + "|---" * n_dim + "|\n"
74
+ return out
75
+
76
+
77
+ def generate_table(dim1, dim2, checks=None):
78
+ n_dim = len(dim1)
79
+ table = create_header(n_dim, checks)
80
  # tensor 1
81
+ table += create_row(dim1)
 
 
 
 
 
82
  # tensor 2
83
+ table += create_row(dim2)
84
+ return table
85
+
86
+
87
+ def alignment_and_fill_with_ones(dim1, dim2):
88
+ n_dim = max(len(dim1), len(dim2))
89
+
90
+ if len(dim1) == len(dim2):
91
+ pass
92
+ elif len(dim1) < len(dim2):
93
+ placeholder = [1] * (n_dim - len(dim1))
94
+ placeholder.extend(dim1)
95
+ dim1 = placeholder
96
+ else:
97
+ placeholder = [1] * (n_dim - len(dim2))
98
+ placeholder.extend(dim2)
99
+ dim2 = placeholder
100
+ return dim1, dim2
101
+
102
+ def check_validity(dim1,dim2):
103
+ if len(dim1) < 2:
104
+ return ["WIP"] * len(dim1)
105
+ out = []
106
+ for i in range(len(dim1)-2):
107
+ if dim1[i] == dim2[i]:
108
+ out.append("V")
109
+ else :
110
+ out.append("X")
111
+ # final dims
112
+ if dim1[-1] == dim2[-2]:
113
+ out.extend(["V","V"])
114
+ else :
115
+ out.extend(["X","X"])
116
+ return out
117
+
118
+
119
+ def substitute_ones_with_concat(dim1,dim2):
120
+ for i in range(len(dim1)-2):
121
+ dim1[i] = dim2[i] if dim1[i] == 1 else dim1[i]
122
+ dim2[i] = dim1[i] if dim2[i] == 1 else dim2[i]
123
+ return dim1, dim2
124
 
125
  def predict(dim1, dim2):
126
  dim1 = sanitize_dimention(dim1)
127
  dim2 = sanitize_dimention(dim2)
128
+ dim1, dim2, code = generate_example(dim1, dim2)
 
 
 
 
 
 
 
 
129
  # TODO
130
+ # fix for dims if one or both have dimensions is 1
131
+ # Table 1
132
+ dim1, dim2 = alignment_and_fill_with_ones(dim1, dim2)
133
+ table1 = generate_table(dim1, dim2)
134
+ # Table 2
135
+ dim1, dim2 = substitute_ones_with_concat(dim1,dim2)
136
+ table2 = generate_table(dim1, dim2)
137
+ # Table 3
138
+ checks = check_validity(dim1,dim2)
139
+ table3 = generate_table(dim1,dim2,checks)
140
+
141
  out = code
142
+ out += "\n# Step1 (alignment and pre_append with ones)\n" + table1
143
+ out += "\n# Step2 (susbtitute columns that have 1 with concat)\nexcept for last 2 dimensions\n" + table2
144
+ out += "\n# Step3 (check if matrix multiplication is valid)\n"
145
+ out += "* last dimension of dim1 should equal before last dimension of dim2\n"
146
+ out += "* all the other dimensions should be equal to one another\n\n" + table3
147
  return out
148
 
149