Skip to content

Instantly share code, notes, and snippets.

View chiragjn's full-sized avatar
⌨️
Programming with Python mostly but sometimes Go and Typescript

Chirag Jain chiragjn

⌨️
Programming with Python mostly but sometimes Go and Typescript
View GitHub Profile
@chiragjn
chiragjn / SingleCameraCaptureSnippet.java
Last active February 21, 2016 13:45
Android Quick(and possibly dirty) Single Image Capture With Optional JPEG Compression
//Add the following to your Activity
//Modify context and onActivityResult() if using Fragments
//Call startImageCapture() to initiate
//File is returned in onActivityResult()
private Uri mImageUri;
private static final int CAMERA_REQUEST = 1888;
public void startImageCapture(){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@chiragjn
chiragjn / classification_with_pytorch.ipynb
Created June 25, 2017 12:31
Good old MNIST with PyTorch
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@chiragjn
chiragjn / next_permutation.py
Last active August 15, 2017 21:35
C++ std::next_permutation with Python
def next_permutation(mi):
"""
Modifies in place to give lexicographically smallest permutation of mi greater than the given permutation
Args:
mi (iterable): Mutable iterable, must implement __len__
Returns:
bool: True if next permutation was found, False otherwise
"""
if len(mi) <= 1:
@chiragjn
chiragjn / vecvis.py
Created March 21, 2018 13:35
Put your vectors onto tensorboard!
# encoding: utf-8
"""
Original credits
@author: BrikerMan
@contact: [email protected]
@blog: https://eliyar.biz
@version: 1.0
@license: Apache Licence
@file: w2v_visualizer.py
@time: 2017/7/30 上午9:37
@chiragjn
chiragjn / textcat.ipynb
Created October 1, 2018 16:35
Notebook for my Intro to NLP talk for undergraduates
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@chiragjn
chiragjn / rowwise_cosine_similarity.py
Last active August 14, 2019 09:49
Given two 2D matrices of shape N x D, compute cosine similarity between A[i, :] and B[i, :]
import numpy as np
import scipy.sparse
def cosine_similarity_kernel(
matrix_a: Union[np.array, scipy.sparse.csr_matrix],
matrix_b: Union[np.array, scipy.sparse.csr_matrix],
eps: float = 1e-9,
) -> np.array:
if scipy.sparse.issparse(matrix_a) or scipy.sparse.issparse(matrix_b):
a_norm = (scipy.linalg.norm(matrix_a, axis=1) + eps).flatten()
@chiragjn
chiragjn / feedforwardadapterlayer.py
Last active July 29, 2019 10:08
Adapter FF layer as described in Adapter-bert
import numpy as np
import tensorflow as tf
K = tf.keras.backend
def gelu(x):
"""Gaussian Error Linear Unit.
This is a smoother version of the RELU.
Original paper: https://arxiv.org/abs/1606.08415
Args:
from contextlib import contextmanager
import cProfile, pstats, io
from timeit import default_timer as timer
from pyinstrument import Profiler
from pyinstrument.renderers import ConsoleRenderer
@contextmanager
def pyinst(r=None):
r = {} if r is None else r