una manera más simple de corrroborar la suma de las probabilidades
sum(initTagStateProb.values())
Desambiguación y etiquetado de palabras
Introducción a la desambiguación
Etiquetado rápido en Python: español e inglés
Etiquetado rápido en Python: Stanza (Stanford NLP)
Modelos Markovianos Latentes (HMM)
Cadenas de Markov
Modelos Markovianos latentes (HMM)
Entrenando un HMM
Fases de entrenamiento de un HMM
Entrenando un HMM en Python
Algoritmo de Viterbi
El algoritmo de Viterbi
Cálculo de las probabilidades de Viterbi
Carga del modelo HMM y distribución inicial
Implementación de algoritmo de Viterbi en Python
Entrenamiento directo de HMM con NLTK
Modelos Markovianos de máxima entropía (MEMM)
Modelos Markovianos de máxima entropia (MEMM)
Algoritmo de Viterbi para MEMM
Reto: construye un MEMM en Python
Clasificación de texto con NLTK
El problema general de la clasificación de texto
Tareas de clasificación con NLTK
Modelos de clasificación en Python: nombres
Modelos de clasificación en Python: documentos
Implementación de un modelo de clasificación de texto
Naive Bayes
Naive Bayes en Python: preparación de los datos
Naive Bayes en Python: construcción del modelo
Naive Bayes en Python: ejecución del modelo
Métricas para algoritmos de clasificación
Reto final: construye un modelo de sentimientos
You don't have access to this class
Keep learning! Join and start boosting your career
The Viterbi algorithm is fundamental in natural language processing (NLP), specifically in solving problems with latent Markov models. In this content, we will show you step-by-step how to implement this algorithm in Python, taking advantage of its flexibility and power to subdivide complex problems into manageable parts. Let's dive into this exciting dynamic programming project!
To begin with, it is crucial to prepare the development environment and make sure we have the necessary data to implement the algorithm. Here we detail the process:
Import necessary libraries: start by importing the essential libraries such as Conlu
and NumPy
, which are vital for data and structure handling.
import numpy as np
Download the corpus and the trained model: Use GitHub to get the Ancora Corpus data in Spanish and to load our previously trained transition and emission models. Make sure you have the .npy
files downloaded and accessible in your working environment.
Configure the kernel and directories: Connect to a Python kernel and refresh the file system to confirm that the data has been downloaded correctly. This is an essential step to ensure that everything is ready for processing.
To work with the trained model, it is essential to load the probability matrices from the saved files. Let's see how it is done:
Loading data from NumPy files: use np.load
to load the transition and emission probability matrices, making sure that the data are read correctly.
transition_probabilities = np.load("transition.npy", allow_pickle=True).item()emission_probabilities = np.load("emissionHMM.npy", allow_pickle=True).item()
Validation of the loaded data: Always check that the keys and values of the matrices correspond to what is expected, i.e. check the probabilities to ensure a correct configuration.
Before building the Viterbi algorithm, it is essential to prepare the corpus data translated into manageable grammatical categories. Here are the key steps:
Grammatical category identification: extract a unique list of grammatical categories from the corpus that we will use for labeling. Use Python sets to filter out duplicate items and consolidate the list.
state_set = list({w.split('|')[1] for w in emission_probabilities.keys()})
Assigning indexes to categories: Associates each grammatical category with a unique index to be used in the Viterbi algorithm matrices. Now, each category has a numeric value associated with it for ease of processing.
tag_state_dict = {state: i for i, state in enumerate(state_set)}
A critical component in Viterbi's algorithm is the initial distribution of states. We need to compute the probabilities of the grammatical categories that appear at the beginning of sentences in the corpus.
Calculating initial probabilities: Using a dictionary, count the occasions when each grammatical category is first in a sentence and normalize these counts to convert them to probabilities.
init_tag_state_prob = {}for token_list in parse_ink(data_file): first_tag = token_list[0].upos if first_tag in init_tag_state_prob: init_tag_state_prob[first_tag] += 1 else: init_tag_state_prob[first_tag] = 1
Normalization: Divide each count by the total to get the probabilities.
corpus_length = len(corpus)for tag in init_tag_state_prob: init_tag_state_prob[tag] /= corpus_length
With the environment setup and data prepared, we are ready to implement the Viterbi algorithm. This will allow us to find the most probable sequence of grammatical categories for any given sentence, using dynamic programming to optimize the process.
This method is crucial for predicting tags in text sequences and ensuring accurate and efficient tagging. Stay tuned and keep learning more about this fascinating world of machine learning and natural language processing - stay tuned and never stop learning!
Contributions 7
Questions 2
una manera más simple de corrroborar la suma de las probabilidades
sum(initTagStateProb.values())
Hola Francisco, en primer lugar felicitarte por esta clase y todo el curso en general (lo que llevo hasta el momento), de verdad valoro muchísimo todo el trabajo que has hecho. Por otro lado, quería dar mi aporte, quizás para la identificación de las categorías gramaticales únicas se podría hacer utilizando el dict de la clase pasada que decía tagCountDict, haciendo un set de las llaves de ese diccionario. Solo como una forma de ahorrar código. Saludos!!!
Este curso me esta ayudando bastante, ya que estoy haciendo un proyecto en la maestria relacionado con la detección de odio, me hacian falta estas bases.
Modelo Markoviano Latente > Calculo de las probabilidades de transición y emisión.
Algoritmo de Viterbi > Obtener el camino o ruta, utilizando el que tiene mayor probabilidad. Por ejemplo: “Castillo: Prop, El: Det, Noble: Adj, Trabajador: Adj”, sería la serie o ruta de etiquetas más probables para “Castillo el noble trabajador”.
Que ejercicio tan bueno
para comprobar que las probabilidades suman uno era mas fácil copiar en excel y poner =SUMA()
:'v
Want to see more contributions, questions and answers from the community?