-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_RNN.py
196 lines (162 loc) · 6.39 KB
/
train_RNN.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import torch
import os
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
import pandas as pd
from data_loader import CustomDataset # Assuming CustomDataset loads your data
from model import RNNModel
model_name = "RNN"
ext="TSN"
# Model parameters
input_size = 8
hidden_size = 11
output_size = 4
num_layers = 8
num_epochs=100
learning_Rate=0.001
batch_Size=12
# Instantiate RNN model
model = RNNModel(input_size, hidden_size, num_layers, output_size)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
print(f"Using device: {device}")
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_Rate) # Using Adam optimizer
# Paths to training and testing data
train_csv_path = r"C:\test.csv"
test_csv_path = r"C:\test.csv"
# Assuming CustomDataset class loads your data
train_dataset = CustomDataset(train_csv_path)
test_dataset = CustomDataset(test_csv_path)
train_data_loader = DataLoader(train_dataset, batch_size=batch_Size, shuffle=True)
test_data_loader = DataLoader(test_dataset, batch_size=batch_Size, shuffle=False)
epochs = num_epochs
train_loss_values = []
test_loss_values = []
train_accuracy_values = []
test_accuracy_values = []
# Assuming you have a DataLoader `train_data_loader` and `test_data_loader` for your datasets
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()
# Forward pass
outputs = model(inputs)
# Ensure outputs and labels have the same batch size
if outputs.size(0) != labels.size(0):
raise ValueError(
f'Expected input batch_size ({outputs.size(0)}) to match target batch_size ({labels.size(0)})')
# Calculate loss
loss = criterion(outputs, labels)
# Backward pass and optimize
loss.backward()
optimizer.step()
epoch_train_loss += loss.item()
# Calculate accuracy
_, 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)
# Forward pass
outputs = model(inputs)
# Ensure outputs and labels have the same batch size
if outputs.size(0) != labels.size(0):
raise ValueError(
f'Expected input batch_size ({outputs.size(0)}) to match target batch_size ({labels.size(0)})')
# Calculate loss
loss = criterion(outputs, labels)
epoch_test_loss += loss.item()
# Calculate accuracy
_, 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)
# Print epoch results
print(
f'Epoch [{epoch + 1}/{epochs}], Train Loss: {epoch_train_loss:.4f}, Train Accuracy: {train_accuracy:.2f}%, Test Loss: {epoch_test_loss:.4f}, Test Accuracy: {test_accuracy:.2f}%')
# Specify output folder for saving the model, CSV file, and plots
output_folder1 = r"C:\Models"# Replace with desired path
os.makedirs(output_folder1, exist_ok=True) # Create folder if it doesn't exist
output_folder2 = r"C:\CSV" # Replace with desired path
os.makedirs(output_folder2, exist_ok=True) # Create folder if it doesn't exist
output_folder3 = r"C:\Plots" # Replace with desired path
os.makedirs(output_folder3, exist_ok=True) # Create folder if it doesn't exist
# Save the model state
# Save the final model
model_path = os.path.join(output_folder1, f"{model_name}_{ext}.pth")
torch.save({
'model_state_dict': model.state_dict(),
'input_size': input_size,
'hidden_size': hidden_size,
'num_epochs': num_epochs,
'output_size': output_size,
'num_layers': num_layers,
'learning_rate': learning_Rate,
'hyperparameters': {
'input_size': input_size,
'hidden_size': hidden_size,
'num_epochs': num_epochs,
'output_size': output_size,
'num_layers': num_layers,
'learning_rate': learning_Rate,
'batch_size': batch_Size,
}
}, model_path)
print(f"Model saved at {model_path}")
# Plotting loss and accuracy
# (Code for plotting remains the same as in the previous response)
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)
csv_path = os.path.join(output_folder2, f"{model_name}_{ext}.csv")
train_info_df.to_csv(csv_path, index=False)
print(f"Training data saved at {csv_path}")
# Plotting loss and accuracy
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()
# Save plots as PNG and PDF
png_file_path = os.path.join(output_folder3, f"{model_name}_{ext}.png")
pdf_file_path = os.path.join(output_folder3, f"{model_name}_{ext}.pdf")
plt.savefig(png_file_path, format='png', dpi=600)
plt.savefig(pdf_file_path, format='pdf', dpi=600)
print(f"Plots saved at {png_file_path} and {pdf_file_path}")
plt.tight_layout()
plt.show()