Skip to content

Instantly share code, notes, and snippets.

View vadimkantorov's full-sized avatar
looking for an internship for summer/fall 2021

Vadim Kantorov vadimkantorov

looking for an internship for summer/fall 2021
View GitHub Profile
vadimkantorov /
Created March 25, 2015 14:09
A script to install pip packages locally
#! /bin/bash
# Usage example: bash ipython
# Add the following export to your .bashrc or .profile to use the unpacked packages
# export PYTHONPATH=$HOME/user_lib/pip_user/lib/python2.7/site-packages:$PYTHONPATH
mkdir -p $INSTALL_DIR
PYTHONUSERBASE=$INSTALL_DIR pip install --user "$@"
View fsharp_units_of_measure_sample.fsx
open Microsoft.FSharp.Data.UnitSystems.SI.UnitNames
open Microsoft.FSharp.Data.UnitSystems.SI.UnitSymbols
type mm =
static member perMeter = 1e+3<mm/m>
let pi = System.Math.PI
let epsilon_0 = 8.854187817e-12<F m^-1>
let elementary_charge = 1.602176565e-19<coulomb>
import sys
import psutil
import subprocess
import datetime
import time
bashScriptPath = sys.argv[1]
stdoutPath, stderrPath = sys.argv[2:] or (None, None)
peakRssBytes = 0
vadimkantorov / matlab_embed_figure_in_html.m
Last active August 19, 2016 15:46
A Matlab snippet to output Base64-encoded img's to be used for HTML visualizations
View matlab_embed_figure_in_html.m
f = figure();
plot(1:10, 1:10);
rgbData = figToImStream('figHandle', f, 'imageFormat', 'jpg', 'outputType', 'uint8');
%print(f, '-djpg', '/tmp/vis.jpg'); rgbData = fread(fopen('/tmp/vis.jpg'), 'uint8=>uint8'); % replace previous line by this line in case of failure
fprintf('<img src="data:image/jpeg;base64,%s" />', char(org.apache.commons.codec.binary.Base64.encodeBase64(rgbData))');
vadimkantorov /
Created September 7, 2016 16:01
A Bash script for extracting URLs from Facebook message archive file (messages.htm) for a given user name. The script will print most recent links first.
# Usage: bash "Vadim Kantorov" > urls.txt
cat messages.htm | xpath -q -e "//div[@class='thread' and contains(text(), '$1')]" | grep -o 'http[^ <]*' | tac
vadimkantorov /
Last active September 12, 2016 19:35
A script to download and unpack and use Ubuntu packages without root permissions
#! /bin/bash
# Usage example: bash libopencv-dev ~/deb
# Author: Vadim Kantorov
# Add the following exports to your .bashrc or .profile to use the unpacked packages
# export PATH=$HOME/deb/usr/bin:$PATH
# export LD_LIBRARY_PATH=$HOME/deb/usr/lib:$HOME/deb/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
# export CPATH=$HOME/deb/usr/include:$CPATH
vadimkantorov / DynamicView.lua
Last active November 10, 2016 06:32
DynamicView module for Torch that views an input tensor dynamically at runtime with user-provided function of its size. See an example of total variation computation below.
View DynamicView.lua
function DynamicView(getSizeTable)
local module = nn.View(-1)
module.updateOutput = function(self, input) return nn.View.updateOutput(self:resetSize(unpack(getSizeTable(input:size()))), input) end
return module
-- function TotalVariation() --accepts 4D tensors
-- local sk = torch.Tensor(2, 1, 2,2)
-- sk[1][1]:copy(torch.Tensor(2,2):set(torch.Storage({-1, 1, 0, 0})))
-- sk[2][1]:copy(torch.Tensor(2,2):set(torch.Storage({-1, 0, 1, 0})))
vadimkantorov / vis_html_table.lua
Last active November 24, 2016 14:24
Torch routine for generating HTML tables with embedded images
View vis_html_table.lua
require 'image'
require 'base64' -- luarocks install lbase64
function vis_html_table(vis_path)
return {
file =, 'w'):write('<html><body><table>'),
close = function(self)
figure = function(self, img, top_legend, bottom_legend, rescale, width, height)
vadimkantorov / whiten.lua
Last active January 8, 2017 13:27
Torch routine and module for whitening, supporting large matrices, e.g. 5K x 10K
View whiten.lua
-- Produces mean and the whitening transform of matrix x[n, d] holding n data points of dimension k, using rank k approximation
-- Auxiliary gaussian matrix g[d, f * k] will use f = 3 by default
-- Original paper:
function whiten(x, k, f)
local n, d = unpack(x:size():totable())
local mean = x:mean(1)
local g =, math.min((f or 3) * k, x:size(2)))
vadimkantorov / TripletEmbeddingCriterion.lua
Last active March 1, 2017 19:27
A Torch implementation of triplet loss using autograd
View TripletEmbeddingCriterion.lua
autograd = require 'autograd'
-- assumes batch size = 3 (anchor, positive, negative)
function TripletEmbeddingCriterion(margin)
local auto_criterion = autograd.nn.AutoCriterion(return torch.CharTensor(3):random(string.byte('A'), string.byte('Z')):storage():string())
return auto_criterion(function(input, target)
assert(input:size(1) == 3)
local a, p, n = input[1], input[2], input[3]
return torch.sum(torch.cmax(torch.sum(torch.pow(a - p, 2), 1) - torch.sum(torch.pow(a - n, 2), 1) + margin, 0))