Skip to content

Instantly share code, notes, and snippets.

View hackintoshrao's full-sized avatar
📚
Exploring AI agents on code search and understanding

Karthic Rao hackintoshrao

📚
Exploring AI agents on code search and understanding
View GitHub Profile
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
import numpy as np
def pad(sequences, max_len):
return pad_sequences(sequences, maxlen=max_len)
def categorize_labels(labels) :
return to_categorical(np.asarray(labels))
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
def tokenize(top_n_words, texts):
tokenizer = Tokenizer(num_words=top_n_words)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)
word_index = tokenizer.word_index
@hackintoshrao
hackintoshrao / AttentionWithContext.py
Created August 21, 2017 06:39 — 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"
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':
@hackintoshrao
hackintoshrao / kalman.cpp
Created July 20, 2017 07:12
Kalman filter update and prediction
// Write a function 'filter()' that implements a multi-
// dimensional Kalman Filter for the example given
//============================================================================
#include <iostream>
#include "Dense"
#include <vector>
using namespace std;
using namespace Eigen;
@hackintoshrao
hackintoshrao / measure_and_update.py
Last active July 17, 2017 07:15
Measure and update iteration in localization of self driving cars.
# Write a program that will iteratively update and
# predict based on the location measurements
# and inferred motions shown below.
def update(mean1, var1, mean2, var2):
new_mean = float(var2 * mean1 + var1 * mean2) / (var1 + var2)
new_var = 1./(1./var1 + 1./var2)
return [new_mean, new_var]
def predict(mean1, var1, mean2, var2):
@hackintoshrao
hackintoshrao / batches.py
Created July 14, 2017 07:20
Convert the RNN's to batches
def get_batches(arr, n_seqs, n_steps):
'''Create a generator that returns batches of size
n_seqs x n_steps from arr.
Arguments
---------
arr: Array you want to make batches from
n_seqs: Batch size, the number of sequences per batch
n_steps: Number of sequence steps per batch
'''
@hackintoshrao
hackintoshrao / weight_update.py
Created June 28, 2017 12:51
Example of math for weight udpate
import numpy as np
def sigmoid(x):
"""
Calculate sigmoid
"""
return 1/(1+np.exp(-x))
def sigmoid_prime(x):
"""
@hackintoshrao
hackintoshrao / fit-poly.py
Created June 18, 2017 03:17
Code snippet for fitting a polynomial to the lane lines
# Use histogram on both side of the lane to obtain starting
# point for lane line scanning.
midpoint = np.int(histogram.shape[0]/2)
leftx_base = np.argmax(histogram[:midpoint])
rightx_base = np.argmax(histogram[midpoint:]) + midpoint
out_img = np.dstack((binary_warped, binary_warped, binary_warped))*255
# Choose the number of sliding windows
nwindows = 9
@hackintoshrao
hackintoshrao / color_hist.py
Created June 14, 2017 12:07
Histogram of color instensity distribution of image
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread('cutout1.jpg')
# Define a function to compute color histogram features
def color_hist(img, nbins=32, bins_range=(0, 256)):
# Compute the histogram of the RGB channels separately
@hackintoshrao
hackintoshrao / box.py
Created June 14, 2017 06:20
draw rectangle boxes at given coordinates on the given image.
import np as numpy
import cv2
"""
draw rectangle boxes at given coordinates on the given image.
"""
def draw_boxes(img, bboxes, color=(0, 0, 255), thick=6):
# make a copy of the image
draw_img = np.copy(img)
# draw each bounding box on your image copy using cv2.rectangle()