Skip to content

Instantly share code, notes, and snippets.

View JosephCatrambone's full-sized avatar

Joseph Catrambone JosephCatrambone

View GitHub Profile
@JosephCatrambone
JosephCatrambone / spiral_iterator.py
Last active March 4, 2024 06:23
Discrete Grid Spiral Iterator
def square_iterator(x: int, y: int):
"""Generate all the points on a square 2d lattice, spiraling out from the center. Modified from a solution by Nikita Rybak. https://stackoverflow.com/a/3706260"""
dx = 1
dy = 0
segment_length = 1
leg_iterations = 0
while True:
x += dx
y += dy
@JosephCatrambone
JosephCatrambone / cluster_lines.py
Created November 17, 2023 18:28
Cluster Suggestions
import itertools
import sys
import numpy
import torch
from scipy.cluster.vq import kmeans, kmeans2
from transformers import AutoTokenizer, GPT2Model
def magnitude(x):
return numpy.dot(x, x)**0.5
@JosephCatrambone
JosephCatrambone / lib.rs
Last active March 8, 2024 01:32
Embedding GPT-2 in Godot via Rust
mod ml_thread;
use gdnative::prelude::{godot_print, methods, Method, NativeClass, Node as GDNode, InitHandle, godot_init};
use ml_thread::start_language_model_thread;
use std::sync::mpsc::{channel, Receiver, RecvError, Sender, SendError};
const MAX_INPUT_LENGTH: usize = 512;
const BATCH_SIZE: usize = 1;
@JosephCatrambone
JosephCatrambone / union_find.py
Created July 11, 2022 23:51
A Union-Find / Disjoint-Set Python class
class UnionFind:
def __init__(self):
self.parents = dict()
def add(self, node_id:int):
self.parents[node_id] = node_id
def union(self, node_a:int, node_b:int):
min_parent = min(self.parents[node_a], self.parents[node_b])
self.parents[node_a] = min_parent
@JosephCatrambone
JosephCatrambone / tinypico_wireless_morse_keyboard.ino
Created May 15, 2022 21:03
TinyPICO BlueTooth BLE Morse Keyboard
#include <BleKeyboard.h>
#include <TinyPICO.h>
TinyPICO tinypico = TinyPICO();
BleKeyboard keyboard; // keyboard("Name", "manufacturer", battery_level_100)
// a .-
// b -...
// c -.-.
// d -..
@JosephCatrambone
JosephCatrambone / DecisionTree.gd
Created February 25, 2022 04:32
Basic Decision Tree in GDScript
# Sample Usage:
# var day_features = [
# # freezing, raining, foggy, sunny
# [0, 1, 0, 0],
# [0, 0, 0, 1],
# [0, 0, 0, 0],
# [0, 0, 1, 0],
# [1, 0, 0, 1],
# [1, 1, 0, 0],
# [1, 0, 1, 0],
@JosephCatrambone
JosephCatrambone / decision_tree.rs
Created April 7, 2021 02:15
A Rust-language decision tree implementation with no external dependencies.
//use serde::{Serialize, Deserialize};
use std::collections::HashMap;
// If you have serde, serialization is available with this derive chain:
//#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[derive(Clone, Debug, Default)]
pub struct DecisionTree {
feature: usize,
threshold: f32,
@JosephCatrambone
JosephCatrambone / basic_backprop.py
Created February 23, 2021 18:34
Reverse Mode Automatic Differentiation
import numpy
import random
class Graph:
def __init__(self):
self.nodes = list()
self.last_tape = None
def forward(self, variables, output_node):
for n in self.nodes:
n.forward(variables)
@JosephCatrambone
JosephCatrambone / owa.tracker-combined-latest.minified.js
Created February 5, 2021 05:43
owa.tracker-combined-latest.minified.js - The Great Suspender Breakdown
(function(exports) {
if (!this.JSON) {
this.JSON = {};
}
(function() {
"use strict";
function f(n) {
return n < 10 ? "0" + n : n;
}
if (typeof Date.prototype.toJSON !== "function") {
@JosephCatrambone
JosephCatrambone / gan.py
Created September 7, 2020 18:57
Cleaned PyTorch GAN Code
import numpy
from pathlib import Path
import sys
import torch
from torch import nn
import torchvision
from torchvision import datasets
from torchvision import transforms