Skip to content

Instantly share code, notes, and snippets.

View NoelKennedy's full-sized avatar

Noel Kennedy NoelKennedy

View GitHub Profile
@NoelKennedy
NoelKennedy / Tensorflow- Keras FP16 training.py
Created January 24, 2019 11:06
How to configure keras - tensorflow for training using FP16
import keras.backend as K
dtype='float16'
K.set_floatx(dtype)
# default is 1e-7 which is too small for float16. Without adjusting the epsilon, we will get NaN predictions because of divide by zero problems
K.set_epsilon(1e-4)
@NoelKennedy
NoelKennedy / token_occulusion_plots.py
Last active May 18, 2018 15:31
Method 2 : Hide each token in the input sentence one-by-one and see how this changes the likelihood of the sentence's class
import math
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
# x is a vector representing a sentence. Sentence is an vector of integers.
# this method returns a matrix Z of size number_of_tokens_in_sentence * number_of_tokens_in_sentence
# st that z[i][j] == x[j] except when i==j, z[i][j] == 0. ie the token is blanked out with the padding indicator
def create_occlusion_batch(x):
number_tokens_in_sentence=x.shape[0]
@NoelKennedy
NoelKennedy / generate_sentences.py
Last active May 18, 2018 15:34
Method 1 : Text-generation by gradient ascent on a filter's activation function
def make_random_input_sentence(sequence_length=300):
import random
input_words=list()
input_sentence=list()
for i in range(0,sequence_length):
word=random.choice(list(sg.wv.vocab.keys()))
input_words.append(word)
vector=sg[word]
input_sentence.append(vector)
disease_token_bit=np.zeros((sequence_length,1))
@NoelKennedy
NoelKennedy / i2.py
Created May 4, 2018 10:16
Create sentence via normal distribution
np.random.normal(loc=0.0, scale=1.0)
@NoelKennedy
NoelKennedy / i.py
Created May 4, 2018 10:15
Sentence initialisation for word2vec
def make_random_input_sentence(sequence_length=300,word2vec):
import random
input_words=list()
input_sentence=list()
for i in range(0,sequence_length):
word=random.choice(list(sg.wv.vocab.keys()))
input_words.append(word)
vector=word2vec[word]
input_sentence.append(vector)
input_sentence=np.array(input_sentence)
@NoelKennedy
NoelKennedy / Filter_activation.py
Last active May 18, 2018 15:35
Method 3 : rank input sentences by a filter's activation function score
# converts array of embedding intergers to a list of tokens that those integers represent
# the disease token is shown in upper case
# x_1 = array of embedding integers
# x_2 = array of disease phrase indicators
def embeddings_to_tokens(x_1,x_2,retain_null=True):
tokens=list([embedding_to_token_map[embedding_id] for embedding_id in x_1])
for i in range(0,x_2.shape[0]):
if x_2[i]==1:
@NoelKennedy
NoelKennedy / scalesque querystring example.cs
Created May 19, 2012 10:00
scalesque querystring example
public interface IQueryString {
Option<string> GetValue(string key);
}
@NoelKennedy
NoelKennedy / gist:2323679
Created April 6, 2012 22:47
Either<T,U> in action
public abstract partial class Either<T, U> {...}
//here are some business rules
public Either<LoanDeclinedReason, LoanRequest> CheckCustomerCredit(LoanRequest request) {
if (request.Customer.CreditScore > 8) return Either.Right(request);
return Either.Left(LoanDeclinedReason.InsufficientCreditScore);
}
public Either<LoanDeclinedReason, LoanRequest> CheckGoldPrerequisiteCustomer(LoanRequest request) {
if (request.Value < 10000) return Either.Right(request); //dont need gold status for less than 10,000
if (request.Customer.GoldCustomer) return Either.Right(request);
@NoelKennedy
NoelKennedy / tax.cs
Created September 22, 2011 16:37 — forked from flq/tax.cs
the tax thingy
public static decimal GetTaxes(decimal salary)
{
var taxBands = new[]
{
new Tuple<Decimal, Decimal, Decimal>(0, 5070, 0.1m),
new Tuple<Decimal, Decimal, Decimal>(5070, 8660, 0.14m),
new Tuple<Decimal, Decimal, Decimal>(8660, 14070, 0.23m),
new Tuple<Decimal, Decimal, Decimal>(14070, 21240, 0.3m),
new Tuple<Decimal, Decimal, Decimal>(21240, 40230, 0.33m),
new Tuple<Decimal, Decimal, Decimal>(40230, Decimal.MaxValue, 0.45m)
@NoelKennedy
NoelKennedy / gist:1077633
Created July 12, 2011 08:40
jsonp renderer for bowler
class JsonpViewRenderer extends JsonViewRenderer {
var nameOfCallbackParameter = "callback"
private def jsonWithPadding(renderJson: => Unit, request: Request, response: Response) = {
val callbackOption = request.getStringParameter(nameOfCallbackParameter)
callbackOption match {
case None => renderJson
case Some(callback) => {
response.getWriter.write(callback + "(")