Skip to content

Instantly share code, notes, and snippets.

View udibr's full-sized avatar
🏠
Working from home

Ehud Ben-Reuven udibr

🏠
Working from home
View GitHub Profile
@udibr
udibr / test
Created October 22, 2017 12:44
0x4CDE69AE1A924010Ba33F2944A4bb6dfbFB64958
@udibr
udibr / gruln.py
Last active November 7, 2020 02:34
Keras GRU with Layer Normalization
import numpy as np
from keras.layers import GRU, initializations, K
from collections import OrderedDict
class GRULN(GRU):
'''Gated Recurrent Unit with Layer Normalization
Current impelemtation only works with consume_less = 'gpu' which is already
set.
# Arguments
@udibr
udibr / remove_mask.py
Created May 28, 2016 22:52
Keras layer to remove mask information
from keras.layers.core import Lambda
class RemoveMask(Lambda):
def __init__(self):
super(RemoveMask, self).__init__((lambda x, mask: x))
self.supports_masking = True
def compute_mask(self, input, input_mask=None):
return None
@udibr
udibr / keras_part_load.py
Last active May 9, 2020 23:12
Load weights to Keras model from file allowing for differences between file and model
import numpy as np
import h5py
import keras.backend as K
def str_shape(x):
return 'x'.join(map(str, x.shape))
def load_weights(model, filepath, lookup={}, ignore=[], transform=None, verbose=True):
"""Modified version of keras load_weights that loads as much as it can.
Useful for transfer learning.
@udibr
udibr / beamsearch.py
Last active October 4, 2021 11:50
beam search for Keras RNN
# variation to https://github.com/ryankiros/skip-thoughts/blob/master/decoding/search.py
def keras_rnn_predict(samples, empty=empty, rnn_model=model, maxlen=maxlen):
"""for every sample, calculate probability for every possible label
you need to supply your RNN model and maxlen - the length of sequences it can handle
"""
data = sequence.pad_sequences(samples, maxlen=maxlen, value=empty)
return rnn_model.predict(data, verbose=0)
def beamsearch(predict=keras_rnn_predict,
@udibr
udibr / py_cudart_memory.py
Last active January 18, 2016 17:45 — forked from justinfx/py_cudart_memory.py
Get memory usage of CUDA GPU, using ctypes and libcudart
import ctypes
# Path to location of libcudart
# Change the path to "cudart<x>_<ver>.dll" to use on Windows, where <x>={32|64} and <ver> is the CUDA version.
_CUDA = "/usr/local/cuda/lib/libcudart.dylib"
cuda = ctypes.cdll.LoadLibrary(_CUDA)
cuda.cudaMemGetInfo.restype = int
cuda.cudaMemGetInfo.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
@udibr
udibr / clean_notebook.py
Last active February 22, 2016 00:57
A command line for cleaning ipython notebook from the content of the output cells
import sys
if len(sys.argv) < 2:
print "Usage: %s <file-name>"%sys.argv[0]
exit(0)
fname = sys.argv[1]
import json
lookup = {'outputs':[], 'execution_count':None}