Skip to content

Instantly share code, notes, and snippets.

View hanneshapke's full-sized avatar

Hannes Hapke hanneshapke

View GitHub Profile
@hanneshapke
hanneshapke / norm_heatmap.py
Last active April 16, 2018 02:44
Normalize the visualization heat map
def norm_heatmap(heatmap):
# element-wise maximum calculation, basically setting all negative values to zero
heatmap = np.maximum(heatmap, 0)
# normalizing the heatmap to values between 0 and 1
norm_heatmap = heatmap / np.max(heatmap)
return norm_heatmap
@hanneshapke
hanneshapke / plot_heatmap.py
Last active April 16, 2018 02:44
Plot the layer heatmap
def plot_heatmap(heatmap, height_ratio=0.05):
# calculating how often the vector should be repeated to display a height relative to the vector length
repeat_vector_n_times = int(heatmap.shape[0] * height_ratio)
plt.matshow([heatmap] * repeat_vector_n_times)
@hanneshapke
hanneshapke / generate_color_for_heatmap_value.py
Last active April 16, 2018 02:45
Generate text color depending on the heat-map value
def color(hvalue, threshold, max=1, cdefault='black', colors=['red', 'yellow', 'green', 'cyan', 'blue']):
num_colors = len(colors)
if hvalue < threshold:
return cdefault
for i, color in enumerate(colors):
if hvalue > (max - (max - threshold) / num_colors * (i + 1)):
return color
@hanneshapke
hanneshapke / get_highlighted_tokens.py
Last active April 16, 2018 02:46
get_highlighted_tokens
def get_highlighted_tokens(tokens, matrix, model, layer_name, threshold, y_labels):
indices = get_token_indices(model, layer_name, threshold, matrix, y_labels)
ctokens = []
for i, t in enumerate(tokens):
if i in indices.keys():
_color = color(indices[i], threshold=threshold)
ctokens.append(cstr(t, color=_color))
else:
ctokens.append(t)
@hanneshapke
hanneshapke / get_token_indicies.py
Last active April 16, 2018 02:47
get_token_indicies
def get_token_indices(model, layer_name, threshold, matrix, y_labels):
heatmap = get_heatmap(model=model, layer_name=layer_name, matrix=matrix, y_labels=y_labels)
_, output_dim = get_conv_layer(model, layer_name)
# depending on the ration between the input and layer output shape, we need to calculate
# how many original tokens have contributed to the layer output
dim_ratio = matrix.shape[1] / output_dim
if dim_ratio < 1.5:
window_size = 1
else:
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@hanneshapke
hanneshapke / redis_vectors.py
Last active May 24, 2018 22:01
Mimicking Gensim's KeyedVectors class
import bz2
import numpy as np
import pickle
from django.conf import settings
from django_redis import get_redis_connection
from gensim.models.keyedvectors import KeyedVectors
from .constants import GOOGLE_WORD2VEC_MODEL_NAME
from .redis import load_word2vec_model_into_redis, query_redis
@hanneshapke
hanneshapke / .bashrc
Last active June 12, 2018 20:37
dev setup
export CURRENT_DEV=kreuzberg
alias latest_dev='cd ~/development/$CURRENT_DEV'
# ssh tunnel
alias ssd='~/bin/ssh_host_color.sh ubuntu@remote -p 823 -L 6006:gpu:6006'
# add additional paths to the PYTHONPATH
export PYTHONPATH=$PYTHONPATH:~/development/additional_package
# git shortcuts
@hanneshapke
hanneshapke / generate_text_tag.py
Created April 11, 2018 22:59
Generate the text tag with the color attribute
def cstr(s, color='black'):
return "<text style=\"color:{}\">{}</text>".format(color, s)
@hanneshapke
hanneshapke / Editable Fields with ReactJS and ES2015
Last active June 21, 2019 12:26
Simple editable field with ReactJS and ES2015
### Try it out with JSBin
(JSBin)[http://jsbin.com/dijefajalo/edit?html,js,console,output]