• 全连接网络实现回归【房价预测的数据】


    也是分为data,model,train,test

    1. import torch
    2. import torch.nn as nn
    3. import torch.nn.functional as F
    4. import torch.optim as optim
    5. class FCNet(nn.Module):
    6. def __init__(self):
    7. super(FCNet,self).__init__()
    8. self.fc1 = nn.Linear(331,200)
    9. self.fc2 = nn.Linear(200,150)
    10. self.fc3 = nn.Linear(150,100)
    11. self.fc4 = nn.Linear(100,1)
    12. #因为是回归问题,所以输出是1
    13. def forward(self,x):
    14. x = F.relu(self.fc1(x))
    15. x = F.relu(self.fc2(x))
    16. x = F.relu(self.fc3(x))
    17. x = self.fc4(x)
    18. return x
    19. class FCNet3(nn.Module):
    20. def __init__(self):
    21. super(FCNet3,self).__init__()
    22. self.fc1 = nn.Linear(331,200)
    23. self.fc2 = nn.Linear(200,100)
    24. self.fc3 = nn.Linear(100,1)
    25. def forward(self,x):
    26. x = F.relu(self.fc1(x))
    27. x = F.relu(self.fc2(x))
    28. x = self.fc3(x)
    29. return x
    30. # print(net)

    1. import pandas as pd
    2. import os
    3. import torch
    4. # my_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    5. if torch.cuda.is_available():
    6. my_device = torch.device('cuda')
    7. else:
    8. my_device = torch.device('cpu')
    9. training_data = pd.read_csv('./kaggle_house_pred_train.csv')
    10. testing_data = pd.read_csv('./kaggle_house_pred_test.csv')
    11. #拼在一起,方便后面统一处理
    12. all_features = pd.concat(( training_data.iloc[:,1:-1], testing_data.iloc[:,1:]))
    13. # print("train_data.shape:",training_data.shape)
    14. # print("test_data.shape:",testing_data.shape)
    15. # print("all_features:",all_features.shape)
    16. # print(training_data.iloc[:5,:8])
    17. #处理:把一些不是数值的那些特征值进行转换,并且归一化,还有就是把空值填充为0
    18. numeric_features = all_features.dtypes[all_features.dtypes != 'object'].index
    19. # print(numeric_features)
    20. all_features[numeric_features] = all_features[numeric_features].apply(lambda x: (x - x.mean()) / (x.std()))
    21. #all_features[numeric_features] = all_features[numeric_features]
    22. all_features[numeric_features] = all_features[numeric_features].fillna(0)
    23. all_features = pd.get_dummies(all_features, dummy_na = True)
    24. #df = all_features.to_csv('./newdata.csv')
    25. print("all_features:",all_features)
    26. #把数据分成训练数据和测试数据
    27. n_train = training_data.shape[0]
    28. #all_features = all_features.astype('float')
    29. train_features = torch.tensor(all_features[:n_train].values, dtype = torch.float32)
    30. test_features = torch.tensor(all_features[n_train:].values, dtype = torch.float32)
    31. train_labels = torch.tensor(training_data.SalePrice.values.reshape(-1, 1), dtype = torch.float32)
    32. print("train_features.shape:", train_features.shape)
    33. print("test_features.shape:", test_features.shape)
    34. print("train_labels:", train_labels.shape)
    35. #保存转换之后的数据
    36. new_train_data = pd.DataFrame(train_features.numpy()).to_csv('./train_data_normalization.csv')
    37. new_train_labels = pd.DataFrame(train_labels.numpy()).to_csv('./train_labels_normal.csv')
    38. train_dataset = torch.utils.data.TensorDataset(train_features,train_labels)
    39. train_dataloadr = torch.utils.data.DataLoader(train_dataset,batch_size=32,shuffle = True,num_workers = 0,pin_memory = True)
    40. #因为要测试 所有就没有真实标签了,dataloader也可以直接只放数据,后面测试时候就是inputs = data
    41. test_dataset = torch.utils.data.TensorDataset(test_features)
    42. test_dataloadr = torch.utils.data.DataLoader(test_dataset,batch_size=32,shuffle = True,num_workers = 0,pin_memory = True)
    43. #print(len(train_dataloadr))
    44. # print(len(test_dataloadr))
    45. #print(train_labels)

    1. import torch
    2. import torch.nn as nn
    3. import torch.functional as F
    4. import torch.optim as optim
    5. from Model import FCNet
    6. import data
    7. import matplotlib.pyplot as plt
    8. if torch.cuda.is_available():
    9. my_device = torch.device('cuda:0')
    10. else:
    11. my_device = torch.device('cpu')
    12. print(my_device)
    13. net = FCNet().to(my_device)
    14. #print(net)
    15. criterion = nn.MSELoss()
    16. optimizer = optim.Adam(net.parameters(),lr=0.0001)
    17. epochs = 2000
    18. def train(train_loader):
    19. train_loss = []
    20. for epoch in range(epochs):
    21. loss_sum = 0
    22. for i, data in enumerate(train_loader):
    23. inputs,labels = data
    24. print(data)
    25. inputs,labels = inputs.to(my_device),labels.to(my_device)
    26. optimizer.zero_grad()
    27. outputs = net(inputs)
    28. print('outputs=',outputs)
    29. print('labels=',labels)
    30. #因为是回归问题,所以直接放到loss中就可以了
    31. loss = criterion(outputs,labels)
    32. # print(loss.item())
    33. loss.backward()
    34. optimizer.step()
    35. loss_sum += loss.item()
    36. if i%32 == 31:
    37. print('Batch {}'.format(i+1),'Loss {}'.format(loss_sum/100))
    38. train_loss.append(loss_sum)
    39. torch.save(net.state_dict(),'./f4_weights_epoch2000.pth')
    40. plt.plot(range(epochs),train_loss)
    41. plt.show()
    42. train(data.train_dataloadr)
    1. import pandas as pd
    2. import data
    3. import torch
    4. from Model import FCNet
    5. if torch.cuda.is_available():
    6. my_device = torch.device('cuda:0')
    7. else:
    8. my_device = torch.device('cpu')
    9. test_data = data.testing_data
    10. test_features = data.test_features
    11. def test(test_features):
    12. test_features = test_features.to(my_device)
    13. preds = net(test_features).detach().to('cpu').numpy()
    14. print(preds.squeeze().shape)
    15. test_data['SalePrice'] = pd.Series(preds.squeeze())
    16. return pd.concat([test_data['Id'],test_data['SalePrice']],axis=1)
    17. net = FCNet().to(my_device)
    18. net.load_state_dict(torch.load('./f4_weights_epoch2000.pth'))
    19. res = test(test_features)
    20. res.to_csv('./f4_test_res.csv',index=False)

    预测结果还挺接近的

  • 相关阅读:
    音视频开发的正确(学习思路+技术指导)
    vue 中 Vue.prototype 详解及使用
    巧用字典树算法,轻松实现日志实时聚类分析
    VS Code For Web 深入浅出 -- 进程间通信篇
    分布式存储系统Ceph应用组件介绍
    Android:使用命令行发现keytool不是内部命令解决办法
    CentOS发生ping百度失败以及连接不到网关的问题解决
    天锐绿盾加密软件——企业数据防泄密-CAD图纸、文档、源代码加密管理系统@德人合科技
    程序员基础能力系列
    python面试题(36~50)
  • 原文地址:https://blog.csdn.net/wacebb/article/details/133470161