Skip to content

Instantly share code, notes, and snippets.

View ddofer's full-sized avatar

Dan Ofer ddofer

View GitHub Profile
@chretm
chretm / gist:fdcefce520ddfa1b66af3c730d4928c0
Created April 13, 2017 09:16
semantic-similarity-for-short-sentence_python3
#author : Sujit Pal
#Note: this is a python3 updated version of http://sujitpal.blogspot.fr/2014/12/semantic-similarity-for-short-sentences.html
# by mathieu Chrétien (mchretien.pro@mail.com)
#contributor : Mathieu Chrétien
from __future__ import division
import nltk
from nltk.corpus import wordnet as wn
@nigeljyng
nigeljyng / AttentionWithContext.py
Last active February 10, 2021 14:02 — forked from cbaziotis/AttentionWithContext.py
Keras Layer that implements an Attention mechanism, with a context/query vector, for temporal data. Supports Masking. Follows the work of Yang et al. [https://www.cs.cmu.edu/~diyiy/docs/naacl16.pdf] "Hierarchical Attention Networks for Document Classification"
class AttentionWithContext(Layer):
"""
Attention operation, with a context/query vector, for temporal data.
Supports Masking.
Follows the work of Yang et al. [https://www.cs.cmu.edu/~diyiy/docs/naacl16.pdf]
"Hierarchical Attention Networks for Document Classification"
by using a context vector to assist the attention
# Input shape
3D tensor with shape: `(samples, steps, features)`.
# Output shape
def load_challenge_data(df,start_at,truncate_at):
seq_len = np.max([truncate_at,df[:,1].max().astype(int)+1])
n_vars = df.shape[1]-2 # Drop unit_number and time
n_series = int(df[:,0].max())
feature_data = np.zeros([seq_len,n_series,n_vars])
times_to_event = np.zeros([seq_len,n_series,1])
seq_lengths = np.zeros([n_series])
mask = np.ones([seq_len,n_series,1])
@maximus009
maximus009 / outer_product_keras.py
Created February 16, 2017 20:06
Calculate the outer product/bilinear projection in Keras
from keras.layers import Lambda
from keras import backend as K
from numpy import newaxis
from keras.models import Model, Input
def outer_product(inputs):
"""
inputs: list of two tensors (of equal dimensions,
for which you need to compute the outer product
@cbaziotis
cbaziotis / AttentionWithContext.py
Last active April 25, 2022 14:37
Keras Layer that implements an Attention mechanism, with a context/query vector, for temporal data. Supports Masking. Follows the work of Yang et al. [https://www.cs.cmu.edu/~diyiy/docs/naacl16.pdf] "Hierarchical Attention Networks for Document Classification"
def dot_product(x, kernel):
"""
Wrapper for dot product operation, in order to be compatible with both
Theano and Tensorflow
Args:
x (): input
kernel (): weights
Returns:
"""
if K.backend() == 'tensorflow':
@thomasjungblut
thomasjungblut / xgb_bayes_opt_cv.py
Last active May 22, 2024 22:14
XGBoost hyper parameter optimization using bayes_opt
from bayes_opt import BayesianOptimization
from sklearn.cross_validation import KFold
import xgboost as xgb
def xgbCv(train, features, numRounds, eta, gamma, maxDepth, minChildWeight, subsample, colSample):
# prepare xgb parameters
params = {
"objective": "reg:linear",
"booster" : "gbtree",
"eval_metric": "mae",
@jkleint
jkleint / timeseries_cnn.py
Created July 29, 2016 04:05
Example of using Keras to implement a 1D convolutional neural network (CNN) for timeseries prediction.
#!/usr/bin/env python
"""
Example of using Keras to implement a 1D convolutional neural network (CNN) for timeseries prediction.
"""
from __future__ import print_function, division
import numpy as np
from keras.layers import Convolution1D, Dense, MaxPooling1D, Flatten
from keras.models import Sequential
@balzer82
balzer82 / TimeSeries-Decomposition.ipynb
Last active June 20, 2022 14:38
TimeSeries Decomposition in Python with statsmodels and Pandas
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
def determine_feature_importance(df):
#Determines the importance of individual features within a dataframe
#Grab header for all feature values excluding score & ids
features_list = df.columns.values[4::]
print "Features List: \n", features_list
#set X equal to all feature values, excluding Score & ID fields
X = df.values[:,4::]
#set y equal to all Score values
@karpathy
karpathy / min-char-rnn.py
Last active May 22, 2024 08:28
Minimal character-level language model with a Vanilla Recurrent Neural Network, in Python/numpy
"""
Minimal character-level Vanilla RNN model. Written by Andrej Karpathy (@karpathy)
BSD License
"""
import numpy as np
# data I/O
data = open('input.txt', 'r').read() # should be simple plain text file
chars = list(set(data))
data_size, vocab_size = len(data), len(chars)