我的代码中有一个错误,当我尝试获取输出时,它给我的成本函数提供了奇怪的输出
我是数据科学行业的新手,一直在从事我的第二个项目。我正在对线性回归实施梯度下降来优化我的成本模型,但我的 C 变量的输出似乎很奇怪。有人可以告诉我我的编码是否正确或者我做错了什么吗?下面是我的代码:
#Model Creation
import torch
import random from sklearn.preprocessing
import StandardScaler
input1 = (MarginMerged.drop(['BTC', 'Date'], 1))
input2 = torch.tensor(inputs.astype(float).values)
output = torch.tensor([MarginMerged['BTC']])
sc=StandardScaler()
standardized_x = sc.fit_transform(input2) #Standardizing for better convergence of Gradients
w1 = torch.randn(1, requires_grad = True) #Randomizing weights and allowing tracking of gradients
w2 = torch.randn(1, requires_grad = True)
w3 = torch.randn(1, requires_grad = True)
w4 = torch.randn(1, requires_grad = True)
b = torch.randn(1, requires_grad = True)
def regression(x1, w1, x2, w2, x3, w3, x4, w4, b): #Simple regression
function y = x1 * w1 + x2 * w2 + x3 * w3 + x4 * w4 + b return y
#Evaluating Model and Cost Function
yhat = regression(input2[:, 0], w1, input2[:, 1], w2, input2[:, 2], w3, input2[:, 3], w4, b)
def MSE(yhat, y):
sigma = torch.sum((yhat - y) **2) return sigma / len(y)
C = MSE(yhat, output)
Output:
tensor([ -766136.2741, -910269.9667, -827688.1826, -825344.9633,
-886781.1021, -939034.5842, -999291.1625, -996561.4835,
-1355946.4095, -1101955.2536, -1142216.8116, -1212749.9003,
-1737812.1974, -1621638.2128, -1758945.7766, -2119769.5569,
-2590650.6940, -3916453.0230, -4122350.1070, -9693393.5401,
-13584547.6533, -17504209.3513, -24114284.5156, -22302542.7874,
-19064275.4255, -35323125.1089, -86678884.6189, -66240221.4384,
-41395859.9706, -34334016.6174, -18819275.6493, -16508314.3713,
-9682844.7546, -14240738.2622, -11479996.4053, -9299528.3805,
-9841421.4224, -15140955.9555, -10965320.5364, -6465157.0859,
-6044804.7645, -4984105.2754, -7711268.3907, -11424855.3688,
-12303663.6678, -14704509.5725, -9483839.7203, -7645405.3528,
-5656487.9378, -5047090.0958, -3954263.1534, -5806911.3040,
-8229332.1597, -12360791.8777, -11092046.1287], dtype=torch.float64,
*Output should be positive due to implementing MSE*
I am new to data science industry and have been working on my second project. I am implementing gradient descent to my linear regression to optimize my cost model, but the output from my C variable seemed very strange. Can someone please tell me if I coded this correctly or is there something I am doing wrong? Below is my code:
#Model Creation
import torch
import random from sklearn.preprocessing
import StandardScaler
input1 = (MarginMerged.drop(['BTC', 'Date'], 1))
input2 = torch.tensor(inputs.astype(float).values)
output = torch.tensor([MarginMerged['BTC']])
sc=StandardScaler()
standardized_x = sc.fit_transform(input2) #Standardizing for better convergence of Gradients
w1 = torch.randn(1, requires_grad = True) #Randomizing weights and allowing tracking of gradients
w2 = torch.randn(1, requires_grad = True)
w3 = torch.randn(1, requires_grad = True)
w4 = torch.randn(1, requires_grad = True)
b = torch.randn(1, requires_grad = True)
def regression(x1, w1, x2, w2, x3, w3, x4, w4, b): #Simple regression
function y = x1 * w1 + x2 * w2 + x3 * w3 + x4 * w4 + b return y
#Evaluating Model and Cost Function
yhat = regression(input2[:, 0], w1, input2[:, 1], w2, input2[:, 2], w3, input2[:, 3], w4, b)
def MSE(yhat, y):
sigma = torch.sum((yhat - y) **2) return sigma / len(y)
C = MSE(yhat, output)
Output:
tensor([ -766136.2741, -910269.9667, -827688.1826, -825344.9633,
-886781.1021, -939034.5842, -999291.1625, -996561.4835,
-1355946.4095, -1101955.2536, -1142216.8116, -1212749.9003,
-1737812.1974, -1621638.2128, -1758945.7766, -2119769.5569,
-2590650.6940, -3916453.0230, -4122350.1070, -9693393.5401,
-13584547.6533, -17504209.3513, -24114284.5156, -22302542.7874,
-19064275.4255, -35323125.1089, -86678884.6189, -66240221.4384,
-41395859.9706, -34334016.6174, -18819275.6493, -16508314.3713,
-9682844.7546, -14240738.2622, -11479996.4053, -9299528.3805,
-9841421.4224, -15140955.9555, -10965320.5364, -6465157.0859,
-6044804.7645, -4984105.2754, -7711268.3907, -11424855.3688,
-12303663.6678, -14704509.5725, -9483839.7203, -7645405.3528,
-5656487.9378, -5047090.0958, -3954263.1534, -5806911.3040,
-8229332.1597, -12360791.8777, -11092046.1287], dtype=torch.float64,
*Output should be positive due to implementing MSE*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论