Skip to content

Instantly share code, notes, and snippets.

the-bass / pre-commit
Created March 12, 2021 06:27
pre-commit hook for Rust projects
# Assigns stdin to keyboard - otherwise we're not able to listen to user input below.
exec < /dev/tty
message_prefix="[GIT HOOK | pre-commit]"
echo "$message_prefix Running clippy."
if ! cargo clippy
the-bass /
Last active November 24, 2023 09:56
Calculating the confusion matrix between two PyTorch tensors (a batch of predictions) - Last tested with PyTorch 0.4.1
import torch
def confusion(prediction, truth):
""" Returns the confusion matrix for the values in the `prediction` and `truth`
tensors, i.e. the amount of positions where the values of `prediction`
and `truth` are
- 1 and 1 (True Positive)
- 1 and 0 (False Positive)
- 0 and 0 (True Negative)
the-bass /
Last active January 1, 2024 08:28
Rename the parameters of a PyTorch module's saved state dict. Last tested with PyTorch 1.0.1.
import torch
from collections import OrderedDict
def rename_state_dict_keys(source, key_transformation, target=None):
source -> Path to the saved state dict.
key_transformation -> Function that accepts the old key names of the state
dict as the only argument and returns the new key name.
target (optional) -> Path at which the new state dict should be saved
the-bass / get_flickr_img_url.js
Created April 2, 2018 10:57
Display the URL of an image on flickr in the JS console.
var testimonials = document.querySelectorAll('img.main-photo');, function(element, index) {
the-bass / application.html.erb
Last active April 15, 2020 15:01
Using Google Analytics with Rails 5 and Turbolinks 5. This code is taken from the conversation between @preetpalS and @packagethief on
<%# Put this code snippet between the <head></head>-tags in your application layout and %>
<%# replace 'UA-XXXXXXXX-X' with your own unique Google Analytics Tracking ID %>
<%# ... %>
<%# ... %>
<% if Rails.env.production? %>
<script type="text/javascript">
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),