Skip to content

Instantly share code, notes, and snippets.

Avatar

Tomasz Bartczak kretes

View GitHub Profile
@kretes
kretes / ModelWeightsPrinter.py
Last active Sep 6, 2021
tensorflow callback that prints some basic statistics about weights
View ModelWeightsPrinter.py
class ModelWeightsPrinter(Callback):
def __init__(self, model) -> None:
super().__init__()
self.model = model
def print_stats(self, hist):
if hist:
allw = np.hstack([x.flatten() for x in self.model.get_weights()])
h = np.histogram(allw, bins=np.linspace(-1, 1, 5))
print("weights_histogram")
View tf_47032.py
import tensorflow as tf
from tensorflow import keras
import numpy as np
N = 6
CLASSES = 3
FEATURES = 4
model = keras.Sequential([
keras.layers.Dense(10, input_shape=(FEATURES,)),
@kretes
kretes / tf_measure.py
Created May 12, 2021
function wrapper to measure execution time in tf graph
View tf_measure.py
def measure_time_tf(fun, name):
def time_measuring(*args):
start = tf.timestamp()
result = fun(*args)
end = tf.timestamp()
diff_times100 = 100.0 * (end - start)
tf.print(name, tf.cast(tf.cast(diff_times100, tf.int32), tf.float32) / 100.0)
return result
return time_measuring
@kretes
kretes / print_tf_ds.py
Created May 10, 2021
A short utility to apply over a tf.data.Dataset to display what is it processing at given stage
View print_tf_ds.py
def print_ds(ds, what):
def print_elems(*args):
tf.print(f"len of {what}", len(args), *[tf.shape(a) for a in args], args[-1])
return tuple(args)
return ds.map(print_elems)
ds = print_ds(ds, "1")
@kretes
kretes / explain_structure_tf.py
Created Apr 16, 2021
Explains structure with collections/numpy/tensorflow tensors in it
View explain_structure_tf.py
import tensorflow as tf
def explain_structure(obj, how_many=3):
if hasattr(obj, 'shape'):
repr = f"{type(obj)}, {obj.shape}"
if type(obj) == tf.Tensor:
repr = f"{repr} {obj.device}"
return repr
elif type(obj) in [type([]), type(())]:
inner = obj
View ipython.out
In [7]: import numpy as np
...: import tensorflow as tf
...: print(tf.version.GIT_VERSION, tf.version.VERSION)
...:
...: dim = 300
...:
...: def return_single(*args):
...: x = np.random.rand(dim,dim,dim)
...: return x,1
...:
@kretes
kretes / timing_wrapper.py
Last active Jan 12, 2021
timing wrapper that wraps a funciton and logs its execution time to tensorboard
View timing_wrapper.py
from functools import wraps
import itertools
def timing_tb(writer):
def wrapper_outer(func):
cont = itertools.count()
print(f"will log under {func.__name__}")
@wraps(func)
def wrapper(*arg, **kw):
View explain_structure.py
def explain_structure(obj, all=False):
if hasattr(obj, 'shape'):
repr = f"{type(obj)}, {obj.shape}"
if type(obj) == torch.Tensor:
repr = f"{repr} {obj.device}"
return repr
elif type(obj) in [type([]), type(())]:
inner = obj
if not all:
inner = inner[:3]
@kretes
kretes / bpe_hang.py
Created Mar 4, 2020
reproduce of tokenizers hang on encode_batch
View bpe_hang.py
from multiprocessing import Process
import os
from tokenizers.implementations import ByteLevelBPETokenizer
import tokenizers
print(tokenizers.__version__)
# works:
tok = ByteLevelBPETokenizer()
print(tok.encode_batch(['ala']))
@kretes
kretes / dmatrix_from_big_csr.py
Created Dec 31, 2019
Reproducing the problem in xgboost - impossible to create a DMatrix from a big sparse matrix
View dmatrix_from_big_csr.py
import xgboost as xgb
from scipy.sparse.csr import csr_matrix
import numpy as np
from sklearn.datasets import load_svmlight_file
num_rows = int(np.iinfo(np.int32).max / 1000)
num_cols = 1001
more_than_int32_count = num_rows * num_cols - np.iinfo(np.int32).max
print(more_than_int32_count)