Skip to content

Instantly share code, notes, and snippets.

View chrislemke's full-sized avatar
🍩
Workinonit

Chris Lemke chrislemke

🍩
Workinonit
View GitHub Profile
@chrislemke
chrislemke / pinjector.py
Last active October 28, 2023 10:15
pinjector - automatically inject 1Password(s) from .env.op files.
import functools
import os
import shutil
import subprocess
from contextlib import contextmanager
from typing import Any, Callable, Dict, Iterator, Tuple
from dotenv import load_dotenv
@chrislemke
chrislemke / eval_clf.py
Last active March 22, 2024 09:08
Evaluates a model and outputs a collection of metrics: `ROC AUC`, `Log loss`, `Accuracy`, `Balanced accuracy`, `Average precision score`, `Calibrated pr gain auc score`, and `precision`, `recall`, `fbeta_score` for both classes
from typing import Tuple
import matplotlib.pyplot as plt
import numpy as np
from numpy.typing import NDArray
import pandas as pd
from sklearn.metrics import (
ConfusionMatrixDisplay,
accuracy_score,
auc,
@chrislemke
chrislemke / Batch matrix multiplication.py
Last active August 16, 2021 13:40
Batch matrix multiplication
import torch
# Batch matrix multiplication
X = torch.arange(24).reshape(2, 3, 4)
Y = torch.arange(40).reshape(2, 4, 5)
A = torch.einsum('ijk, ikl->ijl', X, Y)
torch.bmm(X, Y)
print(A)
@chrislemke
chrislemke / Matrix-Matrix multiplication.py
Last active August 15, 2021 05:36
Matrix-Matrix multiplication
import torch
# Matrix-Matrix multiplication
X = torch.arange(6).reshape(2, 3)
Y = torch.arange(12).reshape(3, 4)
A = torch.einsum('ij, jk->ik', X, Y)
torch.mm(X, Y)
print(A)
@chrislemke
chrislemke / Matrix-Vector multiplication.py
Created August 15, 2021 05:35
Matrix-Vector multiplication
import torch
# Matrix-Vector multiplication
X = torch.rand((3, 3))
y = torch.rand((1, 3))
A = torch.einsum('ij, kj->ik', X, y)
torch.mm(X, torch.transpose(y, 0, 1)) # or torch.mm(X, y.T)
print(A)
@chrislemke
chrislemke / Outer product.py
Created August 15, 2021 05:34
Outer product
import torch
# Outer product
v = torch.rand((3))
t = torch.rand((3))
A = torch.einsum('i, j->ij', v, t)
torch.outer(v, t)
print(A)
@chrislemke
chrislemke / Dot product.py
Created August 14, 2021 12:33
Dot product
import torch
# Dot product
v = torch.rand((3))
c = torch.rand((3))
a = torch.einsum('i, i->', v, c)
torch.dot(v, c)
print(a) # tensor(0.5750)
@chrislemke
chrislemke / Element wise multiplication.py
Created August 14, 2021 12:18
Element wise multiplication
import torch
# Element wise multiplication
X = torch.rand((3, 2))
Y = torch.rand((3, 2))
A = torch.einsum('ij, ij->ij', X, Y)
torch.mul(X, Y) # or X * Y
print(A)
@chrislemke
chrislemke / Row and column summation.py
Last active August 14, 2021 12:04
Row and column summation
import torch
X = torch.rand((2, 3))
# Row summation
a = torch.einsum('ij->i', X)
torch.sum(X, axis=1)
print(a) # tensor([1.4088, 1.7803])
@chrislemke
chrislemke / Summation.py
Last active August 14, 2021 12:02
Summation
import torch
# Summation
X = torch.rand((2, 3))
a = torch.einsum('ij->', X)
torch.sum(X)
print(a) # tensor(3.5585)