Skip to content

Commit

Permalink
Generate new dataset for each run
Browse files Browse the repository at this point in the history
  • Loading branch information
Mullestafa authored and joshniemela committed Dec 28, 2024
1 parent 999f56b commit dc5a292
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ data/*
*.png
datasets/*
.idea/
*.svg
65 changes: 63 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ wandb = "^0.17.9"
scipy = "^1.14.1"
bayesian-optimization = "^2.0.1"
safetensors = "^0.4.5"
pyqt5 = "^5.15.11"


[build-system]
Expand Down
10 changes: 6 additions & 4 deletions three_nodes_classification/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def forward(self, data):
class SAGE(nn.Module):
def __init__(self, normalise=False):
super(SAGE, self).__init__()
self.conv = SAGEConv(1, 1, normalize=normalise, bias=True, aggr="sum")
self.conv = SAGEConv(1, 1, normalize=normalise, bias=False, aggr="sum")

def forward(self, data):
x, edge_index, _ = data.x, data.edge_index, data.batch
Expand All @@ -39,9 +39,11 @@ def __init__(self, normalise=False, activation="simple"):
self.conv = SAGEConv(1, 1, normalize=normalise, bias=False, aggr="sum")

if activation == "gaussian":
self.activation = lambda x: torch.exp(-torch.pow(x[:, 0], 2))
self.activation = lambda x: torch.exp(-torch.pow(x, 2))
elif activation == "simple":
self.activation = lambda x: 1 - torch.pow(x[:, 0], 2)
self.activation = lambda x: 1 - torch.pow(x, 2)
elif activation == "sigmoid":
self.activation = torch.sigmoid

def forward(self, data):
x, edge_index, _ = data.x, data.edge_index, data.batch
Expand All @@ -50,6 +52,6 @@ def forward(self, data):

x = x.view(-1, 3)

out = self.activation(x)
out = self.activation(x[:, 0])

return out
27 changes: 19 additions & 8 deletions three_nodes_classification/plot_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from torch_geometric.loader import DataLoader
import matplotlib

# matplotlib.use("Qt5Agg")
matplotlib.use("pgf")
matplotlib.use("Qt5Agg")
# matplotlib.use("pgf")


def print_model_parameters(model):
Expand All @@ -20,16 +20,20 @@ def print_model_parameters(model):
data_loader = DataLoader(data_list, batch_size=1028, shuffle=True)

# Create a grid of w1 and w2 values
w1_range = np.linspace(-1, 1, 20)
w2_range = np.linspace(-1, 1, 20)
w1_range = np.linspace(-1, 1, 100)
w2_range = np.linspace(-1, 1, 100)
W1, W2 = np.meshgrid(w1_range, w2_range)

# Initialize your model
model = NonLinearSAGE()
model = NonLinearSAGE(activation="gaussian")
criterion = torch.nn.CrossEntropyLoss()

Loss = np.zeros((len(w1_range), len(w2_range)))

max_loss = 0
max_loss_w_1_weight = 0
max_loss_w_2_weight = 0

# Calculate loss over the grid
for i in range(len(w1_range)):
for j in range(len(w2_range)):
Expand All @@ -42,9 +46,17 @@ def print_model_parameters(model):
loss = criterion(out, batch.y)
total_loss += loss.item()
Loss[i, j] = total_loss / len(data_loader)

if Loss[i, j] > max_loss:
max_loss = Loss[i, j]
max_loss_w_1_weight = W1[i, j]
max_loss_w_2_weight = W2[i, j]
print(f"Finished {i+1}/{len(w1_range)} iterations")

print(f"Max loss: {max_loss:.4f}")
print(f"Max loss w1 weight: {max_loss_w_1_weight:.4f}")
print(f"Max loss w2 weight: {max_loss_w_2_weight:.4f}")


# Plot the 3D surface
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
Expand All @@ -53,10 +65,9 @@ def print_model_parameters(model):
ax.set_xlabel("w1")
ax.set_ylabel("w2")
ax.set_zlabel("Loss")
ax.set_title("3D Loss Surface")

# high resolution plot
plt.savefig("three_neighbour_classifier_gaussian_activatoin.pgf", dpi=900)
plt.savefig("three_neighbour_classifier_gaussian_activation.svg", dpi=900)
# plt.savefig("three_neighbour_classifier_gaussian_activatoin.png", dpi=900)

plt.ion()
Expand Down
13 changes: 6 additions & 7 deletions three_nodes_classification/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from torch.optim import AdamW

# Set the seed for reproducibility
torch.manual_seed(0)
# torch.manual_seed(29)


# Training function
Expand All @@ -31,7 +31,7 @@ def evaluate(model, data_loader):
with torch.no_grad():
for batch in data_loader:
outputs = model(batch)
predicted = (outputs > 0.5).float()
predicted = (outputs.abs() > 0.5).float()
total += batch.y.size(0)
correct += (predicted == batch.y).sum().item()
accuracy = 100 * correct / total
Expand All @@ -48,17 +48,16 @@ def main(
num_epochs=100,
num_runs=5,
):
# Generate training and test datasets
train_data_list = generate_three_nodes_dataset(samples)
test_data_list = generate_three_nodes_dataset(samples)
train_loader = DataLoader(train_data_list, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_data_list, batch_size=batch_size, shuffle=True)

accuracies = []

print(f"Training model: {model_type}, Normalise: {normalise}")

for run in range(num_runs):
train_data_list = generate_three_nodes_dataset(samples)
test_data_list = generate_three_nodes_dataset(samples)
train_loader = DataLoader(train_data_list, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_data_list, batch_size=batch_size, shuffle=True)
# Select model type based on user input
if model_type == "GCN":
model = GCN(normalise=normalise)
Expand Down

0 comments on commit dc5a292

Please sign in to comment.