projects
/
pytorch.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Update.
[pytorch.git]
/
eingather.py
diff --git
a/eingather.py
b/eingather.py
index
f8b3e89
..
588aacb
100755
(executable)
--- a/
eingather.py
+++ b/
eingather.py
@@
-15,52
+15,41
@@
def eingather(op, src, *indexes):
s_indexes = re.findall("\(([^)]*)\)", s_src)
s_src = re.sub("\([^)]*\)", "_", s_src)
s_indexes = re.findall("\(([^)]*)\)", s_src)
s_src = re.sub("\([^)]*\)", "_", s_src)
- shape = tuple(src.size(s_src.index(v)) for v in s_dst)
-
- idx = []
- n_index = 0
-
- for i in range(src.dim()):
- v = s_src[i]
- if v == "_":
- index, s_index = indexes[n_index], s_indexes[n_index]
- n_index += 1
-
- sub_idx = []
-
- for i in range(index.dim()):
- v = s_index[i]
+ all_sizes = tuple(d for s in ( src, ) + indexes for d in s.size())
+ s_all = "".join([s_src] + s_indexes)
+ shape = tuple(all_sizes[s_all.index(v)] for v in s_dst)
+
+ def do(x,s_x):
+ idx = []
+ n_index = 0
+
+ for i in range(x.dim()):
+ v = s_x[i]
+ if v == "_":
+ idx.append(do(indexes[n_index], s_indexes[n_index]))
+ n_index += 1
+ else:
j = s_dst.index(v)
a = (
j = s_dst.index(v)
a = (
- torch.arange(
inde
x.size(i))
+ torch.arange(x.size(i))
.reshape((1,) * j + (-1,) + (1,) * (len(s_dst) - j - 1))
.expand(shape)
)
.reshape((1,) * j + (-1,) + (1,) * (len(s_dst) - j - 1))
.expand(shape)
)
- sub_idx.append(a)
-
- index = index[sub_idx]
- idx.append(index)
- else:
- j = s_dst.index(v)
- a = (
- torch.arange(src.size(i))
- .reshape((1,) * j + (-1,) + (1,) * (len(s_dst) - j - 1))
- .expand(shape)
- )
- idx.append(a)
+ idx.append(a)
-
return src
[idx]
+
return x
[idx]
+ return do(src,s_src)
#######################
#######################
-src = torch.rand(3, 5, 7, 11
, 13
)
-index1 = torch.randint(src.size(2), (src.size(
4), src.size(1), src.size(4
)))
+src = torch.rand(3, 5, 7, 11)
+index1 = torch.randint(src.size(2), (src.size(
3), src.size(1), src.size(3
)))
index2 = torch.randint(src.size(3), (src.size(1),))
# I want result[a, c, e] = src[c, a, index1[e, a, e], index2[a], e]
index2 = torch.randint(src.size(3), (src.size(1),))
# I want result[a, c, e] = src[c, a, index1[e, a, e], index2[a], e]
-result = eingather("ca(eae)(a)
e
-> ace", src, index1, index2)
+result = eingather("ca(eae)(a) -> ace", src, index1, index2)
# Check
# Check
@@
-69,6
+58,6
@@
error = 0
for a in range(result.size(0)):
for c in range(result.size(1)):
for e in range(result.size(2)):
for a in range(result.size(0)):
for c in range(result.size(1)):
for e in range(result.size(2)):
- error += (result[a, c, e] - src[c, a, index1[e, a, e], index2[a]
, e
]).abs()
+ error += (result[a, c, e] - src[c, a, index1[e, a, e], index2[a]]).abs()
print(error.item())
print(error.item())