"""
多项式回归
"""
def make_features(x):
"""Builds features a matrix with columns [x,x^2,x^3]"""
x = x.unsqueeze(1)
return torch.cat([x ** i for i in range(1,4)],1)
def f(x):
"""Approximated function."""
return x.mm(W_target) + b_target[0]
def get_batch(batch_size=100):
"""Builds a batch (x , f(x)) pair"""
random = torch.randn(batch_size)
random = np.sort(random)
random = torch.Tensor(random)
x = make_features(random)
y = f(x)
return Variable(x) , Variable(y)
# Define model
class poly_model(nn.Module):
def __init__(self):
super(poly_model,self).__init__()
self.poly = nn.Linear(3,1)
def forward(self,x):
out = self.poly(x)
return out
if __name__ == '__main__':
epoch = 0
W_target
深度学习之PyTorch---- 多项式线性回归
最新推荐文章于 2024-10-24 22:41:13 发布
本文详细介绍了如何使用PyTorch进行多项式线性回归,通过实例展示了数据预处理、模型构建、训练过程及结果评估,帮助读者深入理解深度学习在简单线性问题中的应用。
摘要由CSDN通过智能技术生成