深度學(xué)習(xí)&PyTorch 之 DNN-回歸中使用HR數(shù)據(jù)集進(jìn)行了實(shí)現(xiàn),但是HR數(shù)據(jù)集中只有一個(gè)變量,這里我們使用多變量在進(jìn)行模擬一下
流程還是跟前面一樣
graph TD
A[數(shù)據(jù)導(dǎo)入] --> B[數(shù)據(jù)拆分]
B[數(shù)據(jù)拆分] --> C[Tensor轉(zhuǎn)換]
C[Tensor轉(zhuǎn)換] --> D[數(shù)據(jù)重構(gòu)]
D[數(shù)據(jù)重構(gòu)] --> E[模型定義]
E[模型定義] --> F[模型訓(xùn)練]
F[模型訓(xùn)練] --> G[結(jié)果展示]
1.1 數(shù)據(jù)導(dǎo)入
我們使用波士頓房價(jià)預(yù)測(cè)數(shù)據(jù),這是個(gè)開源的數(shù)據(jù)集,所以通用性更強(qiáng)
data = pd.read_csv('./boston_house_prices.csv')
data

在這里插入圖片描述
1.2 數(shù)據(jù)拆分
from sklearn.model_selection import train_test_split
train,test = train_test_split(data, train_size=0.7)
train_x = train[['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT']].values
test_x = test[['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT']].values
train_y = train.MEDV.values.reshape(-1, 1)
test_y = test.MEDV.values.reshape(-1, 1)
1.3 To Tensor
train_x = torch.from_numpy(train_x).type(torch.FloatTensor)
test_x = torch.from_numpy(test_x).type(torch.FloatTensor)
train_y = torch.from_numpy(train_y).type(torch.FloatTensor)
test_y = torch.from_numpy(test_y).type(torch.FloatTensor)
1.4 數(shù)據(jù)重構(gòu)
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
train_ds = TensorDataset(X, Y)
train_dl = DataLoader(train_ds, batch_size=batch, shuffle=True)
train_ds = TensorDataset(train_x, train_y)
train_dl = DataLoader(train_ds, batch_size=batch, shuffle=True)
test_ds = TensorDataset(test_x, test_y)
test_dl = DataLoader(test_ds, batch_size=batch * 2)
與之前是一樣的
1.5 網(wǎng)絡(luò)定義
class LinearModel(nn.Module):
def __init__(self):
super(LinearModel, self).__init__()
self.linear = nn.Linear(13, 1)
def forward(self, inputs):
logits = self.linear(inputs)
return logits
我們這里有13個(gè)特征變量
1.6 訓(xùn)練
model = LinearModel()
loss_fn = nn.MSELoss()
opt = torch.optim.SGD(model.parameters(), lr=lr) # 定義優(yōu)化器
train_loss = []
train_acc = []
test_loss = []
test_acc = []
for epoch in range(epochs+1):
model.train()
for xb, yb in train_dl:
pred = model(xb)
loss = loss_fn(pred, yb)
loss.backward()
opt.step()
opt.zero_grad()
if epoch%10==0:
model.eval()
with torch.no_grad():
train_epoch_loss = sum(loss_fn(model(xb), yb) for xb, yb in train_dl)
test_epoch_loss = sum(loss_fn(model(xb), yb) for xb, yb in test_dl)
train_loss.append(train_epoch_loss.data.item() / len(train_dl))
test_loss.append(test_epoch_loss.data.item() / len(test_dl))
template = ("epoch:{:2d}, 訓(xùn)練損失:{:.5f}, 驗(yàn)證損失:{:.5f}")
print(template.format(epoch, train_epoch_loss.data.item() / len(train_dl), test_epoch_loss.data.item() / len(test_dl)))
print('訓(xùn)練完成')
epoch: 0, 訓(xùn)練損失:469.15608, 驗(yàn)證損失:440.95737
epoch:10, 訓(xùn)練損失:101.80890, 驗(yàn)證損失:109.48333
epoch:20, 訓(xùn)練損失:91.18239, 驗(yàn)證損失:100.17014
epoch:30, 訓(xùn)練損失:100.83169, 驗(yàn)證損失:97.70323
epoch:40, 訓(xùn)練損失:89.96843, 驗(yàn)證損失:97.37273
epoch:50, 訓(xùn)練損失:94.20027, 驗(yàn)證損失:96.82300
......
epoch:480, 訓(xùn)練損失:74.97700, 驗(yàn)證損失:81.29946
epoch:490, 訓(xùn)練損失:74.74702, 驗(yàn)證損失:80.76858
epoch:500, 訓(xùn)練損失:89.31947, 驗(yàn)證損失:83.06767
訓(xùn)練完成
1.7 結(jié)果展示
import matplotlib.pyplot as plt
plt.plot(range(len(train_loss)), train_loss, label='train_loss')
plt.plot(range(len(test_loss)), test_loss, label='test_loss')
plt.legend()

在這里插入圖片描述