Skip to content

Instantly share code, notes, and snippets.

View samarthbhargav's full-sized avatar

Samarth Bhargav samarthbhargav

View GitHub Profile
@samarthbhargav
samarthbhargav / pairwise_kl_torch.py
Created January 25, 2024 14:07
Pairwise KL Divergence between two diagonal gaussian distributions
def pairwise_kl_divergence(mean1, var1, mean2, var2):
# Ensure that variances are non-negative
var1 = torch.clamp(var1, min=1e-10)
var2 = torch.clamp(var2, min=1e-10)
k = mean1.size(1)
# shape = BZ_1xD
logvar1 = torch.log(var1)
# log determinant
index_sets = {1, 2}
list_of_metrics = [
("ERR", err),
("MAP", average_precision),
("Recall@1",recall_at_1),
("Recall@5", recall_at_5),
("Recall@10", recall_at_10),
("Precision@1", precision_at_1),
("Precision@5", precision_at_5),
@samarthbhargav
samarthbhargav / CUDA Availability in Torch
Created September 24, 2018 10:00
One liner to test if cuda is available
python -c "import torch; print(torch.cuda.is_available())"
@samarthbhargav
samarthbhargav / python_stop_thread.py
Last active June 20, 2022 09:50
A 'stoppable' thread for Python
# source: https://www.safaribooksonline.com/library/view/python-cookbook-2nd/0596007973/ch09s03.html
import threading
class TestThread(threading.Thread):
def _ _init_ _(self, name='TestThread'):
""" constructor, setting initial variables """
self._stopevent = threading.Event( )
self._sleepperiod = 1.0
threading.Thread._ _init_ _(self, name=name)
def run(self):
@samarthbhargav
samarthbhargav / scrape_xkcd.py
Last active December 12, 2017 15:57
XKCD Scraper - using BeautifulSoup and requests
from bs4 import BeautifulSoup
import requests
import shutil
import glob
from os import listdir
from os.path import isfile, join
def save_image(url, filename):
response = requests.get(url, stream=True)
@samarthbhargav
samarthbhargav / timer.py
Created December 18, 2014 05:52
a simple profiler for python
# source: http://www.huyng.com/posts/python-performance-analysis/
import time
class Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
def __enter__(self):
self.start = time.time()
return self
@samarthbhargav
samarthbhargav / powerset.py
Created December 4, 2014 11:17
Recursive generation of a power set
# as seen in 6.00.2x, Week 6, Lecture 1, Video 4
def powerset(iterable):
if len(iterable) == 0:
return [[]]
smaller = powerset(iterable[1:])
withElem = []
for s in smaller:
withElem.append( s + [iterable[0]])
@samarthbhargav
samarthbhargav / .bashrc
Last active March 3, 2018 10:01
bashrc for Hadoop
# Hadoop Properties
export HADOOP_PREFIX=/usr/local/hadoop # installation location
export HADOOP_HOME=$HADOOP_PREFIX
export HADOOP_COMMON_HOME=$HADOOP_PREFIX
export HADOOP_CONF_DIR=$HADOOP_PREFIX/etc/hadoop
export HADOOP_HDFS_HOME=$HADOOP_PREFIX
export HADOOP_MAPRED_HOME=$HADOOP_PREFIX
export HADOOP_YARN_HOME=$HADOOP_PREFIX
export PATH=$PATH:$HADOOP_PREFIX/bin
@samarthbhargav
samarthbhargav / yarn-site.xml
Created October 17, 2014 09:21
Conf for Hadoop
<?xml version="1.0"?>
<configuration>
<property>
<name>yarn.nodemanager.resource.memory-mb</name>
<value>8192</value>
</property>
<property>
<name>yarn.scheduler.maximum-allocation-mb</name>
<value>4096</value>
</property>
@samarthbhargav
samarthbhargav / mapred-site.xml
Created October 17, 2014 09:18
Conf for Hadoop
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
<property>
<name>mapreduce.jobhistory.address</name>
<value>localhost:10020</value>