Skip to content

Instantly share code, notes, and snippets.

View vseledkin's full-sized avatar

Viacheslav Seledkin vseledkin

  • Milky Way, Orion Arm, Sol System, 3
View GitHub Profile
@vseledkin
vseledkin / unit_normalize_a_batch_of_vectors.py
Last active October 2, 2021 08:41
unit normalize a batch of vectors
import numpy as np
# vectors is an array of vectors of any length
vectors = np.array(vectors, dtype=np.float32)
print('normalize %s vectors' % len(vectors))
vectors = vectors / np.linalg.norm(vectors, ord=2, axis=1, keepdims=True)
@vseledkin
vseledkin / pickle_load.py
Last active October 2, 2021 10:04
pickle save load
import pickle as pk
with open('filename.pk', 'rb') as filehandler:
obj = pk.load(filehandler)
@vseledkin
vseledkin / knn_cosine.py
Created November 20, 2018 13:08
knn search cosine
def knn_cosine_search(x, D, K):
""" find K nearest neighbours of data among D """
ndata = len(D)
K = K if K < ndata else ndata
# euclidean distances from the other points
x = x.reshape(1, -1)
cos = cdist(D, x, 'cosine').reshape(-1)
idx = np.argsort(cos) # sorting
# return the indexes of K nearest neighbours
@vseledkin
vseledkin / knn.py
Last active November 20, 2018 13:07
knn search euclidean
def knn_euclidean_search(x, D, K):
""" find K nearest neighbours of data among D """
ndata = len(D)
K = K if K < ndata else ndata
# euclidean distances from the other points
sqd = np.sqrt(((D - x)**2).sum(axis=1))
idx = np.argsort(sqd) # sorting
# return the indexes of K nearest neighbours
idx = idx[:K]
d = sqd[idx]
@vseledkin
vseledkin / sh
Last active March 2, 2019 18:03
preconfigured tensorflow building script
x#! /bin/bash
# use XCode 8.2.1
export TF_TYPE=”cpu”
export TF_NEED_GCP=0
export TF_NEED_HDFS=0
export TF_NEED_AWS=0
export TF_NEED_KAFKA=0
export TF_ENABLE_XLA=1
export TF_NEED_GDR=0
export TF_NEED_VERBS=0

Build tensorflow on OSX with NVIDIA CUDA support (GPU acceleration)

These instructions are based on Mistobaan's gist but expanded and updated to work with the latest tensorflow OSX CUDA PR.

Requirements

OS X 10.10 (Yosemite) or newer

@vseledkin
vseledkin / tensorflow_1_6_rc1_high_sierra_gpu.md
Created April 7, 2018 16:19 — forked from orpcam/tensorflow_1_6_rc1_high_sierra_gpu.md
Tensorflow 1.6-rc1 on macOS High Sierra 10.13.3 with GPU Acceleration
@vseledkin
vseledkin / keybase.md
Created January 10, 2018 17:28
keybase git identity

Keybase proof

I hereby claim:

  • I am vseledkin on github.
  • I am apfelson (https://keybase.io/apfelson) on keybase.
  • I have a public key ASDIO3bGsJnLc8uayvwOjez7HcHNdbVDjDRGKhmGdbTKrgo

To claim this, I am signing this object:

@vseledkin
vseledkin / highlight.go
Created September 13, 2017 10:18
wrap some spans within text with specified strings (for search result highlighting)
func substitute(text, before, after string, positions [][]int) string {
// flatten
flatten_positions := make([]int, 2*len(positions))
for i, pos := range positions {
flatten_positions[2*i] = pos[0]
flatten_positions[2*i+1] = pos[1]
}
sort.Ints(flatten_positions)
var count int
@vseledkin
vseledkin / ngrams.go
Created September 13, 2017 10:15
split string to n-grams separating by space
func SplitToNGrams(s string, n []int) []string {
words := strings.Fields(s)
var tokens []string
for cursor, _ := range words {
for _, ni := range n {
// get suffix of length ni in words back from ni position in words array
if cursor+ni <= len(words) {
tokens = append(tokens, strings.Join(words[cursor:cursor+ni], "•"))
}
}