Skip to content

Instantly share code, notes, and snippets.

View ryanpeach's full-sized avatar

Ryan Peach ryanpeach

View GitHub Profile
@ryanpeach
ryanpeach / main.rs
Last active September 16, 2024 02:45
My default main.rs with error flags and logging
#![deny(
missing_docs,
rustdoc::unescaped_backticks
)]
@ryanpeach
ryanpeach / Cargo.toml
Last active September 16, 2024 03:00
My favorite rust packages and settings
[profile.dev]
opt-level = 0
debug = true
[profile.release]
opt-level = 3
debug = false
[dependencies]
# Errors
@ryanpeach
ryanpeach / .pre-commit-config.yaml
Created September 11, 2024 19:43
My favorite pre-commits
exclude: "docs|.git"
default_stages: [commit]
fail_fast: false
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import random as rand
from functools import partial
def twiddle(run, args, p, dp, tol = 0.2, N = 100, logger = None):
""" Uses gradient descent to find the optimal value of p as input for function run.
run is a function which takes p as an argument and returns an error (with 0 being optimal) as an output.
dp is the initial magnitute for each index of p to begin
N is the max number of iterations, after which the best value of p is returned.
tol is the max error allowed, under which this function will terminate. """
best_err, best_p, best_dp, n = 1000000, None, None, 0
@ryanpeach
ryanpeach / earlystopping.py
Last active November 28, 2023 08:22
A Python 3 implementation of the early stopping algorithm described in the Deep Learning book by Ian Goodfellow. Untested, needs basic syntax correction.
""" Python 3 implementation of Deep Learning book early stop algorithm.
@book{Goodfellow-et-al-2016,
title={Deep Learning},
author={Ian Goodfellow and Yoshua Bengio and Aaron Courville},
publisher={MIT Press},
note={\url{http://www.deeplearningbook.org}},
year={2016}
}
"""
@ryanpeach
ryanpeach / ransac.py
Created December 1, 2016 04:16
An implementation of RANSAC with a linear fit model.
import numpy as np
import cv2
import random
import scipy.stats
# Matplotlib
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
@ryanpeach
ryanpeach / hungarian.py
Created September 28, 2016 18:03
A Python 2 implementation of both the Hungarian and Murty's Algorithm, which I believe runs in O(n^3) time, with sources, memoization, testing methods, and capable of being run in parallel instances with multiprocessing.pool.
# Ryan Peach 3/1/2016
# References Used for this Implementation
# https://en.wikipedia.org/wiki/Hungarian_algorithm
# https://github.com/bmc/munkres/blob/master/munkres.py
# http://csclab.murraystate.edu/bob.pilgrim/445/munkres.html
# ---
# No copying and pasting of online code was performed, though some code may turn out to be similar due to the standardized nature of this algorithm.
# This is a very different implementation due to the fact that it heavily uses numpy, vastly simplifies many poorly pythonized coding elements
# removes the "class" approach for a function based one. Etc.
# Improvements that need to be made is to require the matrix class, and to vectorize iterations through the matrix.
@ryanpeach
ryanpeach / timeout.py
Created September 23, 2016 06:57
Good for single loop timeout functions which may or may not contain a non-nested timeout element themselves. Ran into issues when nesting this function within one of it's own, timeout is not referenced within sub-scopes.
from time import time, sleep
from functools import wraps
import socket
import unittest
class TimeoutError(socket.timeout):
pass
none_val = lambda x: 0.0 if x is None else float(x) # Returns float(x), or 0 if x is None