Pular para o conteúdo principal

Tensor Flow - Similarity

 Fonte: https://www-marktechpost-com.cdn.ampproject.org/c/s/www.marktechpost.com/2021/09/13/tensorflow-introduces-tensorflow-similarity-an-easy-and-fast-python-package-to-train-similarity-models-using-tensorflow/?amp

TensorFlow Introduces the first version of ‘TensorFlow Similarity’. 

TensorFlow Similarity is an easy and fast Python package to train similarity models using TensorFlow. 

Deep learning models are powerful for recommendation systems because they are trained using contrastive learning. The Contrastive Learning is a technique that teaches the model to learn an embedding space in which similar examples are pulled together while distinct ones live far apart.

The idea of a contrastive loss is to find the distance between two points in an embedding space. When applied across all examples, this trains models how similar or dissimilar they are by controlling for other attributes that might affect those distances–so at its core it just measures similarities between objects!  

Once the model is trained, an index is created with embeddings of the various items and searchable. TensorFlow Similarity uses Fast Approximate Nearest Neighbor Search (ANN) to instantiate the closest matching items from the index in sub-linear time. 

One of the great things about similarity models is that you can add unlimited new classes to your index without retraining. Instead, all it takes are some embeddings for representative items from these newly-added groups, and they will be automatically stored in place so as not to interrupt any current training process.

TensorFlow Similarity introduces the SimilarityModel(), a new Keras model that natively supports embedding indexing and querying. This allows users to perform end-to-end training and evaluation quickly and efficiently. Within 20 lines of code, it trains, indexes and searches on MNIST data.

Fonte: https://blog.tensorflow.org/2021/09/introducing-tensorflow-similarity.html

Other approaches, such as using model feature extraction, require the use of an exact nearest neighbor search to find related items and may not be as accurate as a trained similarity model. This prevents those methods scaling as performing an exact search requires a quadratic time in the size of the search index. In contrast, TensorFlow Similarity’s built-in Approximate Nearest Neighbor indexing system, which relies on the NMSLIB, makes it possible to search over millions of indexed items, retrieving the top-K similar matches within a fraction of second. 

Código

from tensorflow.keras import layers

# Embedding output layer with L2 norm
from tensorflow_similarity.layers import MetricEmbedding 
# Specialized metric loss
from tensorflow_similarity.losses import MultiSimilarityLoss 
# Sub classed keras Model with support for indexing
from tensorflow_similarity.models import SimilarityModel
# Data sampler that pulls datasets directly from tf dataset catalog
from tensorflow_similarity.samplers import TFDatasetMultiShotMemorySampler
# Nearest neighbor visualizer
from tensorflow_similarity.visualization import viz_neigbors_imgs


# Data sampler that generates balanced batches from MNIST dataset
sampler = TFDatasetMultiShotMemorySampler(dataset_name='mnist', classes_per_batch=10)

# Build a Similarity model using standard Keras layers
inputs = layers.Input(shape=(28, 28, 1))
x = layers.Rescaling(1/255)(inputs)
x = layers.Conv2D(64, 3, activation='relu')(x)
x = layers.Flatten()(x)
x = layers.Dense(64, activation='relu')(x)
outputs = MetricEmbedding(64)(x)

# Build a specialized Similarity model
model = SimilarityModel(inputs, outputs)

# Train Similarity model using contrastive loss
model.compile('adam', loss=MultiSimilarityLoss())
model.fit(sampler, epochs=5)

# Index 100 embedded MNIST examples to make them searchable
sx, sy = sampler.get_slice(0,100)
model.index(x=sx, y=sy, data=sx)

# Find the top 5 most similar indexed MNIST examples for a given example
qx, qy = sampler.get_slice(3713, 1)
nns = model.single_lookup(qx[0])

# Visualize the query example and its top 5 neighbors
viz_neigbors_imgs(qx[0], qy[0], nns)

 Example -> https://github.com/tensorflow/similarity/blob/master/examples/supervised_hello_world.ipynb


Comentários

Postagens mais visitadas deste blog

Connected Papers: Uma abordagem alternativa para revisão da literatura

Durante um projeto de pesquisa podemos encontrar um artigo que nos identificamos em termos de problema de pesquisa e também de solução. Então surge a vontade de saber como essa área de pesquisa se desenvolveu até chegar a esse ponto ou quais desdobramentos ocorreram a partir dessa solução proposta para identificar o estado da arte nesse tema. Podemos seguir duas abordagens:  realizar uma revisão sistemática usando palavras chaves que melhor caracterizam o tema em bibliotecas digitais de referência para encontrar artigos relacionados ou realizar snowballing ancorado nesse artigo que identificamos previamente, explorando os artigos citados (backward) ou os artigos que o citam (forward)  Mas a ferramenta Connected Papers propõe uma abordagem alternativa para essa busca. O problema inicial é dado um artigo de interesse, precisamos encontrar outros artigos relacionados de "certa forma". Find different methods and approaches to the same subject Track down the state of the art rese...

Knowledge Graph Embedding with Triple Context - Leitura de Abstract

  Jun Shi, Huan Gao, Guilin Qi, and Zhangquan Zhou. 2017. Knowledge Graph Embedding with Triple Context. In Proceedings of the 2017 ACM on Conference on Information and Knowledge Management (CIKM '17). Association for Computing Machinery, New York, NY, USA, 2299–2302. https://doi.org/10.1145/3132847.3133119 ABSTRACT Knowledge graph embedding, which aims to represent entities and relations in vector spaces, has shown outstanding performance on a few knowledge graph completion tasks. Most existing methods are based on the assumption that a knowledge graph is a set of separate triples, ignoring rich graph features, i.e., structural information in the graph. In this paper, we take advantages of structures in knowledge graphs, especially local structures around a triple, which we refer to as triple context. We then propose a Triple-Context-based knowledge Embedding model (TCE). For each triple, two kinds of structure information are considered as its context in the graph; one is the out...

Exploratory Search: From Finding to Understanding - Leitura de Artigo

Gary Marchionini. 2006. Exploratory search: from finding to understanding. Commun. ACM  49, 4 (April 2006), 41–46. https://doi.org/10.1145/1121949.1121979   This article distinguishes exploratory search that blends quer ying and browsing strategies from retrieval that is best served by analytical strategies ...   Exploratory search. Search is a fundamental life activity.   A hierarchy of information needs may also be defined that ranges from basic facts that guide short-term actions (for example, the predicted chance for rain today to decide whether to bring an umbr ella) to networks of related concepts that help us under stand phenomena or execute complex activities (for example, the relationships between bond prices and stock prices to manage a retirement portfolio) to com plex networks of tacit and explicit knowledge that accretes as expertise over a lifetime (for example, the most promising paths of investigation for the sea soned scholar or designer)....