0

我正在尝试用 Python 而不是 matlab 解决 Andrew NG 的课程第 3 周作业,我发现 fmin_bfgs 与 ex2.m 文件中的 fminunc 类似

问题是,当我尝试将成本函数和计算梯度的函数输入 fmin_bfgs 时,它会返回一个回溯,说明:TypeError: 'numpy.ndarray' object is not callable。我很难尝试调试问题,因此将不胜感激

# code that produces the error:

results = opt.fmin_bfgs(costFunction.cost_function(weights, data_input, desired_output), weights,
                        fprime=costFunction.compute_grad(weights, data_input, desired_output), maxiter=400)

#the costFunction module:

import numpy as np
import sigmoid


def cost_function(theta, x, y):
    size = np.shape(x)
    z = np.dot(x, theta)
    h = sigmoid.sigmoid(z)
    j = (1 / size[0]) * (np.dot(-y, np.log(h)) - np.dot((1 - y), np.log(1 - h)))
    return j


def compute_grad(theta, x, y):
    size = np.shape(x)
    z = np.dot(x, theta)
    h = sigmoid.sigmoid(z)
    error = np.subtract(h, y)
    grad = (1 / size[0]) * np.dot(np.transpose(error), x)
    return grad
4

0 回答 0