我在scikit-tensor中使用以下代码进行parafac分解。此代码是scikit-tensor的示例。
from sktensor import dtensor, cp_als, parafac2, tucker_hooi
import numpy
import sktensor
T=dtensor(numpy.arange(100).reshape(2, 5,10))
print (type(T))
P, F, D, A, fit, itr, exectimes = parafac2.parafac2(T, 3, init=3, ma_iter=5, conv= 4)
当我运行此代码时,输出为......
Traceback (most recent call last):
File "C:/Users/meghdad/PycharmProjects/tensorInPython/dtensor1.py", line 17, in <module>
P, F, D, A, fit, itr, exectimes = parafac2.parafac2(T, 3, init=3, ma_iter=5, conv= 4)
File "C:\Anaconda3\lib\site-packages\scikit_tensor-0.1-py3.5.egg\sktensor\parafac2.py", line 50, in parafac2
File "C:\Anaconda3\lib\site-packages\scikit_tensor-0.1-py3.5.egg\sktensor\parafac2.py", line 113, in __init
UnboundLocalError: local variable 'F' referenced before assignment
如何解决此错误?
答案 0 :(得分:1)
我查看版本0.1的source code。 &#34; init&#34;的唯一有效值关键字是&#34; nvecs&#34;或&#34;随机&#34;。默认值为&#34; nvecs&#34;。如果您尝试其中任何一种,您将摆脱错误:
P, F, D, A, fit, itr, exectimes = parafac2.parafac2(T, 3, init='nvecs', ma_iter=5, conv= 4)
或者
P, F, D, A, fit, itr, exectimes = parafac2.parafac2(T, 3, init='random', ma_iter=5, conv= 4)