使用numpy在python中编写L-BFGS最小化函数时出错



我正试图为对称矩阵的初始猜测创建一个L-BFGS最小化函数(以简化第一个hessian(。我使用的语言是python,我们可以使用的库是numpy。虽然可能还有其他我还不知道的错误,但我想帮助解决一个特定的错误,其中我的随机矩阵a(nxn(被解释为"str"。非常感谢。我有以下代码:

# -*- coding: utf-8 -*-
"""
Created on Sun Feb 23 16:33:22 2020
@author: 12064
"""
n = input("input the dimension of the function : ")
x_row = np.array(x)
x_column = np.transpose(x_row)
A = np.random.randn(n,n)
B = np.transpose(A)
f = np.matmul(B, A)
m = 5
k = 0
alpha = 1
epsilon = 10 ** -4
#define var's
gradf = np.gradient(f)
gradf_at_x = gradf.dot(x)
hessian_0 = np.gradient(gradf)
g[k] = gradf.dot(x[k])
condition_1 = g[k].dot(g[k])
p_T[k] = np.array(n)
p[k] = np.transpose(p_T)
#index an array
memory1 = np.linspace(change_g[k-m], change_g[k])#
memory2 = np.linspace(change_x[k-m], change_x[k])
memory3 = np.linspace(alpha[k-m], alpha[k])
memory4 = np.linspace(p[k-m], p[k])
memory5 = np.linspace(x[k-m], x[k+1])
memory6 = np.linspace(g[k-m], g[k])

def myminimizer(x, f):
if np.sqrt(condition_1) < epsilon:
#magnitude of the gradient (square root of dot product of grad f at x'k) <= epsilon (10^-4)
return x, k
else: 
if k < 1:
p[k] = -1 * (hessian_0 * gradf_at_x)
else:
p = -1 * r
omega = f.dot(x[k] + alpha[k]*p[k])
while 1:
alpha = r * alpha
x[k+1] = x[k] + alpha[k] * p[k]
change_x = x[k+1] - x[k]
change_g = g[k+1] - g[k]
q[k] = g[k]
for i in range(k-m, k):
alpha[i] = p[i] * change_x_transpose * q[i]
q = q - alpha[i] * change_g[i]
r = H[k] * q
for i in range (k-m, k):
Beta[i] = p[i] * change_g_transpose[i] * r[i]
r = r + change_x * (alpha[i] - Beta[i]) #check that its Beta[i]
k += 1
if np.sqrt(condition_1) < epsilon:
#magnitude of the gradient (square root of dot product of grad f at x'k) <= epsilon (10^-4)
print ("x, k")
print ("x is your omtimizer of f")
else: 
if k < 1:
p[k] = -1 * (hessian_0 * gradf_at_x)
else:
p = -1 * r
omega = f.dot(x[k] + alpha[k]*p[k])
while 1:
alpha = r * alpha
x[k+1] = x[k] + alpha[k] * p[k]
change_x = x[k+1] - x[k]
change_g = g[k+1] - g[k]
q[k] = g[k]
for i in range(k-m, k):
alpha[i] = p[i] * change_x_transpose * q[i]
q = q - alpha[i] * change_g[i]
r = H[k] * q
for i in range (k-m, k):
Beta[i] = p[i] * change_g_transpose[i] * r[i]
r = r + change_x * (alpha[i] - Beta[i]) #check that its Beta[i]
k += 1
#END CODE#

我的错误消息是:

runfile('C:/Users/12064/Desktop/Duncan/assignments/ass 4 4realsies.py', wdir='C:/Users/12064/Desktop/Duncan/assignments')
input the dimension of the function : 10
Traceback (most recent call last):
File "<ipython-input-41-e748654944e8>", line 1, in <module>
runfile('C:/Users/12064/Desktop/Duncan/assignments/ass 4 4realsies.py', wdir='C:/Users/12064/Desktop/Duncan/assignments')
File "C:Users12064Anaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:Users12064Anaconda3libsite-packagesspyder_kernelscustomizespydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/12064/Desktop/Duncan/assignments/ass 4 4realsies.py", line 11, in <module>
A = np.random.randn(n,n)
File "mtrand.pyx", line 1425, in mtrand.RandomState.randn
File "mtrand.pyx", line 1555, in mtrand.RandomState.standard_normal
File "mtrand.pyx", line 167, in mtrand.cont0_array
TypeError: 'str' object cannot be interpreted as an integer

任何关于我试图创建的矩阵为什么会导致这个错误的说明都将不胜感激!

将输入字符串转换为整数:

n = int(input("input the dimension of the function : "))

最新更新