Skip to content

Instantly share code, notes, and snippets.

@the-bass
the-bass / pre-commit
Created March 12, 2021 06:27
pre-commit hook for Rust projects
#!/bin/sh
# 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
then
@the-bass
the-bass / confusion_matrix_between_two_pytorch_tensors.py
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
the-bass / rename_state_dict_keys.py
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
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');
Array.prototype.forEach.call(testimonials, function(element, index) {
console.log(element.src);
});
@the-bass
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 https://github.com/turbolinks/turbolinks/issues/73.
<%# 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 %>
<%# ... %>
<head>
<%# ... %>
<% if Rails.env.production? %>
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),