Reputation: 5788
I have very basic sample of Linear Regression. Implementation below (without Regularization)
class Learning:
def assume(self, weights, x):
return np.dot(x, np.transpose(weights))
def cost(self, weights, x, y, lam):
predict = self.assume(weights, x) \
.reshape(len(x), 1)
val = np.sum(np.square(predict - y), axis=0)
assert val is not None
assert val.shape == (1,)
return val[0] / 2 * len(x)
def grad(self, weights, x, y, lam):
predict = self.assume(weights, x)\
.reshape(len(x), 1)
val = np.sum(np.multiply(
x, (predict - y)), axis=0)
assert val is not None
assert val.shape == weights.shape
return val / len(x)
And I want to check gradient, that it works, with scipy.optimize
.
learn = Learning()
INPUTS = np.array([[1, 2],
[1, 3],
[1, 6]])
OUTPUTS = np.array([[3], [5], [11]])
WEIGHTS = np.array([1, 1])
t_check_grad = scipy.optimize.check_grad(
learn.cost, learn.grad, WEIGHTS,INPUTS, OUTPUTS, 0)
print(t_check_grad)
# Output will be 73.2241602235811!!!
I manually checked all computation from start to end. And it's actually right implementatino. But in output I see extremely big difference! What is the reason?
Upvotes: 2
Views: 91
Reputation: 23101
In your cost function you should return
val[0] / (2 * len(x))
instead of val[0] / 2 * len(x)
. Then you will have
print(t_check_grad)
# 1.20853633278e-07
Upvotes: 2