Я пытаюсь решить задачу 3-го курса Эндрю Н.Г. в Python вместо matlab, и обнаружил, что fmin_bfgs аналогично fminunc в файле ex2.m
Проблема в том, что когда я пытаюсь ввести функцию стоимости и функцию, которая вычисляет градиенты в fmin_bfgs, она возвращает трассировку, говорящую, что:
Ошибка типа: объект 'numpy.ndarray' не вызывается.
Я с трудом пытаюсь отладить проблему, поэтому любая помощь будет признательна
# 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