1313from torchviz import make_dot
1414
1515# ----------------- Setup -----------------
16- DATA_DIR = "data"
17- os .makedirs (DATA_DIR , exist_ok = True )
18- EMBEDDINGS_DIR = "../cache/Model_Sense.4n1/round_7/embeddings" # your embeddings directory
19- MODEL_PATH = "../cache/Model_Sense.4n1/round_7/Model_Sense.4n1_round7.pth" # path to saved SimpleNN state_dict
20-
16+ NAME = "Model_Sense.4n1"
17+ ROUND = 5
18+ OUTPUT_DIR = f"../{ NAME } _Data_Visualization"
19+ os .makedirs (OUTPUT_DIR , exist_ok = True )
20+ EMBEDDINGS_DIR = f"../cache/{ NAME } /round_{ ROUND } /embeddings" # your embeddings directory
21+ MODEL_PATH = f"../cache/{ NAME } /round_{ ROUND } /{ NAME } _round{ ROUND } .pth" # path to saved SimpleNN state_dict
2122DEVICE = torch .device ("cuda" if torch .cuda .is_available () else "cpu" )
2223
2324
@@ -79,7 +80,7 @@ def visualize_weight_distribution(model_, filename="Weight_Distribution.png"):
7980 plt .title ("Weight Distribution - First Layer" )
8081 plt .xlabel ("Weight Value" )
8182 plt .ylabel ("Frequency" )
82- plt .savefig (os .path .join (DATA_DIR , filename ))
83+ plt .savefig (os .path .join (OUTPUT_DIR , filename ))
8384 plt .close ()
8485
8586
@@ -98,7 +99,7 @@ def hook_fn(module_, input_, output):
9899 plt .title ("Activation Values - First Layer" )
99100 plt .xlabel ("Neuron Index" )
100101 plt .ylabel ("Activation Value" )
101- plt .savefig (os .path .join (DATA_DIR , filename ))
102+ plt .savefig (os .path .join (OUTPUT_DIR , filename ))
102103 plt .close ()
103104
104105
@@ -151,7 +152,7 @@ def visualize_tsne_custom(model_, embedder_, texts_, labels_, filename="Visualiz
151152 plt .xlabel ("t-SNE Dim 1" )
152153 plt .ylabel ("t-SNE Dim 2" )
153154 plt .title ("t-SNE of Custom Real-World Samples" )
154- plt .savefig (os .path .join (DATA_DIR , filename ))
155+ plt .savefig (os .path .join (OUTPUT_DIR , filename ))
155156 plt .close ()
156157
157158
@@ -184,7 +185,7 @@ def visualize_tsne(model_, dataloader_, filename="Visualize_tSNE.png", use_penul
184185 plt .xlabel ("t-SNE Dim 1" )
185186 plt .ylabel ("t-SNE Dim 2" )
186187 plt .title ("t-SNE Visualization of Features" )
187- plt .savefig (os .path .join (DATA_DIR , filename ))
188+ plt .savefig (os .path .join (OUTPUT_DIR , filename ))
188189 plt .close ()
189190
190191
@@ -198,7 +199,7 @@ def visualize_feature_importance(input_dim_, filename="Feature_Importance.svg"):
198199 plt .xlabel ("Features" )
199200 plt .ylabel ("Importance" )
200201 plt .xticks (rotation = 45 )
201- plt .savefig (os .path .join (DATA_DIR , filename ))
202+ plt .savefig (os .path .join (OUTPUT_DIR , filename ))
202203 plt .close ()
203204
204205
@@ -230,11 +231,11 @@ def plot_loss_landscape_3d(model_, dataloader_, criterion_, grid_size=30, epsilo
230231 X_grid , Y_grid = np .meshgrid (x , y )
231232 fig = go .Figure (data = [go .Surface (z = loss_values , x = X_grid , y = Y_grid , colorscale = "Viridis" )])
232233 fig .update_layout (title = "Loss Landscape" , scene = dict (xaxis_title = "u" , yaxis_title = "v" , zaxis_title = "Loss" ))
233- fig .write_html (os .path .join (DATA_DIR , filename ))
234+ fig .write_html (os .path .join (OUTPUT_DIR , filename ))
234235
235236
236237def save_model_state_dict (model_ , filename = "Model_State_Dict.txt" ):
237- with open (os .path .join (DATA_DIR , filename ), "w" ) as f :
238+ with open (os .path .join (OUTPUT_DIR , filename ), "w" ) as f :
238239 for name , tensor in model_ .state_dict ().items ():
239240 f .write (f"{ name } : { tensor .size ()} \n " )
240241
@@ -243,7 +244,7 @@ def generate_model_visualization(model_, input_dim_, filename="Model_Visualizati
243244 dummy_input = torch .randn (1 , input_dim_ ).to (DEVICE )
244245 dot = make_dot (model_ (dummy_input ), params = dict (model_ .named_parameters ()))
245246 dot .format = "png"
246- dot .render (filename = os .path .join (DATA_DIR , filename ), format = "png" )
247+ dot .render (filename = os .path .join (OUTPUT_DIR , filename ), format = "png" )
247248
248249
249250def save_graph (model_ , filename = "Neural_Network_Nodes_Graph.gexf" ):
@@ -255,11 +256,11 @@ def save_graph(model_, filename="Neural_Network_Nodes_Graph.gexf"):
255256 rows , cols = np .where (np .abs (W ) > threshold )
256257 for r , c in zip (rows , cols ):
257258 G .add_edge (f"{ name } _in_{ c } " , f"{ name } _out_{ r } " , weight = W [r , c ])
258- nx .write_gexf (G , os .path .join (DATA_DIR , filename ))
259+ nx .write_gexf (G , os .path .join (OUTPUT_DIR , filename ))
259260
260261
261262def save_model_summary (model_ , filename = "Model_Summary.txt" ):
262- with open (os .path .join (DATA_DIR , filename ), "w" ) as f :
263+ with open (os .path .join (OUTPUT_DIR , filename ), "w" ) as f :
263264 f .write (str (model_ ))
264265
265266
0 commit comments