Skip to content

Instantly share code, notes, and snippets.

View gavincangan's full-sized avatar

Barnabas Gavin Cangan gavincangan

View GitHub Profile
@gavincangan
gavincangan / AttentionWithContext.py
Created July 17, 2018 21:22 — forked from nigeljyng/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
@gavincangan
gavincangan / get_available_gpus.py
Created July 12, 2018 17:15 — forked from jovianlin/get_available_gpus.py
Get List of Devices in TensorFlow
from tensorflow.python.client import device_lib
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
get_available_gpus()
@gavincangan
gavincangan / beamsearch.py
Created July 18, 2018 17:23 — forked from udibr/beamsearch.py
beam search for Keras RNN
# variation to https://github.com/ryankiros/skip-thoughts/blob/master/decoding/search.py
def keras_rnn_predict(samples, empty=empty, rnn_model=model, maxlen=maxlen):
"""for every sample, calculate probability for every possible label
you need to supply your RNN model and maxlen - the length of sequences it can handle
"""
data = sequence.pad_sequences(samples, maxlen=maxlen, value=empty)
return rnn_model.predict(data, verbose=0)
def beamsearch(predict=keras_rnn_predict,
@gavincangan
gavincangan / tf_beam_decoder.py
Created July 19, 2018 20:38 — forked from igormq/tf_beam_decoder.py
Tensorflow Beam Search
import tensorflow as tf
def beam_decoder(decoder_inputs, initial_state, cell, loop_function, scope=None,
beam_size=7, done_token=0
):
"""
Beam search decoder
Args:
decoder_inputs: A list of 2D Tensors [batch_size x input_size].
import time
import math
def find_fact(num, cur_quot=[]):
ix = 3
if(num < 2):
return cur_quot
while(ix < math.sqrt(num)):
if(num % ix == 0):
cur_quot.append(ix)
@gavincangan
gavincangan / build_and_run_cpp.sh
Created September 3, 2018 23:31
Build and run a C++ file using g++
#!/bin/bash
cpp_file_to_build=$1
output_file=$(pwd)/${cpp_file_to_build/\.cpp/\.o}
rename_output_file=$(pwd)/zz_${cpp_file_to_build/\.cpp/\.o}ld
mv $output_file $rename_output_file
g++ -std=gnu++11 -Wall $cpp_file_to_build -o $output_file;
@gavincangan
gavincangan / notes.bat.sh
Created January 29, 2019 17:30
Terminal / Cmd prompt - notes
# Check for open ports in a range
for i in $(seq 20 99); do netcat -v -n -z -w 1 192.168.5.$i 22; done
# Windows - show WiFi password
netsh wlan show profile name=”<Profile-Name>” key=clear
# Windows - show all devices in network
arp -a
# Convert images or other files to pdf using ImageMagick

Keybase proof

I hereby claim:

  • I am gavincangan on github.
  • I am bgavin (https://keybase.io/bgavin) on keybase.
  • I have a public key ASAzVotPWrTux92cdLLsndyYfpkLiCaj9vofPyQzC1yAFQo

To claim this, I am signing this object:

@gavincangan
gavincangan / glob_cpp.cpp
Created February 27, 2019 18:23
Glob C++
/*
* Source: https://stackoverflow.com/questions/8401777/simple-glob-in-c-on-unix-system
*/
#include <glob.h> // glob(), globfree()
#include <string.h> // memset()
#include <vector>
#include <stdexcept>
#include <string>
#include <sstream>
@gavincangan
gavincangan / check_cov_psd.m
Created March 5, 2019 19:45
Check whether a kernel results in a positive semi-definite covariance matrix using rejection sampling
% https://stats.stackexchange.com/a/394487/204863
clear; close all;
num_points = 100;
xrange = [-100, 100];
x = xrange(1) + 2 * xrange(2) * rand(num_points, 1);
yrange = [0, 20];
y = yrange(1) + 2 * yrange(2) * rand(num_points, 1);