Skip to content

Instantly share code, notes, and snippets.

View stefanthaler's full-sized avatar

Stefan Thaler stefanthaler

View GitHub Profile
@stefanthaler
stefanthaler / group_trunc_seq_length.py
Created July 22, 2020 11:26
Tensorflow DataSet transformation that groups sequential data into buckets and truncates them instead of padding.
#
import tensorflow as tf
import numpy as np
from tensorflow.python.framework import ops
from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.util.tf_export import tf_export
from tensorflow.python.data.experimental import group_by_window
from tensorflow.python.framework import constant_op
@stefanthaler
stefanthaler / python_xlib_select_spike.py
Created August 31, 2019 19:40
A small example for python-xlib that works of events in a semi-blocking way using select.
#!/usr/bin/python3
import sys
import os
from Xlib import X, display, Xutil
import select
# http://python-xlib.sourceforge.net/doc/html/python-xlib_11.html
# Application window (only one)
@stefanthaler
stefanthaler / ensure_gradient_flow.py
Created October 11, 2017 11:53
Simple method for ensuring that the gradient flows through all your variables in TensorFlow.
import tensorflow as tf # 1.3.0
import numpy as np
# model + input
w = tf.Variable(1, name="w", dtype=tf.float32 ) # parameter to optimize for
x = tf.placeholder(shape=(), dtype=tf.float32, name="x") # input
# graph operations
diff_op = tf.multiply(w, x) # an operation that is differentiable
non_diff_op = tf.cast(tf.equal(diff_op, 0), dtype=tf.float32) # operation that is non differentiable
@stefanthaler
stefanthaler / check_reference_links.py
Created April 24, 2017 12:53
Checks a latex .bib file for links. If the script finds links, it tries to reach them to see if they are still alive.
@stefanthaler
stefanthaler / tensorflow-dynamic-rnn-explained.ipynb
Created February 22, 2017 16:13
Tensorflow Dynamic RNN explained
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@stefanthaler
stefanthaler / colorize_word_embeddings.py
Created February 3, 2017 14:38
A simple example to demonstrate how to link embedding metadata to word embeddings in tensorflow / tensorboard
"""
Simple example to demostrate the embedding visualization for word embeddings in tensorflow / tensorboard
https://www.tensorflow.org/how_tos/embedding_viz/
"""
import tensorflow as tf
import os
assert tf.__version__ == '1.0.0-rc0' # if code breaks, check tensorflow version
from tensorflow.contrib.tensorboard.plugins import projector
@stefanthaler
stefanthaler / lstm_backwards_embedding.py
Created December 1, 2016 15:56
Keras Embedding + Backwards LSTM
from keras.models import Sequential # model used
from keras.layers import Dense, Embedding, LSTM # layers used
batch_size = 2 # how many sequence to process in parallel
time_steps = 3 # lstm length, number of cells, etc.
input_dim = 1 # number of features
embedding_size = 5 # size of embedding vector
model = Sequential()
model.add(Embedding(
@stefanthaler
stefanthaler / stateful_lstm_embedding.py
Last active September 2, 2020 14:10
Simple example for a stateful keras LSTM with embedding.
"""
Learning Task:
Given a sequence, predict a label based on the first value of the sequence
Explanation of stateful LSTM and setup:
http://philipperemy.github.io/keras-stateful-lstm/
Exmple:
given a sequence [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], predict 1
given a sequence [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], predict 0
@stefanthaler
stefanthaler / fischer_wagner_explained.py
Created November 23, 2016 13:12
Implementation of the Fisher Wagner algorithm for calculation of the Levensthein distance which outputs all steps to enable understanding.
import numpy as np
from scipy.stats import entropy # calculate kl divergence #http://scipy.github.io/devdocs/generated/scipy.stats.entropy.html
np.set_printoptions(precision=2)
# https://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm
# Original Paper http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.367.5281&rep=rep1&type=pdf
deletion_cost = 1
insertion_cost = 1
substitution_cost = 1
# sudo pip install pulp
# sudo apt-get install python-glpk gplk-utils
from pulp import *
optimization_problem = LpProblem("Nutrients Optimization", LpMinimize)
# Cost Parameters for Selecting a food
# diversity (minimum amount of certain foods)
# diversity (minimum amount of vegetables)
# multiple cost factors (money, co2, water)