Skip to content

Commit

Permalink
I think the seed, new plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementineDomine committed Oct 24, 2024
1 parent 8081050 commit 7e7cac9
Show file tree
Hide file tree
Showing 6 changed files with 386 additions and 125 deletions.
410 changes: 296 additions & 114 deletions neuralplayground/agents/domine_2023_2.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ConfigTemplate:
),
config_field.Field(
name="num_nodes_max_test",
types=[int],
types=[list],
),
config_field.Field(
name="num_hidden",
Expand Down
6 changes: 3 additions & 3 deletions neuralplayground/agents/domine_2023_extras_2/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resample: False # @param
wandb_on: False
seed: 45
plot: True
dataset: 'random' # 'random' or 'omniglot'
dataset: 'random' # 'random' or 'omniglot' or position
weighted: False

num_hidden: 15 # @param
Expand All @@ -21,5 +21,5 @@ num_nodes_min: 5
num_features: 1

batch_size_test: 2
num_nodes_max_test: 7
num_nodes_min_test: 5
num_nodes_max_test: [40]
num_nodes_min_test: 10
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def __init__(self, num_hidden, num_feature, num_layers, num_message_passing_step
self.layer_norm = layer_norm
self.norm_layers = nn.ModuleList(
[nn.LayerNorm(num_hidden) for _ in range(num_message_passing_steps)]) if layer_norm else None

# Define the softmax layer
self.softmax = nn.Softmax(dim=1) # Apply softmax across the logits (along the class dimension)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from torchvision import transforms
from neuralplayground.agents.domine_2023_extras.class_utils import rng_sequence_from_rng

def create_random_matrix(rows, cols, low=0, high=1):
def create_random_matrix(rows, cols,seed, low=0, high=1):
"""
Generates a random matrix with the specified dimensions.
Parameters:
Expand All @@ -18,7 +18,6 @@ def create_random_matrix(rows, cols, low=0, high=1):
"""
return np.random.uniform(low, high, (rows, cols))


def get_omniglot_items(n):
# Define transformations for the images
transform = transforms.Compose([
Expand Down Expand Up @@ -115,8 +114,9 @@ def generate_source_and_sink(num_nodes):
sink = np.random.randint(0, num_nodes)

return source, sink
def sample_random_graph(num_features, num_nodes):
node_features = torch.tensor(create_random_matrix(num_nodes,num_features))
def sample_random_graph(num_features, num_nodes,seed):
# This is a graph with edges feature and random features
node_features = torch.tensor(create_random_matrix(num_nodes,num_features,seed))
edges , edge_features_tensor = create_line_graph_edge_list_with_features(num_nodes)
input_node_features = np.zeros((int(num_nodes), 2))
sink, source = generate_source_and_sink(num_nodes)
Expand All @@ -130,7 +130,8 @@ def sample_random_graph(num_features, num_nodes):

#TODO: we need to merge this two potentially

def sample_omniglot_graph(num_nodes):
def sample_omniglot_graph(num_nodes,seed):
# This is a graph with edges feature and omniglot features
node_features = torch.tensor(get_omniglot_items(num_nodes))
edges , edge_features_tensor = create_line_graph_edge_list_with_features(num_nodes)
input_node_features = np.zeros((int(num_nodes), 2))
Expand All @@ -143,4 +144,34 @@ def sample_omniglot_graph(num_nodes):
node_features = torch.tensor(combined_node_features, dtype=torch.float32)
return node_features, edges, edge_features_tensor, source, sink

def sample_random_graph_position(num_features, num_nodes,seed):
# This is a graph with edges feature and position features
node_features = torch.tensor(create_random_matrix(num_nodes,num_features))
edges , edge_features_tensor = create_line_graph_edge_list_with_features(num_nodes)
input_node_features = np.zeros((int(num_nodes), 2))
sink, source = generate_source_and_sink(num_nodes)
input_node_features[source, 0] = 1 # Set source node feature
input_node_features[sink, 1] = 1 # Set sink node feature
# Concatenate the feature matrices along the feature dimension (axis=1)
combined_node_features = np.concatenate([node_features, input_node_features], axis=1)
position = torch.tensor([np.arange(0, num_nodes)])
combined_node_features_pos = np.concatenate([combined_node_features, position.T], axis=1)
# Convert combined node features back to a tensor
node_features = torch.tensor(combined_node_features_pos , dtype=torch.float32)
return node_features, edges, edge_features_tensor, source, sink

def sample_random_graph_position_no_edges(num_features, num_nodes,seed):
# This is a graph with no edges feature but position features
node_features = torch.tensor(create_random_matrix(num_nodes,num_features))
edges , edge_features_tensor = create_line_graph_edge_list_with_features(num_nodes)
input_node_features = np.zeros((int(num_nodes), 2))
sink, source = generate_source_and_sink(num_nodes)
input_node_features[source, 0] = 1 # Set source node feature
input_node_features[sink, 1] = 1 # Set sink node feature
# Concatenate the feature matrices along the feature dimension (axis=1)
combined_node_features = np.concatenate([node_features, input_node_features], axis=1)
# Convert combined node features back to a tensor
node_features = torch.tensor(combined_node_features, dtype=torch.float32)
return node_features, edges, source, sink

#TODO: we need to merge this into one function because this is ungly
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,53 @@ def plot_curves(curves, path, title, legend_labels=None, x_label=None, y_label=N
ax.legend()
plt.savefig(path)
plt.show()
plt.close()
plt.close()

def plot_curves_2(curves, std_devs=None, path=None, title=None, legend_labels=None, x_label=None, y_label=None):
fig, ax = plt.subplots(figsize=(8, 6))
ax.set_title(title)

if x_label:
ax.set_xlabel(x_label)
if y_label:
ax.set_ylabel(y_label)

colormap = plt.get_cmap("viridis")

# Use numpy linspace to create the values array
values = np.linspace(0, 1, len(curves))

# Map the values to colors using the chosen colormap
colors = [colormap(value) for value in values]

for i, curve in enumerate(curves):
label = legend_labels[i] if legend_labels else None
color = colors[i % len(colors)]

# Convert PyTorch tensors to NumPy arrays for plotting
if isinstance(curve, torch.Tensor):
curve = curve.detach().numpy()

# Plot the average curve
ax.plot(curve, label=label, color=color)

# If std_devs are provided, plot the shaded region for the standard deviation
if std_devs is not None:
std_dev = std_devs[i]

# Convert std_dev to numpy if it is a PyTorch tensor
if isinstance(std_dev, torch.Tensor):
std_dev = std_dev.detach().numpy()

# Shaded region (curve ± std deviation)
ax.fill_between(np.arange(len(curve)), np.asarray(curve) - np.asarray( std_dev), np.asarray(curve) + np.asarray(std_dev), color=color, alpha=0.3)

if legend_labels:
ax.legend()

# Save plot to file if path is provided
if path:
plt.savefig(path)

plt.show()
plt.close()

0 comments on commit 7e7cac9

Please sign in to comment.