This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TriangleIndexes(nn.Module): | |
| def __init__(self, adjacency_matrix): | |
| super().__init__() | |
| self.adjacency_matrix = adjacency_matrix | |
| def forward(self): | |
| # tensor of indexes of each neighbors of each nodes | |
| nonzero = self.adjacency_matrix.nonzero() | |
| neighbors_one_indexes = nonzero.reshape(self.adjacency_matrix.shape[0],15,2)[:,:,1].clone() | |
| neighbors_two_indexes = neighbors_one_indexes[neighbors_one_indexes] # Tensor for each 2 neighbors for each nodes (neighbors of neighbors) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sets_to_lists(nested_dict): | |
| for key, value in nested_dict.items(): | |
| if isinstance(value, set): | |
| nested_dict[key] = list(value) | |
| elif isinstance(value, dict): | |
| sets_to_lists(value) | |
| return nested_dict |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def merge_dict(d1, d2): | |
| """Merge d2 into d1 with terminal values stored in sets.""" | |
| for key, value in d2.items(): | |
| if key in d1: | |
| if isinstance(value, dict) and isinstance(d1[key], dict): | |
| merge_dict(d1[key], value) | |
| else: | |
| if not isinstance(d1[key], set): | |
| d1[key] = set([d1[key]]) | |
| d1[key].add(value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import xmltodict | |
| from bs4 import BeautifulSoup | |
| with open(filepath, 'r') as f: | |
| file = f.read() | |
| soup = BeautifulSoup(file, 'xml') | |
| str_xml = str(soup) | |
| dict_xml = xmltodict.parse(str_xml) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def count_leaves_dict(element:dict) -> int: | |
| if isinstance(element, list): return len(element) | |
| if not isinstance(element, dict): raise Exception("Wrong data type") | |
| nb_leaves = 0 | |
| for _, value in element.items(): | |
| nb_leaves += count_leaves_dict(value) | |
| return nb_leaves |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| d_sorted = dict(sorted(d.items(), key=lambda item: item[1], reverse=True)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import matplotlib.pyplot as plt | |
| import matplotlib.patches as patches | |
| from random import random | |
| def draw_path(options:list[int], choices:list[int], margin:int=1, lw:int=7.5, inch:int=5): | |
| fig, ax = plt.subplots() | |
| ax.set_aspect('equal') | |
| ax.axis('off') | |
| theta1_limit, theta2_limit = 0, 360 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ffmpeg -r 24 -i images/%01d.png -vcodec mpeg4 -y movie.mp4 | |
| ‾‾ ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ ‾‾‾‾‾ ‾‾‾‾‾‾‾‾‾ | |
| ↑ ↑ ↑ ↑ | |
| FPS images format name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from torch.utils.tensorboard import SummaryWriter | |
| writer = SummaryWriter() | |
| def save_histograms(writer, step, model): | |
| def flatten_gru_weights(gru_layer): | |
| flattened_weights = [] | |
| for param in gru_layer.parameters(): | |
| flattened_weights.append(param.data.view(-1)) | |
| return torch.cat(flattened_weights) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pathlib | |
| import numpy as np | |
| import cv2 | |
| from tensorboard.backend.event_processing import event_accumulator | |
| event_filepath = 'runs/Apr19_11-02-02_RNSCWL0127/events.out.tfevents.1713517322.RNSCWL0127.15332.0' | |
| output_dir = 'images/img_train' | |
| event_acc = event_accumulator.EventAccumulator(event_filepath, size_guidance={'images': 0}) |