我有一个简单的代码,我正在尝试在0到2pi的间隔上对正弦进行线性回归,我试图通过线性回归来确定正弦的泰勒展开系数,但是当我将5阶项相加时,它得到的是nan值进行梯度下降时保持体重
它给我这样的错误 result
可能是什么原因造成的?我曾尝试过初始化很小的权重,但是没有成功,但是当我将学习率降至e-9时,它开始执行某些操作,但是过程非常缓慢
import numpy as np
#import matplotlib.pyplot as plt
class Linear_Regression(object):
def __init__(self):
return None
def fit(self, X,
Y,
learning_rate = 0.000001,
epoch = 5000,
momentum = 0,
print_period = 250):
#if print period is 0 it never prints
#dont forget that X should come with a column of 1's (constant)
#this ill be equivalent to bias
#cofficient will be automatically calcualated by gradiend descent
cost_array = []
N, M = X.shape
W = np.random.randn(M)
for step in range(0, epoch):
Y_pred = X.dot(W)
derivative = X.T.dot(Y_pred - Y)
W = W - momentum*W - learning_rate*derivative
if step % (print_period) == 0:
#this if else statement ensures that if print period is
cost = error_rate_ish_linear(Y_pred, Y)
cost_array.append(cost)
print('iterateion', str(step))
print(cost)
self.W = W
return X.dot(W), cost_array
def predict(self, X):
Weights = self.W
return X.dot(Weights)
def error_rate_ish_linear(Y, Yhat):
dif = np.abs(Y - Yhat)
return dif.sum()
x1 = np.linspace(0, 2*np.pi, 30).reshape(-1,1)
y = np.sin(x1)
#x = np.append(x1, bias, axis = 1)
x = np.append(x1, x1**2, axis = 1)
x = np.append(x, x1**3, axis = 1)
x = np.append(x, x1**4, axis = 1)
x = np.append(x, x1**5, axis = 1)
#x = np.append(x, x1**6, axis = 1)
#x = np.append(x, x1**7, axis = 1)
#x = np.append(x, x1**8, axis = 1)
y = y.flatten()
linar = Linear_Regression()
zaza = linar.fit(x,y, epoch = 10000, learning_rate = 1e-5, print_period = 250)
gela = linar.predict(x)