Forgive me for yet another question on Python decorators. I did read through many of them, but I wonder what the best solution to the specific following problem is.
I have written several functions that do some form of gradient descent in numpy/scipy. Given a matrix X, I try to iteratively minimize some distance, d(X, AS), as functions of A and S. Each algorithm follows the same basic procedure, but each has a different update rule. For example, here were two of my functions (note the only difference is in the update rule):
def algo1(X, A=None, S=None, K=2, maxiter=10, c=0.1):
M, N = X.shape
if A is None:
A = matrix(rand(M, K))
if S is None:
S = matrix(rand(K, N))
for iter in range(maxiter):
# Begin update rule.
A = multiply(A, (X*S.T + c)/(A*S*S.T + c))
S = multiply(S, (A.T*X + c)/(A.T*A*S + c))
# End update rule.
for k in range(K):
na = norm(A[:,k])
A[:,k] /= na
S[k,:] *= na
return A, S
... and the other:
def algo2(X, A=None, S=None, K=2, maxiter=10, c=0.1):
M, N = X.shape
O = matrix(ones([M, N]))
if A is None:
A = matrix(rand(M, K))
if S is None:
S = matrix(rand(K, N))
for iter in range(maxiter):
# Begin update rule.
A = multiply(A, ((X/(A*S))*S.T + c)/(O*S.T + c))
S = multiply(S, (A.T*(X/(A*S)) + c)/(A.T*O + c))
# End update rule.
for k in range(K):
na = norm(A[:,k])
A[:,k] /= na
S[k,:] *= na
return A, S
Both functions are successful on their own. Obviously, these functions are asking to be refactored. The unit of code that differs is the update rule. So here is my attempt at refactoring:
@iterate
def algo1(X, A=None, S=None, K=2, maxiter=10, c=0.1):
A = multiply(A, (X*S.T + c)/(A*S*S.T + c))
S = multiply(S, (A.T*X + c)/(A.T*A*S + c))
@iterate
def algo2(X, A=None, S=None, K=2, maxiter=10, c=0.1):
A = multiply(A, ((X/(A*S))*S.T + c)/(O*S.T + c))
S = multiply(S, (A.T*(X/(A*S)) + c)/(A.T*O + c))
Here are some potential function calls:
A, S = algo1(X)
A, S = algo1(X, A0, S0, maxiter=50, c=0.2)
A, S = algo1(X, K=10, maxiter=40)
Questions:
- What technique is best suited for refactoring this code? Function decorators?
- If so, how would you write
iterate
? What confuses me, in particular, are the arguments/parameters, e.g., with vs. without default values, accessing them in the decorator and "wrapper", etc. For example, the update rules themselves do not requireK
, but the initialization code does, so I wonder if my function signatures are correct.
EDIT: Thank you for the help. More questions:
- Is it true that a wrapper (e.g.,
inner
) is only necessary when parameters are being passed? Because I see decorator examples without wrappers, and no parameters are passed, and they work just fine. - From reading the Python docs some more,
functools
appears useful; is its main purpose to preserve the metadata of the original function (e.g.,algo1.__name__
andalgo1.__doc__
)? - With the signatures
def algo1(X, A, S, c)
anddef inner(X, A=None, S=None, K=2, maxiter=10, c=0.1)
, the callalgo1(X, maxiter=20)
still works. Syntactically, I'm not sure why that is. For learning purposes, could you clarify (or cite a reference)? Thanks!