Skip to content

Instantly share code, notes, and snippets.

View eladn's full-sized avatar

Elad Nachmias eladn

  • CS Faculty @ Technion
View GitHub Profile
import math
import random
import itertools
import multiprocessing
from typing import Optional, Callable, List, Tuple, Any, Dict, TypeVar
__all__ = ['parallel_chunks_map']
@eladn
eladn / tensors_data_class.py
Last active October 26, 2020 10:50
Methodology for structurally organizing and batching PyTorch tensors. Especially useful for arranging the tensors of the preprocessed input or when some module's forward returns multiple tensors. A class inherits from TensorsDataClass may have fields which store PyTorch tensors. Instance of such class support some tensor methods like to(), cpu()…
import torch
import hashlib
import functools
import dataclasses
import numpy as np
from typing import List, Union, Optional, Tuple, Dict, Set, Any, final
from typing_extensions import Protocol
def seq_lengths_to_mask(seq_lengths: torch.LongTensor, max_seq_len: int, batch_first: bool = True):
@eladn
eladn / pytorch_apply_batched_embeddings.py
Last active May 2, 2020 17:32
PyTorch auxiliary function apply_batched_embeddings() to apply different embedding for each batch example, where the embedding matrices are arranged in a tensor shaped (batch_size, nr_words, embedding_dim)
import torch
import torch.nn as nn
from typing import Optional, Union
def apply_batched_embeddings(
batched_embeddings: torch.Tensor, indices: torch.Tensor,
mask: Optional[torch.Tensor] = None, padding_embedding_vector: Optional[torch.Tensor] = None,
common_embeddings: Optional[Union[torch.Tensor, nn.Embedding]] = None) -> torch.Tensor:
indices_device = indices.device
@eladn
eladn / calc_number_of_simple_paths.py
Created April 1, 2020 09:13
Python function that calculates the number of simple paths in a networkx graph using DFS algorithm
import networkx as nx
import numpy as np
def calc_number_of_simple_paths(graph: nx.DiGraph, s, t):
num_of_simple_paths = np.full(graph.number_of_nodes(), np.nan)
is_in_dfs_stack = np.full(graph.number_of_nodes(), 0)
def _dfs(u):
if is_in_dfs_stack[u]:
return 0
@eladn
eladn / alphabeta_rb_minimax.py
Created February 20, 2020 13:05
AlphaBeta Resource-Bound MiniMax algorithm written in Python
import typing
import networkx as nx
import numpy as np
from enum import Enum
__all__ = ['Agent', 'alpha_beta_rb_minimax']
class Agent(Enum):
@eladn
eladn / jobs_parallel_executor.py
Last active February 24, 2020 13:35
Auxiliary python tool for parallel async jobs execution with progress outputs (tqdm bar or printings per completed job) and execution status logging (#completed, #succeed, #failed, #remain, avgJobExecTime, throughput, ETA)
import os
import sys
import typing
import dataclasses
import multiprocessing
import functools
import time
import logging
@eladn
eladn / mongoose_schema_codegen_plugin.ts
Last active December 17, 2019 00:43
TypeScript plugin for GraphQL Code Generator that emits mongoose schema definitions
/*
Based on: https://github.com/MartinPavlik/graphql-codegen-mongoose-schema
*/
import {
GraphQLSchema, TypeInfo,
} from 'graphql';
import {
schemaToTemplateContext,
AstNode,
@eladn
eladn / betweenness_centrality.py
Created July 12, 2019 16:40
Python function to calculate betweenness centrality of a graph (using networkx, numpy, scipy, deque)
import numpy as np
from networkx import Graph, number_of_nodes
from collections import deque
from scipy.misc import comb
class GraphWithNodesMappings(Graph):
def __init__(self, *args, **kargs):
super().__init__(*args, **kargs)
self.__node_name_to_idx = {}
@eladn
eladn / income_salary_tax_calculator.py
Last active March 31, 2019 08:34
Python income salary tax calculator
import numpy as np
from collections import namedtuple
from itertools import chain
TaxStep = namedtuple('TaxStep', ['up_to', 'tax_percent'])
# Israeli income salary tax steps; updated on the 21.04.18; taken from:
# https://www.xn----1hcmgxnk8ede.co.il/%D7%9E%D7%93%D7%A8%D7%92%D7%95%D7%AA-%D7%9E%D7%A1
income_salary_tax_steps = [
TaxStep(up_to=6240, tax_percent=10),
@eladn
eladn / heapdict.py
Last active January 26, 2019 10:51
Python priority queue, inherits from collections.MutableMapping, and allows adding and removing items.
import collections
"""
This is based on HeapDict 1.0.0, but includes a few modifications.
https://pypi.org/project/HeapDict/
"""
def doc(s):
if hasattr(s, '__call__'):
s = s.__doc__