-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathco2pred.py
More file actions
133 lines (100 loc) · 3.93 KB
/
co2pred.py
File metadata and controls
133 lines (100 loc) · 3.93 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import torch
import numpy as np
import matplotlib.pyplot as plt
from torch import nn
from torch.utils.data import Dataset, DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
import pandas as pd
device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else "cpu"
print(f"Using {device} device")
def calculate_mae(predictions, targets):
return torch.mean(torch.abs(predictions - targets))
maxi = 681
mini = 2.4
class TextDataset():
def __init__(self, file_path):
self.data = pd.read_excel(file_path, header = None)
self.data = self.data.values # Convert DataFrame to NumPy array before using PyTorch
self.data = torch.tensor(self.data, dtype=torch.float32)
self.data = (self.data - self.data.min()) / (self.data.max() - self.data.min())
self.inputs = self.data[:, :-5]
self.outputs = self.data[:, -5:]
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.inputs[idx], self.outputs[idx]
# Define model
class NeuralNetwork(nn.Module):
def __init__(self):
super().__init__()
self.linear_relu_stack = nn.Sequential(
nn.Linear(48, 64),
nn.ReLU(),
nn.Linear(64, 64),
nn.ReLU(),
nn.Linear(64, 64),
nn.ReLU(),
nn.Linear(64, 5)
)
def forward(self, x):
logits = self.linear_relu_stack(x)
return logits
model = NeuralNetwork().to(device)
print(model)
loss_fn = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.007)
def train(dataloader, model, loss_fn, optimizer):
size = len(dataloader.dataset)
model.train()
for batch, (X, y) in enumerate(dataloader):
X, y = X.to(device), y.to(device)
# Compute prediction error
pred = model(X)
loss = loss_fn(pred, y)
# Backpropagation
loss.backward()
optimizer.step()
optimizer.zero_grad()
loss, current = loss.item(), (batch + 1) * len(X)
# print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
def test(dataloader, model, loss_fn, record):
size = len(dataloader.dataset)
num_batches = len(dataloader)
model.eval()
test_loss = 0
total_mae = 0 # Variable to accumulate MAE
all_preds = []
with torch.no_grad():
for X, y in dataloader:
X, y = X.to(device), y.to(device)
pred = model(X)
test_loss += loss_fn(pred, y).item()
mae = calculate_mae(pred, y)
total_mae += mae.item()
all_preds.append(pred.cpu().numpy())
test_loss /= num_batches
total_mae /= num_batches
print(f"Test Error: \n Avg loss: {test_loss:.4f} \n Avg MAE: {total_mae:.4f}")
if (record == True):
all_preds = np.concatenate(all_preds, axis=0)
all_preds = [x * (maxi - mini) + mini for x in all_preds]
all_preds = np.array(all_preds, dtype = np.float32)
return all_preds
if __name__ == "__main__":
training_data = TextDataset('table1_train.xlsx')
test_data = TextDataset('table1_test.xlsx')
pred_data = TextDataset('table1_copy.xlsx')
train_dataloader = DataLoader(training_data, batch_size=10, drop_last = False)
test_dataloader = DataLoader(test_data, batch_size=10, drop_last = False)
pred_dataloader = DataLoader(pred_data, batch_size=51, drop_last=False)
epochs = 2000
for t in range(epochs):
train(train_dataloader, model, loss_fn, optimizer)
if (t % 50 == 0): test(test_dataloader, model, loss_fn, False)
pred = test(pred_dataloader, model, loss_fn, True)
print(pred)
# Convert NumPy array to csv
pred = pd.DataFrame(pred)
pred.to_csv('output.csv', index=False, header=False)
print("Finish!")