即时通讯尝试实现使用Numpy的渐变下降算法。后面的内容是避免算法中的任何循环,所以我使用矩阵和numpy.dot来进行计算。我敢肯定,数学的,但每当我试图通过这个功能我得到一个错误信息:使用numpy的渐变下降机器学习
def grad(feature_matrix, output, initial_weights, step_size, tolerance):
converged = False
w = np.array(initial_weights) # make sure it's a numpy array
X=feature_matrix
y=output
s=step_size
t=0
RSS=0
J=[]
while not converged:
y_h=np.dot(X,w)
e=y-_yh
w=w+s*2*np.dot(np.transpose(X),e)
gradient_magnitude=sqrt(np.dot(np.transpose(X),e)
RSS=np.dot(e,e)
J.append(RSS)
t=t+1
if gradient_magnitude < tolerance:
converged = True
return(weights,J,t)
我总是得到这个错误:
File "<ipython-input-14-db210106141b>", line 15
RSS=np.dot(e,e)
^
SyntaxError: invalid syntax
如果删除RSS = np.dot( e,e)行并尝试通过该函数,然后我得到:
File "<ipython-input-15-b0b1a5aebd0c>", line 16
J.append(RSS)
^
SyntaxError: invalid syntax
它似乎是与函数结构的东西。 这可能是显而易见的,我很想念,但我一直在寻找这个功能三天,看着其他的例子,我只是找不到错误。
请帮忙!
提示:如果您删除线和错误仍然存在这不是删除线这是错误的。在 – miraculixx