Я реализовал логику для ? (?) и немного запутался в моей логике.Как рассчитать стоимость ? (?) и градиентного спуска?
Ниже приведен код.
def compute_cost(X, y, theta):
m = len(y)
inner = np.power(((X @ theta.T) - y), 2)
return np.sum(inner) / (2 * m)
J = -(2.0/size) * np.dot(inner,X)
return J
theta = np.zeros( (2, 1) )
print compute_cost(X, y, theta)
theta = np.array([[1.0], [1.0]])
print compute_cost(X, y, theta)
def gradient_descent(X, y, theta, alpha, num_iters):
#Trying to understand what goes here?
# initialize some useful values
m = len(y) # number of training examples
J_history = np.zeros( (num_iters, 1) ) # an array of the history of
#our J cost function
J_history[0] = compute_cost(X, y, theta) # the initial cost
# perform num_iters iterations of the gradient descent
for iter in range(1, num_iters):
J_history[iter] = compute_cost(X, y, theta)
return (theta, J_history)
theta = np.zeros( (2, 1) )
iterations = 1500;
alpha = 0.01
theta, J_history = gradient_descent(X, y, theta, alpha, iterations)
#Expected result should be like:
print theta
>>> [[-3.62981201] [ 1.16631419]]
print J_history
>>> [[ 32.07273388]
[ 6.73719046]
[ 5.93159357]
...,
[ 4.4834581 ]
[ 4.48343473]
[ 4.48341145]]
И я получаю следующую ошибку: ValueError: shape (2,97) и (1,2) не выровнены: 97 (dim1)! = 1 (дим 0)