我正在尝试对葡萄酒数据集实施多元线性回归。但是当我将 Pytorch 的结果与 Python 的临时代码进行比较时,损失并不相同。
我的暂存代码:
功能:
def yinfer(X, beta):
return beta[0] + np.dot(X,beta[1:])
def cost(X, Y, beta):
sum = 0
m = len(Y)
for i in range(m):
sum = sum + ( yinfer(X[i],beta) - Y[i])*(yinfer(X[i],beta) - Y[i])
return sum/(1.0*m)
Run Code Online (Sandbox Code Playgroud)
主要代码:
alpha = 0.005
b=[0,0.04086357 ,-0.02831656 ,0.09622949 ,-0.15162516 ,0.60188454 ,0.47528714,
-0.6066466 ,-0.22995654 ,-0.58388734 ,0.20954669 ,-0.67851365]
beta = np.array(b)
print(beta)
iterations = 1000
arr_cost = np.zeros((iterations,2))
m = len(Y)
temp_beta = np.zeros(12)
for i in range(iterations):
for k in range(m):
temp_beta[0] …Run Code Online (Sandbox Code Playgroud)