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

问题描述 投票:0回答:1

我正在尝试为对称矩阵的初始猜测创建L-BFGS最小化函数(以简化采用第一个粗麻布的方法)。我使用的语言是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:\Users\12064\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\12064\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.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

关于为什么我要创建的矩阵导致此错误的任何说明,都将不胜感激!

python numpy minimization
1个回答
0
投票

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

n = int(input("input the dimension of the function : "))
© www.soinside.com 2019 - 2024. All rights reserved.