Skip to content

Instantly share code, notes, and snippets.

# function to untar arxiv source files into a folder
untar_arxiv () {
# Check if the file has a .tar.gz suffix
if [[ $1 == *.tar.gz ]]; then
# Extract the base name without the .tar.gz suffix
base_name=$(basename "$1" .tar.gz)
mkdir -p "$base_name"
tar -xvf "$1" -C "$base_name"
else
# Add the .tar.gz suffix to the filename
@gngdb
gngdb / itertools_einsum.py
Created April 10, 2024 22:13
einsum implemented with `itertools.product`
import torch
import itertools
from collections import OrderedDict
def einsum_itertools(equation, *operands, verbose=False):
# Parse the equation
input_labels, output_labels = equation.split('->')
input_labels = input_labels.split(',')
if verbose:
print(f"{input_labels=} {output_labels=}")
@gngdb
gngdb / Efficient Top 1 Error Scatter Plot.ipynb
Last active February 5, 2023 17:19
Graphing performance for a report.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@gngdb
gngdb / save_bits.py
Created August 27, 2018 10:48
Save a string of ones and zeros as those ones and zeros in binary using Python.
import random
from io import StringIO
def write_bitstream(fname, bits):
# bits are a string of ones and zeros, based on this
# stackoverflow answer: https://stackoverflow.com/a/16888829/6938913
# was broken due to utf-8 encoding using up to 4 bytes: https://stackoverflow.com/a/33349765/6937913
sio = StringIO(bits)
with open(fname, 'wb') as f:
while 1:

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 / tpu_configssh.py
Last active October 11, 2022 19:23
Script to do the same thing as https://cloud.google.com/sdk/gcloud/reference/compute/config-ssh but works for TPU VMs
# script to add gcloud instances to ssh config
#
# Usage: tpu_configssh.py <instance_name> <instance_name> ...
#
# A version of this exists in gcloud compute config-ssh but it doesn't work for TPU VMs
#
# Works by parsing the output of dryrun mode of gcloud compute ssh, example:
# $ gcloud alpha compute tpus tpu-vm ssh instance-name --dry-run
# /usr/bin/ssh -t -i /home/user/.ssh/google_compute_engine -o CheckHostIP=no -o HashKnownHosts=no -o HostKeyAlias=<alias> -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/home/user/.ssh/google_compute_known_hosts user@IP
@gngdb
gngdb / Basin Hopping.ipynb
Last active September 24, 2022 12:03
An example using scipy.optimize's basin hopping with torch calculating gradients
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@gngdb
gngdb / .bashrc
Last active September 23, 2022 13:25
Add to bashrc to autocomplete nvim scp commands: `nvim <remote>:/path/to/file.py`
# autocomplete nvim scp commands
nvim () {
local params=();
while [[ ! -z $1 ]]; do
if [[ "$1" =~ ^[a-z0-9-]*:/.*$ ]]; then
params=("scp://${1/:\//\/\//}" "${params[@]}");
else
params+=("$1");
fi;
shift;
@gngdb
gngdb / example_usage.py
Last active May 18, 2022 05:32
Wrap PyTorch functions for scipy's optimize.minimize: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html (I also made a repo to do this https://github.com/gngdb/pytorch-minimize, although I had forgotten about this gist at the time)
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import numpy as np
from scipy import optimize
from obj import PyTorchObjective
@gngdb
gngdb / downloader.py
Last active July 26, 2021 20:48
Download utility for AIST++ edited to use aria2c https://google.github.io/aistplusplus_dataset/download.html
# coding=utf-8
# Copyright 2020 The Google AI Perception Team Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software