Skip to content

Instantly share code, notes, and snippets.

View alexshtf's full-sized avatar

Alex Shtof alexshtf

View GitHub Profile
@alexshtf
alexshtf / batch_iters.py
Last active August 31, 2024 19:53
Batch iterators
import torch
class BatchIter:
"""
tensors: feature tensors (each with shape: num_instances x *)
"""
def __init__(self, *tensors, batch_size, shuffle=True):
self.tensors = tensors
@alexshtf
alexshtf / torch_ops.py
Created August 15, 2024 11:23
pytorch ops for ranking
import torch
import torch.nn.functional as F
import copy
import math
def ipw_crossentropy(weights, scores, label):
propensities = torch.reciprocal(weights)
return (
-label * (scores + torch.log(propensities))
@alexshtf
alexshtf / constexpr_sqrt.cpp
Last active September 17, 2024 08:59
Constexpr version of c++ square root (for doubles)
#include <limits>
namespace Detail
{
double constexpr sqrtNewtonRaphson(double x, double curr, double prev)
{
return curr == prev
? curr
: sqrtNewtonRaphson(x, 0.5 * (curr + x / curr), curr);
}