python预算房价

import torch
import time
x_data=torch.tensor([[1.0],[2.0],[3.0],[4.0],[5.0],[6.0]])
y_data=torch.tensor([[9600.0],[9550.0],[9382.0],[9250.0],[9200],[9150.0]])
class linearModel(torch.nn.Module):
    def __init__(self):
        super(linearModel,self).__init__()
        self.linear=torch.nn.Linear(1,1)
    def forward(self,x):
        return self.linear(x)
model=linearModel()
criterion=torch.nn.MSELoss(size_average=True)
optim=torch.optim.SGD(model.parameters(),lr=0.01)

if __name__ == '__main__':
    for epoch in range (200):
        y_pred=model(x_data)
        loss=criterion(y_pred,y_data)
        print(epoch,loss.item())
        time.sleep(0.03)
        optim.zero_grad()
        loss.backward()
        optim.step()
    x=torch.tensor([[9.0]])
    y=model(x)
    print(f"y={y}")

你可能感兴趣的:(python,开发语言,深度学习)