Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/es-419/hub/tutorials/bert_experts.ipynb
25118 views
Kernel: Python 3

Licensed under the Apache License, Version 2.0 (the "License");

#@title Copyright 2020 The TensorFlow Hub Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ==============================================================================

Expertos en BERT de TF-Hub

En este Colab se muestra cómo hacer lo siguiente:

  • Cargar modelos BERT de TensorFlow Hub que hayan sido entrenados con tareas diferentes, incluidas MNLI, SQuAD y PubMed.

  • Usar un modelo de preprocesamiento de coincidencias para tokenizar el texto en bruto y convertirlo en los ID.

  • Generar la salida agrupada y secuencial desde los ide de entrada del token, con el modelo cargado.

  • Buscar la similitud semántica de las salidas agrupadas de diferentes oraciones.

Nota: Este Colab se debería ejecutar con un tiempo de ejecución de GPU.

Preparación e importaciones

!pip install --quiet "tensorflow-text==2.11.*"
import seaborn as sns from sklearn.metrics import pairwise import tensorflow as tf import tensorflow_hub as hub import tensorflow_text as text # Imports TF ops for preprocessing.
#@title Configure the model { run: "auto" } BERT_MODEL = "https://tfhub.dev/google/experts/bert/wiki_books/2" # @param {type: "string"} ["https://tfhub.dev/google/experts/bert/wiki_books/2", "https://tfhub.dev/google/experts/bert/wiki_books/mnli/2", "https://tfhub.dev/google/experts/bert/wiki_books/qnli/2", "https://tfhub.dev/google/experts/bert/wiki_books/qqp/2", "https://tfhub.dev/google/experts/bert/wiki_books/squad2/2", "https://tfhub.dev/google/experts/bert/wiki_books/sst2/2", "https://tfhub.dev/google/experts/bert/pubmed/2", "https://tfhub.dev/google/experts/bert/pubmed/squad2/2"] # Preprocessing must match the model, but all the above use the same. PREPROCESS_MODEL = "https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3"

Oraciones

Para ejecutar el modelo, tomemos algunas oraciones de Wikipedia:

sentences = [ "Here We Go Then, You And I is a 1999 album by Norwegian pop artist Morten Abel. It was Abel's second CD as a solo artist.", "The album went straight to number one on the Norwegian album chart, and sold to double platinum.", "Among the singles released from the album were the songs \"Be My Lover\" and \"Hard To Stay Awake\".", "Riccardo Zegna is an Italian jazz musician.", "Rajko Maksimović is a composer, writer, and music pedagogue.", "One of the most significant Serbian composers of our time, Maksimović has been and remains active in creating works for different ensembles.", "Ceylon spinach is a common name for several plants and may refer to: Basella alba Talinum fruticosum", "A solar eclipse occurs when the Moon passes between Earth and the Sun, thereby totally or partly obscuring the image of the Sun for a viewer on Earth.", "A partial solar eclipse occurs in the polar regions of the Earth when the center of the Moon's shadow misses the Earth.", ]

Ejecución del modelo

Cargamos el modelo BERT de TF-Hub, tokenizamos las oraciones con el modelo de preprocesamiento de coincidencias de TF-Hub, después ingresamos las oraciones tokenizadas al modelo. Para que este Colab siga siendo rápido y simple, recomendamos ejecutarlo en GPU.

Vaya a RuntimeChange runtime type (Tiempo de ejecución → Cambiar tipo de tiempo de ejecución) para confirmar que GPU haya sido seleccionada.

preprocess = hub.load(PREPROCESS_MODEL) bert = hub.load(BERT_MODEL) inputs = preprocess(sentences) outputs = bert(inputs)
print("Sentences:") print(sentences) print("\nBERT inputs:") print(inputs) print("\nPooled embeddings:") print(outputs["pooled_output"]) print("\nPer token embeddings:") print(outputs["sequence_output"])

Similitudes semánticas

Ahora, echemos un vistazo a las incorporaciones de nuestras oraciones en pooled_output y comparemos qué similitudes tienen con las oraciones.

#@title Helper functions def plot_similarity(features, labels): """Plot a similarity matrix of the embeddings.""" cos_sim = pairwise.cosine_similarity(features) sns.set(font_scale=1.2) cbar_kws=dict(use_gridspec=False, location="left") g = sns.heatmap( cos_sim, xticklabels=labels, yticklabels=labels, vmin=0, vmax=1, cmap="Blues", cbar_kws=cbar_kws) g.tick_params(labelright=True, labelleft=False) g.set_yticklabels(labels, rotation=0) g.set_title("Semantic Textual Similarity")
plot_similarity(outputs["pooled_output"], sentences)

Más información