Skip to content

Instantly share code, notes, and snippets.

View gaphex's full-sized avatar

Denis gaphex

  • Moscow
View GitHub Profile
@gaphex
gaphex / euclidean_distance.py
Created June 13, 2019 15:13
euclidean distance computation
squared_distance = tf.reduce_sum(tf.pow(Q - S, 2), reduction_indices=1)
distance = tf.sqrt(squared_distance)
@gaphex
gaphex / get_top_k.py
Last active June 20, 2019 13:46
top_k distances and indices retrieval
top_k = 3
top_neg_dists, top_indices = tf.math.top_k(tf.negative(distance), k=top_k)
top_dists = tf.negative(top_neg_dists)
@gaphex
gaphex / tf_retriever.py
Last active June 20, 2019 12:41
Nearest Neighbour retriever
class TFRanker:
def __init__(self, dim, metric, top_k=3):
self.dim = dim
self.top_k = top_k
self.metric = metric
self.graph = tf.Graph()
self.session = tf.Session(graph=self.graph)
self.build_graph()

>$$e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i$$

Q = tf.placeholder("float", [dim])
S = tf.placeholder("float", [None, dim])
Qr = tf.reshape(Q, (1, -1))
PP = tf.keras.backend.batch_dot(S, S, axes=1)
QQ = tf.matmul(Qr, tf.transpose(Qr))
PQ = tf.matmul(S, tf.transpose(Qr))
distance = PP - 2 * PQ + QQ
We can't make this file beautiful and searchable because it's too large.
-0.02492 -0.01458 0.1818 0.03638 0.4434 -0.299 -0.3193 0.04535 0.03632 0.3071 -0.1837 -0.2976 -0.006374 0.712 -0.06793 0.6587 0.3665 -0.02434 0.07965 0.2109 -0.02144 -0.3918 0.4436 0.256 0.1763 0.01851 0.2637 0.02402 -0.604 -0.007587 0.4468 0.564 -0.2263 -0.2507 0.1254 -0.11273 0.005077 -0.2065 -0.5293 -0.1367 -0.6157 -0.2585 -0.2081 -0.412 0.1055 -0.1884 0.4023 0.0169 -0.1935 -0.02063 -0.4822 -0.05426 -0.6025 -0.613 0.2583 0.738 -0.2817 -0.2483 0.1346 -0.09863 0.09424 -0.1578 0.3335 -0.1814 0.312 0.01714 -0.1401 0.09564 -0.1921 0.1261 -0.11487 -0.1396 -0.1423 0.301 0.08057 -0.1716 -0.2308 0.0813 -0.1829 0.3032 -0.209 -0.2654 0.10913 -0.4744 0.1678 0.2065 -0.04 0.2131 -0.0995 0.7812 0.04114 0.4329 -0.2094 0.1263 0.01222 -0.522 -0.4272 -0.291 0.05374 0.2788 0.05502 0.03622 0.3337 0.635 -0.1764 -0.397 -0.2678 -0.07465 -0.00988 0.1323 -0.2157 -0.0542 0.206 -0.213 -0.4614 -0.1632 0.252 0.288 0.246 0.2472 0.103 0.01074 0.2651 0.2769 0.1927 -0.1417 0.0927 -0.1571 0.003601 0.638 0.0479 -0.3318 -0.1776 -0.011246 -0.2
Label Sentence
wheat SRI LANKA GETS USDA APPROVAL FOR WHEAT PRICE Food Department officials said the U . S . Department of Agriculture approved the Continental Grain Co sale of 52 , 500 tonnes of soft wheat at 89 U . S . Dlrs a tonne C and F from Pacific Northwest to Colombo .
wheat They said the shipment was for April 8 to 20 delivery .
wheat FURTHER ARGENTINE COARSE GRAIN LOSSES FEARED Argentine grain producers adjusted their yield estimates for the 1986 / 87 coarse grain crop downward in the week to yesterday after the heavy rains at the end of March and beginning of April , trade sources said .
wheat They said sunflower , maize and sorghum production estimates had been reduced despite some later warm , dry weather , which has allowed a return to harvesting in some areas .
wheat However , as showers fell intermittently after last weekend , producers feared another spell of prolonged and intense rain could cause more damage to crops already badly hit this season .
wheat Rains in the middle of last week reac
{
"embeddings": [
{
"tensorName": "My tensor",
"tensorShape": [
1134,
768
],
"tensorPath": "https://gist.githubusercontent.com/gaphex/7f3daf793e56d6563bbf1df4ffddb88f/raw/2dba89b44e80c31aa82ee800d07d0c5d972b3275/reuters_bert_embeddings.tsv",
"metadataPath": "https://gist.githubusercontent.com/gaphex/b3d2838e56349d5b53a3011000e2f35e/raw/5d3ce116fb08fb6fdc7fc286053f8e01a44da0fe/reuters_bert_metadata.tsv",
[{"label":"State 0","isSelected":true,"tSNEIteration":1553,"tSNEPerplexity":9,"tSNELearningRate":1,"tSNEis3d":true,"umapIs3d":true,"umapNeighbors":15,"pcaComponentDimensions":[0,1,2],"projections":[{"pca-0":0.5753942131996155,"pca-1":-7.144152641296387,"pca-2":2.135350465774536,"pca-3":0.30271515250205994,"pca-4":-7.306628227233887,"pca-5":2.3445184230804443,"pca-6":5.1897382736206055,"pca-7":-1.710606575012207,"pca-8":0.14948318898677826,"pca-9":-1.2810907363891602,"tsne-0":-19.17779998293605,"tsne-1":-8.737170856322848,"tsne-2":3.4405034737394518},{"pca-0":-3.3178958892822266,"pca-1":-1.894553303718567,"pca-2":-3.520504951477051,"pca-3":-1.990181565284729,"pca-4":0.030808638781309128,"pca-5":-2.9290878772735596,"pca-6":1.1588380336761475,"pca-7":-1.9897701740264893,"pca-8":-1.3593165874481201,"pca-9":-2.974226236343384,"tsne-0":-2.792864945677704,"tsne-1":46.16809832776983,"tsne-2":-34.11351794923433},{"pca-0":-1.2383508682250977,"pca-1":3.416457176208496,"pca-2":-2.6669344902038574,"pca-3":0.48395350575447
{
"embeddings": [
{
"tensorName": "Reuters",
"tensorShape": [
1134,
768
],
"tensorPath": "https://gist.githubusercontent.com/gaphex/7f3daf793e56d6563bbf1df4ffddb88f/raw/2dba89b44e80c31aa82ee800d07d0c5d972b3275/reuters_bert_embeddings.tsv",
"metadataPath": "https://gist.githubusercontent.com/gaphex/b3d2838e56349d5b53a3011000e2f35e/raw/c597282ef1d5f238eb22a0371d321c88a3c7e0ce/reuters_bert_metadata.tsv",