Skip to content

Instantly share code, notes, and snippets.

Avatar

Gavin Gray gngdb

View GitHub Profile
@gngdb
gngdb / mosh-algo
Created Sep 9, 2019
Script for easy mosh connections
View mosh-algo
#!/bin/bash
ALGO_IP=`ip addr | awk '
/^[0-9]+:/ {
sub(/:/,"",$2); iface=$2 }
/^[[:space:]]*inet / {
split($2, a, "/")
print iface" : "a[1]
}' | grep tun | cut -b 8- | cut -d "." -f 1-3 | sed 's/$/.1/'`
mosh --ssh="ssh -i algo.pem" ubuntu@$ALGO_IP
@gngdb
gngdb / cloudshell-algo-reqs.sh
Last active Sep 6, 2019
Script to install algo requirements to a cloud shell environment.
View cloudshell-algo-reqs.sh
sudo apt-get update && sudo apt-get install \
build-essential \
libssl-dev \
libffi-dev \
python-dev \
python-pip \
python-setuptools \
python-virtualenv -y
sudo pip2 install -r requirements.txt
@gngdb
gngdb / twitter_ui.blocklist
Created Aug 2, 2019
uBlock origin rules for a cleaner Twitter (removes search as well because I never use it).
View twitter_ui.blocklist
! 6/23/2019 https://twitter.com
twitter.com##.trends.Trends.module
twitter.com##.r-1adg3ll.css-1dbjc4n > .css-1dbjc4n > .r-1sp51qo.r-1wtj0ep.r-qklmqi.r-rull8r.r-my5ep6.css-1dbjc4n
twitter.com##div.r-1adg3ll.r-qklmqi.r-my5ep6.css-1dbjc4n:nth-of-type(2) > .r-6416eg.r-o7ynqc.r-1w50u8q.r-utggzx.r-6koalj.r-1loqt21.css-1dbjc4n
twitter.com##.r-1l5qxre.css-1dbjc4n
! 6/24/2019 https://twitter.com
twitter.com##div.r-1adg3ll.r-qklmqi.r-my5ep6.css-1dbjc4n:nth-of-type(3) > .r-6416eg.r-o7ynqc.r-1xtiow5.r-5f36wq.r-6koalj.r-1loqt21.css-1dbjc4n
twitter.com##div.r-1adg3ll.r-qklmqi.r-my5ep6.css-1dbjc4n:nth-of-type(4) > .r-6416eg.r-o7ynqc.r-1xtiow5.r-5f36wq.r-6koalj.r-1loqt21.css-1dbjc4n
twitter.com##div.r-1adg3ll.r-qklmqi.r-my5ep6.css-1dbjc4n:nth-of-type(5) > .r-6416eg.r-o7ynqc.r-1xtiow5.r-5f36wq.r-6koalj.r-1loqt21.css-1dbjc4n
View roaming-ssh.md

So, you want to be able to work from anywhere. You want to be on a mountain somewhere, two bars of 3G signal, and you forward that to your laptop with a WiFi hotspot. Open your laptop and your shell on remote is already open and as responsive as possible. Work/life balance? With power like this, who cares?

Problem Scenario

Often, in academic institutions at least, you have the following situation:

@gngdb
gngdb / ssh.py
Last active Jan 18, 2019
Use boto3 to generate an ssh config file entry for all your running instances (checks to see if you've already added them as well).
View ssh.py
import os
import boto3
if __name__ == '__main__':
session = boto3.Session()
ec2 = session.resource('ec2')
ec2c = session.client('ec2')
# add active instances to the user's ssh config file
instances = ec2.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
@gngdb
gngdb / listregions.py
Last active Jan 5, 2019
Where in the world is Carmen Sandiego? If "Carmen Sandiego" is the cheapest spot price on AWS EC2:
View listregions.py
import boto3
class Session(boto3.Session):
def __enter__(self):
return self
def __exit__(self, *args):
del self
with Session() as sess:
# get names of regions
View imagenet-validation.py
# dependencies
import re
import os
import argparse
import torch
from tqdm import tqdm
#import cv2
import numpy as np
import torch.utils.data
import torchnet as tnt
@gngdb
gngdb / index_select_example.py
Last active Nov 20, 2018
index_select and then reshaping is faster than just indexing?
View index_select_example.py
import torch
if __name__ == '__main__':
X = torch.randn(100)
out_shape = (100,100)
idxs = torch.randint(high=100, size=out_shape).long()
assert torch.abs(X[idxs] - X.index_select(0, idxs.view(-1)).view(*out_shape)).max() < 1e-3
from timeit import timeit
setup = 'import torch; X = torch.randn(100); out_shape=(100,100); idxs = torch.randint(high=100, size=out_shape).long()'
print("X[idxs]: ", timeit("_ = X[idxs]", setup=setup, number=100))
@gngdb
gngdb / hashing.py
Last active Nov 20, 2018
Inefficient HashedNet implementation: https://arxiv.org/abs/1504.04788
View hashing.py
# implementation of https://arxiv.org/abs/1504.04788
import torch
import torch.nn as nn
import torch.nn.functional as F
import xxhash
class HashFunction(object):
"""Hash function as described in the paper, maps a key (i,j) to a natural number
in {1,...,K_L}"""
You can’t perform that action at this time.