在优化函数scipy中动态创建变量

时间:2018-08-31 14:58:40

标签: python scipy mathematical-optimization tensor

下面的代码用于找到用三个矩阵a,b,c近似逼近张量展开的最小误差。使用变量a,b,c进行优化。

我是优化案例的新手,所以请帮助我理解这一点。我的查询是:

  1. 在f内,如何在每次迭代中使用a,b,c值。
  2. 如何为每次迭代使用a,b,c为khatri_rao产品创建变量。
  3. 对于每次迭代,params[0]/[1]/[2]的值都不是2D数组值。甚至没有看到初始值?我的代码错误谈论哪个元组?

感谢您的帮助。谢谢

import scipy.optimize as optimize
import numpy as np
import sys
sys.path.append("../tensorly-master/")
import tensorly as tl

def f(params, t0, t1, t2):#arguments are tensor unfold
    a, b, c= params[0], params[1], params[2]#multi variables in optimization
    #norm of tensor unfold mode0 minus "a" matrix multiplied with transpose of ' khatri rao product of "c" & "b" '
    value0= t0 - np.matmul(a, tl.tenalg.khatri_rao([c, b], reverse=False).T)
    value1= t1 - np.matmul(b, tl.tenalg.khatri_rao([a, c], reverse=False).T)
    value2= t2 - np.matmul(c, tl.tenalg.khatri_rao([b, a], reverse=False).T)
    #sum of all norms
    values=np.linalg.norm(value0, "fro")+np.linalg.norm(value1, "fro")+np.linalg.norm(value2, "fro")
    #optimizing the sum of all norms be minimum
    return values

#randomly initialinzing tensor , three arrays and unfolding tensor
tn=np.random.uniform(low=0, high=100, size=(3,3,3))
a=np.random.uniform(low=0, high=100, size=(3,2))
b=np.random.uniform(low=0, high=100, size=(3,2))
c=np.random.uniform(low=0, high=100, size=(3,2))
t0=tl.unfold(tn, 0)
t1=tl.unfold(tn, 1)
t2=tl.unfold(tn, 2)
#optimization
result=optimize.minimize(f, [a, b, c], args=(t0, t1, t2))
if result.success:
    fitted_params = result.x
    print(fitted_params)
else:
    raise ValueError(result.message)

错误是:

error:-
Using numpy backend.
Traceback (most recent call last):
  File "stc.py", line 27, in <module>
    result=optimize.minimize(f, [a, b, c], args=(t0, t1, t2))
  File "/home/manish/.local/lib/python2.7/site-packages/scipy/optimize/_minimize.py", line 597, in minimize
    return _minimize_bfgs(fun, x0, args, jac, callback, **options)
  File "/home/manish/.local/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 963, in _minimize_bfgs
    gfk = myfprime(x0)
  File "/home/manish/.local/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 293, in function_wrapper
    return function(*(wrapper_args + args))
  File "/home/manish/.local/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 723, in approx_fprime
    return _approx_fprime_helper(xk, f, epsilon, args=args)
  File "/home/manish/.local/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 657, in _approx_fprime_helper
    f0 = f(*((xk,) + args))
  File "/home/manish/.local/lib/python2.7/site-packages/scipy/optimize/optimize.py", line 293, in function_wrapper
    return function(*(wrapper_args + args))
  File "stc.py", line 10, in f
    value0= t0 - np.matmul(a, tl.tenalg.khatri_rao([c, b], reverse=False).T)
  File "../tensorly-master/tensorly/tenalg/_khatri_rao.py", line 70, in khatri_rao
    n_columns = matrices[0].shape[1]
IndexError: tuple index out of range

1 个答案:

答案 0 :(得分:0)

optimize通过传递一维数组(形状(n,))来调用f函数,即使给定的初始猜测不是该形状(例如,参见_minimize_bfgs) 。您可以使用reshapenumpy.split从一维数组a中重建正确的二维数组bcparams

import scipy.optimize as optimize
import numpy as np
import tensorly as tl

def f(params, t0, t1, t2): # arguments are tensor unfold
    a, b, c = np.split(x0.reshape(3, 6), 3, axis=1)  # unpack the variables

    # norm of tensor unfold mode0 minus "a" matrix 
    # multiplied with transpose of ' khatri rao product of "c" & "b" '
    value0= t0 - np.matmul(a, tl.tenalg.khatri_rao([c, b], reverse=False).T)
    value1= t1 - np.matmul(b, tl.tenalg.khatri_rao([a, c], reverse=False).T)
    value2= t2 - np.matmul(c, tl.tenalg.khatri_rao([b, a], reverse=False).T)
    #sum of all norms
    values = np.linalg.norm(value0, "fro") + \
             np.linalg.norm(value1, "fro") + \
             np.linalg.norm(value2, "fro")

    # optimizing the sum of all norms be minimum
    return values

# randomly initializing tensor, three arrays and unfolding tensor
tn = np.random.uniform(low=0, high=100, size=(3,3,3))
t0 = tl.unfold(tn, 0)
t1 = tl.unfold(tn, 1)
t2 = tl.unfold(tn, 2)

# Initial guess :
a = np.random.uniform(low=0, high=100, size=(3,2))
b = np.random.uniform(low=0, high=100, size=(3,2))
c = np.random.uniform(low=0, high=100, size=(3,2))

x0 = np.hstack([a, b, c]).ravel()

# optimization
result = optimize.minimize(f, x0, args=(t0, t1, t2))

if result.success:
    fitted_params = result.x
    print(fitted_params)
else:
    raise ValueError(result.message)