-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
127 lines (98 loc) · 3.84 KB
/
train.py
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
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, Dataset
import matplotlib.pyplot as plt
import pandas as pd
from model import *
from data_loader import *
input_size = 2
hidden_size = 4
output_size = 2
# MLPModel
# model = MLPModel(input_size, hidden_size, output_size)
# LSTMModel
model = LSTMModel(input_size, hidden_size, output_size)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
train_csv_path = 'train_dataset.csv'
test_csv_path = 'test_dataset.csv'
train_dataset = CustomDataset(train_csv_path)
test_dataset = CustomDataset(test_csv_path)
train_data_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_data_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)
epochs = 100
train_loss_values = []
test_loss_values = []
train_accuracy_values = []
test_accuracy_values = []
for epoch in range(epochs):
# Training phase
model.train()
epoch_train_loss = 0.0
correct_train = 0
total_train = 0
for inputs, labels in train_data_loader:
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
epoch_train_loss += loss.item()
_, predicted_train = torch.max(outputs.data, 1)
total_train += labels.size(0)
correct_train += (predicted_train == labels).sum().item()
epoch_train_loss /= len(train_data_loader)
train_loss_values.append(epoch_train_loss)
train_accuracy = 100 * correct_train / total_train
train_accuracy_values.append(train_accuracy)
# Testing phase
model.eval()
epoch_test_loss = 0.0
correct_test = 0
total_test = 0
with torch.no_grad():
for inputs, labels in test_data_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
loss = criterion(outputs, labels)
epoch_test_loss += loss.item()
_, predicted_test = torch.max(outputs.data, 1)
total_test += labels.size(0)
correct_test += (predicted_test == labels).sum().item()
epoch_test_loss /= len(test_data_loader)
test_loss_values.append(epoch_test_loss)
test_accuracy = 100 * correct_test / total_test
test_accuracy_values.append(test_accuracy)
if (epoch + 1) % 5 == 0:
print(f'Epoch [{epoch + 1}/{epochs}], Train Loss: {epoch_train_loss:.4f}, Test Loss: {epoch_test_loss:.4f}, Train Accuracy: {train_accuracy:.2f}%, Test Accuracy: {test_accuracy:.2f}%')
torch.save(model.state_dict(), 'saved_model_lstm.pth')
train_info = {'train_loss': train_loss_values,
'train_accuracy': train_accuracy_values,
'test_loss': test_loss_values,
'test_accuracy': test_accuracy_values}
train_info_df = pd.DataFrame(train_info)
train_info_df.to_csv("train_loss_lstm.csv", index=False)
# Plot the loss and accuracy on the same figure
plt.figure(figsize=(12, 4))
# Loss plot
plt.subplot(1, 2, 1)
plt.plot(range(1, epochs + 1), train_loss_values, label='Training Loss')
plt.plot(range(1, epochs + 1), test_loss_values, label='Testing Loss')
plt.title('Training and Testing Loss over Epochs')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
# Accuracy plot
plt.subplot(1, 2, 2)
plt.plot(range(1, epochs + 1), train_accuracy_values, label='Training Accuracy')
plt.plot(range(1, epochs + 1), test_accuracy_values, label='Testing Accuracy')
plt.title('Training and Testing Accuracy over Epochs')
plt.xlabel('Epoch')
plt.ylabel('Accuracy (%)')
plt.legend()
plt.tight_layout()
plt.show()