This repository was archived by the owner on Mar 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathhousing.py
More file actions
111 lines (85 loc) · 3.44 KB
/
Copy pathhousing.py
File metadata and controls
111 lines (85 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import pandas as pd
import torch
import torch.nn as nn
from torch import optim
from torch.utils.data import Dataset, DataLoader, TensorDataset
import time
class BostonHousing(Dataset):
def __init__(self, data: pd.DataFrame):
# Data: All columns except the last one / Target: Only the last column (MEDV)
self.data = torch.tensor(data.iloc[:, :-1].values, dtype=torch.float32)
self.target = torch.tensor(data.iloc[:, -1].values, dtype=torch.float32).view(
-1, 1
)
# Normalize data
self.data = (self.data - self.data.mean(dim=0)) / self.data.std(dim=0)
# Create dataset
self.dataset = TensorDataset(self.data, self.target)
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
return self.dataset[idx]
class LinearRegression(nn.Module):
def __init__(self, input_dim):
super(LinearRegression, self).__init__()
self.linear = nn.Linear(input_dim, 1)
def forward(self, x):
return self.linear(x)
if __name__ == "__main__":
# Load data and split in training and testing sets
df = pd.read_csv("./examples/data/housing.csv")
TRAIN_PCT = 0.99
shuffled_df = df.sample(frac=1, random_state=42)
train_df = shuffled_df[: int(TRAIN_PCT * len(df))]
test_df = shuffled_df[int(TRAIN_PCT * len(df)) :]
train_data = BostonHousing(train_df)
test_data = BostonHousing(test_df)
# Train Parameters
batch_size = 32
num_epochs = 200
learning_rate = 0.02
# Batchwise data loader
loaders = {
"train": DataLoader(
train_data, batch_size=batch_size, shuffle=False, num_workers=1
),
"test": DataLoader(
test_data, batch_size=batch_size, shuffle=False, num_workers=1
),
}
device = torch.device("cpu")
# model = torch.compile(LinearRegression(train_data.data.shape[1]), fullgraph=True, options={"epilogue_fusion": True, "max_autotune": True})
model = LinearRegression(train_data.data.shape[1])
loss_func = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# optimizer = optim.SGD(model.parameters(), lr=learning_rate)
# it seems the python for loop is what is making the program slow (so pytorch has a disadvantage thanks to python)
model.train()
start = time.time()
for epoch in range(num_epochs):
epoch_loss = 0
num_batches = 0
for batch_data, batch_labels in loaders["train"]:
start_batch = time.time()
# Forward pass
outputs = model(batch_data)
loss = loss_func(outputs, batch_labels)
# Backward pass
optimizer.zero_grad()
loss.backward()
optimizer.step()
epoch_loss += loss.item()
num_batches += 1
# print time in ms
# print(f'Batch time: {1000 * (time.time() - start_batch):.2f} ms') # The speed of a batch in basalt and pytorch are similar or pytorch can be faster
print(
f"Epoch [{epoch + 1}/{num_epochs}],\t Avg loss per epoch:"
f" {epoch_loss / num_batches}"
)
print(f"Training time: {time.time() - start:.2f} seconds")
# Evaluate the model
model.eval()
with torch.no_grad():
test_predictions = model(test_data.data)
mse_loss = loss_func(test_predictions, test_data.target).item()
print(f"Mean Squared Error on Test Data: {mse_loss:.4f}")