Skip to content

Instantly share code, notes, and snippets.

View WangHexie's full-sized avatar
☹️
Failing

WangHexie WangHexie

☹️
Failing
View GitHub Profile
@koreyou
koreyou / bm25.py
Created November 1, 2019 05:26
Implementation of OKapi BM25 with sklearn's TfidfVectorizer
""" Implementation of OKapi BM25 with sklearn's TfidfVectorizer
Distributed as CC-0 (https://creativecommons.org/publicdomain/zero/1.0/)
"""
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from scipy import sparse
class BM25(object):
@oscarknagg
oscarknagg / projected_gradient_descent.py
Last active November 25, 2023 03:52
Gist for projected gradient descent adversarial attack using PyTorch
import torch
def projected_gradient_descent(model, x, y, loss_fn, num_steps, step_size, step_norm, eps, eps_norm,
clamp=(0,1), y_target=None):
"""Performs the projected gradient descent attack on a batch of images."""
x_adv = x.clone().detach().requires_grad_(True).to(x.device)
targeted = y_target is not None
num_channels = x.shape[1]
for i in range(num_steps):
@tgsmith61591
tgsmith61591 / ranking.py
Last active March 21, 2024 06:36
Ranking metrics for recommender systems
# -*- coding: utf-8 -*-
#
# Author: Taylor G Smith
#
# Recommender system ranking metrics derived from Spark source for use with
# Python-based recommender libraries (i.e., implicit,
# http://github.com/benfred/implicit/). These metrics are derived from the
# original Spark Scala source code for recommender metrics.
# https://github.com/apache/spark/blob/master/mllib/src/main/scala/org/apache/spark/mllib/evaluation/RankingMetrics.scala
@itspig
itspig / hosts
Last active February 28, 2024 05:51
小米盒子、小米电视去广告 hosts file for MiBox & MiTV
127.0.0.1 ad.mi.com
127.0.0.1 ad.xiaomi.com
127.0.0.1 cdn.ad.xiaomi.com
127.0.0.1 log.ad.xiaomi.com
127.0.0.1 api.ad.xiaomi.com
127.0.0.1 sdkconfig.ad.xiaomi.com
127.0.0.1 o2o.api.xiaomi.com
127.0.0.1 gallery.pandora.xiaomi.com
127.0.0.1 mishop.pandora.xiaomi.com
127.0.0.1 upgrade.mishop.pandora.xiaomi.com
@rahulrajaram
rahulrajaram / .md
Last active April 2, 2023 15:47
Python: Write to a file from multiple threads

I recently came across the need to spawn multiple threads, each of which needs to write to the same file. Since the file will experience contention from multiple resources, we need to guarantee thread-safety.

NOTE: The following examples work with Python 3.x. To execute the following programs using Python 2.7, please replace threading.get_ident() with thread.get_ident(). As a result, you would need to import thread and not threading.

  1. (The following example will take a very long time). It will create 200 threads, each of which will wait until a global lock is available for acquisition.
# threading_lock.py
import threading