【问题标题】:error while writing an L-BFGS minimizing function in python using numpy使用 numpy 在 python 中编写 L-BFGS 最小化函数时出错
【发布时间】:2020-02-24 07:59:29
【问题描述】:

我正在尝试为对称矩阵的初始猜测创建一个 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】:

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

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

    【讨论】:

    • 更多关于代码 sn-p 的上下文会很好。例如。 “input()-函数会将用户的输入作为字符串返回,因此应检查字符串是否为数字(例如通过str.is_digit()),然后通过将其显式转换为整数将其作为数字保存到变量中。- - 这也将有助于未来的读者理解问题、重现问题并理解解决步骤。
    猜你喜欢
    • 1970-01-01
    • 2015-02-19
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多