我有非常基本的线性回归样本。以下实施(不规范)
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)
我想用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!!!
我从头到尾手动检查了所有的计算。实际上是对的。但在产量上我看到了极大的差异!原因是什么?你知道吗
在成本函数中,您应该返回
而不是
val[0] / 2 * len(x)
。那你就有相关问题 更多 >
编程相关推荐