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
Postar um comentário
Sinta-se a vontade para comentar. Críticas construtivas são sempre bem vindas.