-
Notifications
You must be signed in to change notification settings - Fork 3
/
npz.py
236 lines (168 loc) · 5.16 KB
/
npz.py
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
#!/usr/bin/env python
"""
Motivation: Tensor (aka Kronecker, aka outer) product works like this
C = A * B
ij..pq.. ij.. pq..
e.g. all products of elements of rank-2 tensor A
ij
with all elements of rank-3 tensor B form a rank-5 tensor C .
pqr ijpqr
Most notably products with rank-0 tensors (numbers with no indices)
does not increase the tensor rank. In this respect the numpy.outer()
behaves counter intuitive, e.g. numpy.outer(2, 3) == array([[6]]),
is a rank-2 array (albeit with both index ranges limited to one value).
Matrix product can be viewed as a tensor product followed
by contraction over innermost indices, e.g.
C = A * B (sum over repeated k)
mn mk kn
The contraction step can be extended to several indices.
In |matmul| we restrict ourselves to contracting over
several INNERMOST indices. This differs from the conventions
used by dot- and inner product funcitons in NumPy.
See also:
numpy.outer
numpy.kron
numpy.dot
numpy.inner
numpy.matrixmultiply
"""
__all__ = ["matmul", "outer", "dots", "sums"]
from numpy import asarray, empty, shape, dot, sum, transpose
def prod(ns): # name clash with numpy.prod
return reduce(lambda x, y: x * y, ns, 1)
def outer(A, B):
"""Outer product, differs from numpy version for special cases
of scalar arguments:
>>> outer([2, 3], [10, 100])
array([[ 20, 200],
[ 30, 300]])
>>> outer([2, 3], 10)
array([20, 30])
>>> outer(10, [2, 3])
array([20, 30])
"""
return matmul(shape(A), shape(B), (), A, B)
def sums(m, n, k, A):
"""S(m, n) = SUM(k) A[m, k, n]
>>> from numpy import ones, all
>>> a = ones((2,3,4,5,6))
>>> c = sums((2,3), (), (4,5,6), a)
>>> shape(c)
(2, 3)
>>> c[0,0]
120.0
>>> c = sums((), (5,6), (2,3,4), a)
>>> shape(c)
(5, 6)
>>> c[0,0]
24.0
>>> all( a == sums((2,3), (4,5,6), (), a) )
True
"""
A = asarray(A)
assert shape(A) == m + k + n
M = prod(m)
N = prod(n)
K = prod(k)
A.shape = (M, K, N)
C = sum(A, axis=1)
A.shape = m + k + n
C.shape = m + n
return C
def dots(m, n, k, A, B):
"""C[m, n] = A[m, k] * B[m, k, n] (sum over repeated k, not over m)
>>> from numpy import ones
>>> a = ones((2,3,4,5,6))
>>> b = ones((2,3,4,5,6,3,2))
>>> c = dots((2,3,4), (3,2), (5,6), a, b)
>>> shape(c)
(2, 3, 4, 3, 2)
>>> c[0,0,0,0,0]
30.0
Can this be done with vectorize() ?
"""
A = asarray(A)
B = asarray(B)
assert shape(A) == m + k
assert shape(B) == m + k + n
M = prod(m)
N = prod(n)
K = prod(k)
# these are two different views of the same read-only data:
A = A.reshape(M, K)
B = B.reshape(M, K, N)
C = empty((M, N))
# FIXME: how to avoid copying?
for i in xrange(M):
C[i, :] = dot(A[i], B[i])
# A.shape = m + k
# B.shape = m + k + n
C.shape = m + n
return C
def matmul(m, n, k, A, B, transA=False, transB=False):
"""Specialized dot(), inspired by DGEMM
>>> from numpy import zeros
>>> a = zeros((2, 3, 4, 5))
>>> b = zeros((3, 4, 5, 9))
>>> c = matmul( (2,), (9,), (3, 4, 5), a, b)
>>> shape(c)
(2, 9)
>>> c = matmul( (), (3, 4, 5, 9), (), 5.0, b)
>>> shape(c)
(3, 4, 5, 9)
"""
# print "m, n, k =", m, n, k
# sizes:
M = prod(m)
K = prod(k)
N = prod(n)
# print "M, N, K =", M, N, K
# print "matmul: A, B =", A, B, type(A), type(B)
A = asarray(A)
B = asarray(B)
assert A.size == M * K
assert B.size == K * N
if transA:
assert A.shape == k + m
# a different view of the read-only data:
A = A.reshape(K, M)
# transposed view of A:
opA = transpose(A)
else:
assert A.shape == m + k
# a different view of the read-only data:
A = A.reshape(M, K)
# alias:
opA = A
if transB:
assert B.shape == n + k
# a different view of the read-only data:
B = B.reshape(N, K)
# transposed view of B:
opB = transpose(B)
else:
assert B.shape == k + n
# a different view of the read-only data:
B = B.reshape(K, N)
# alias:
opB = B
C = dot(opA, opB)
# reshape back:
# A.shape = m + k
# B.shape = k + n
C.shape = m + n
# print "matmul: C =", C, type(C)
# # FIXME: this is ugly, but to make doctests succed
# # we treat the case of scalar C somewhat special:
# if C.shape == ():
# # 1) we got a numpy scalar, tough input A and B were
# # probably plain python scalars, return plain python:
# # 2) dot(vecA, vecB) returns
# C = C.item()
# NO, this is indeed ugly and broken!
return C
# python npx.py [-v]:
if __name__ == "__main__":
import doctest
doctest.testmod()
# Default options for vim:sw=4:expandtab:smarttab:autoindent:syntax