Skip to content

Instantly share code, notes, and snippets.

Hannes Hapke hanneshapke

Block or report user

Report or block hanneshapke

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@hanneshapke
hanneshapke / generate_layer_heat_map.py
Last active Nov 18, 2019
How to generate the layer heat map in Keras 2.1
View generate_layer_heat_map.py
def get_heatmap(model, layer_name, matrix, y_labels):
# obtain probability of the label with the highest certainty
network_output = model.get_output_at(0)[:, np.argmax(y_labels)]
# obtain the output vector and its dimension of the convolutional layer we want to visualize
conv_layer, layer_output_dim = get_conv_layer(model, layer_name)
# Setting up the calculation of the gradients between the output and the conv layer. Will be executed in the iteration step
grads = K.gradients(network_output, conv_layer.output)[0]
# average the gradients across our samples (one sample) and all filters
@hanneshapke
hanneshapke / track_celery.py
Created Jul 22, 2016
Decorator for Celery functions to measure the execution time and memory usage
View track_celery.py
import time
from memory_profiler import memory_usage
import logging
celery_logger = logging.getLogger('celery')
def track_celery(method):
"""
@hanneshapke
hanneshapke / Editable Fields with ReactJS and ES2015
Last active Jun 21, 2019
Simple editable field with ReactJS and ES2015
View Editable Fields with ReactJS and ES2015
### Try it out with JSBin
(JSBin)[http://jsbin.com/dijefajalo/edit?html,js,console,output]
View model_inference_tf_example.py
import base64
import googleapiclient.discovery
from example_pb2 import Example
from feature_pb2 import BytesList, Feature, Features
def _convert_to_pb(value):
""" Serialize a given sentence to the ProtoBuf Structure required to model the tf.Example data structure.
Feel free to add more features and different data types if your models reqiures different inputs. An overview of
@hanneshapke
hanneshapke / generate_text_tag.py
Created Apr 11, 2018
Generate the text tag with the color attribute
View generate_text_tag.py
def cstr(s, color='black'):
return "<text style=\"color:{}\">{}</text>".format(color, s)
View .bashrc
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 / redis_vectors.py
Last active May 24, 2018
Mimicking Gensim's KeyedVectors class
View redis_vectors.py
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 / redis.py
Created May 24, 2018
Load word vectors into a redis db
View redis.py
import bz2
import pickle
from django.conf import settings
from djang_redis import get_redis_connection
from tqdm import tqdm
from .constants import GOOGLE_WORD2VEC_MODEL_NAME
View visualize_conv_nets_for_nlp.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View get_token_indicies.py
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:
You can’t perform that action at this time.