-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestFile3OG.py
More file actions
119 lines (98 loc) · 4.3 KB
/
testFile3OG.py
File metadata and controls
119 lines (98 loc) · 4.3 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
import time
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
import matplotlib.pyplot as plt
from fnn2 import FNN
from layers2 import Layer
import pandas as pd
# Load Iris dataset using sklearn
def load_iris_data(test_size=0.2, random_state=42):
iris = load_iris()
X = iris['data'].astype(np.float32) # Features
y = iris['target'].astype(int) # Target labels
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=random_state
)
return X_train, X_test, y_train, y_test
# Training function for FNN with time cost, accuracy, and loss convergence tracking
def train_fnn_iris(X_train, y_train, X_test, y_test, batch_size=16, epochs=9, learning_rate=0.1):
rng = np.random.default_rng(1337)
# Initialize the FNN with LogSoftmax activation in the last layer
l1 = Layer(in_features=4, out_features=16, activation_key="relu", use_xavier=False)
l2 = Layer(in_features=16, out_features=16, activation_key="relu", use_xavier=False)
l3 = Layer(in_features=16, out_features=3, activation_key="log_softmax", use_xavier=False)
fnn = FNN(layers=(l1, l2, l3), lr=learning_rate, bias=True, rng=rng)
history = [] # Store loss for each epoch
train_accuracies = []
start_time = time.time() # Start timer
for epoch in range(epochs):
X_train, y_train = shuffle(X_train, y_train) # Shuffle data every epoch
epoch_loss = 0.0
for i in range(0, len(X_train), batch_size):
X_batch = X_train[i:i + batch_size]
y_batch = y_train[i:i + batch_size]
loss, _ = fnn.minibatchGD(X_batch, y_batch, loss_key='nll')
epoch_loss += loss
avg_epoch_loss = epoch_loss / (len(X_train) / batch_size)
history.append(avg_epoch_loss)
# Calculate accuracy on test data
accuracy = evaluate_network(fnn, X_test, y_test, batch_size)
train_accuracies.append(accuracy)
print(f'Epoch {epoch + 1}/{epochs}, Loss: {avg_epoch_loss:.4f}, Test Accuracy: {accuracy:.4f}')
total_time = time.time() - start_time # Calculate time cost
print(f"Training completed in {total_time:.2f} seconds")
return fnn, history, total_time, train_accuracies
def evaluate_network(fnn, X_test, y_test, batch_size=16):
correct = 0
total = 0
for i in range(0, len(X_test), batch_size):
X_batch = X_test[i:i + batch_size]
y_batch = y_test[i:i + batch_size]
for x, y_true in zip(X_batch, y_batch):
output = fnn.forward(x)
predicted = np.argmax(output)
correct += int(predicted == y_true)
total += 1
accuracy = correct / total
return accuracy
def plot_loss_convergence(history):
plt.plot(history, label="Training Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.title("Loss Convergence Over Epochs")
plt.legend()
plt.show()
def plot_loss_accuracy(history, accuracies):
epochs = range(1, len(history) + 1)
fig, ax1 = plt.subplots()
color = 'tab:red'
ax1.set_xlabel('Epochs')
ax1.set_ylabel('Loss', color=color)
ax1.plot(epochs, history, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
color = 'tab:blue'
ax2.set_ylabel('Accuracy', color=color)
ax2.plot(epochs, accuracies, color=color)
ax2.tick_params(axis='y', labelcolor=color)
plt.title('Loss and Accuracy Over Epochs')
fig.tight_layout()
plt.show()
def main():
# Load and preprocess Iris dataset
X_train, X_test, y_train, y_test = load_iris_data()
# Train the FNN on Iris dataset
print("Starting training on Iris dataset...")
fnn, history, total_time, train_accuracies = train_fnn_iris(X_train, y_train, X_test, y_test, batch_size=16, epochs=50, learning_rate=0.01)
print("Training complete.")
# Print the total training time and final accuracy
final_accuracy = evaluate_network(fnn, X_test, y_test, batch_size=16)
print(f"Final Test Accuracy: {final_accuracy:.4f}")
print(f"Total Training Time: {total_time:.2f} seconds")
# Plot the loss convergence
plot_loss_convergence(history)
plot_loss_accuracy(history, train_accuracies)
if __name__ == "__main__":
main()