Paddle ocr gpu support. #4
@@ -4,6 +4,7 @@
|
||||
import re
|
||||
import os
|
||||
from bs4 import BeautifulSoup, NavigableString
|
||||
from latex2mathml.converter import convert as latex_to_mathml
|
||||
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
TEMPLATE_INPUT = os.path.join(BASE_DIR, 'instructions/plantilla_individual.htm')
|
||||
@@ -38,6 +39,30 @@ def md_to_html_para(text):
|
||||
text = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', r'<a href="\2">\1</a>', text)
|
||||
return text
|
||||
|
||||
def convert_latex_formulas(text):
|
||||
"""Convert LaTeX formulas to MathML for Word compatibility."""
|
||||
# Block formulas $$...$$
|
||||
def convert_block(match):
|
||||
latex = match.group(1)
|
||||
try:
|
||||
mathml = latex_to_mathml(latex, display="block")
|
||||
return f'<p class=MsoNormal style="text-align:center">{mathml}</p>'
|
||||
except:
|
||||
return match.group(0) # Keep original if conversion fails
|
||||
|
||||
text = re.sub(r'\$\$([^$]+)\$\$', convert_block, text)
|
||||
|
||||
# Inline formulas $...$
|
||||
def convert_inline(match):
|
||||
latex = match.group(1)
|
||||
try:
|
||||
return latex_to_mathml(latex, display="inline")
|
||||
except:
|
||||
return match.group(0)
|
||||
|
||||
text = re.sub(r'\$([^$]+)\$', convert_inline, text)
|
||||
return text
|
||||
|
||||
def extract_table_title(lines, current_index):
|
||||
"""Look for table title in preceding lines (e.g., **Tabla 1.** *Title*)."""
|
||||
# Check previous non-empty lines for table title
|
||||
@@ -251,6 +276,7 @@ def parse_md_to_html_blocks(md_content):
|
||||
if re.match(r'^[\-\*\+]\s', line):
|
||||
while i < len(lines) and re.match(r'^[\-\*\+]\s', lines[i]):
|
||||
item_text = lines[i][2:].strip()
|
||||
item_text = convert_latex_formulas(item_text)
|
||||
html_blocks.append(f'<p class=MsoListParagraphCxSpMiddle style="margin-left:36pt;text-indent:-18pt"><span lang=ES style="font-family:Symbol">·</span><span lang=ES style="font-size:7pt"> </span><span lang=ES>{md_to_html_para(item_text)}</span></p>')
|
||||
i += 1
|
||||
continue
|
||||
@@ -260,6 +286,7 @@ def parse_md_to_html_blocks(md_content):
|
||||
num = 1
|
||||
while i < len(lines) and re.match(r'^\d+\.\s', lines[i]):
|
||||
item_text = re.sub(r'^\d+\.\s*', '', lines[i]).strip()
|
||||
item_text = convert_latex_formulas(item_text)
|
||||
html_blocks.append(f'<p class=MsoListParagraphCxSpMiddle style="margin-left:36pt;text-indent:-18pt"><span lang=ES>{num}.<span style="font-size:7pt"> </span>{md_to_html_para(item_text)}</span></p>')
|
||||
num += 1
|
||||
i += 1
|
||||
@@ -284,7 +311,12 @@ def parse_md_to_html_blocks(md_content):
|
||||
i += 1
|
||||
|
||||
para_text = ' '.join(para_lines)
|
||||
html_blocks.append(f'<p class=MsoNormal><span lang=ES>{md_to_html_para(para_text)}</span></p>')
|
||||
para_text = convert_latex_formulas(para_text)
|
||||
# Check if paragraph contains MathML (already wrapped)
|
||||
if '<math' in para_text:
|
||||
html_blocks.append(para_text)
|
||||
else:
|
||||
html_blocks.append(f'<p class=MsoNormal><span lang=ES>{md_to_html_para(para_text)}</span></p>')
|
||||
|
||||
return '\n\n'.join(html_blocks)
|
||||
|
||||
|
||||
@@ -4678,11 +4678,11 @@ _Toc14106979"><span style="mso-bookmark:_Toc437515557"><span lang="ES" style="te
|
||||
<p class="MsoNormal"><span lang="ES">La evaluación rigurosa de sistemas OCR requiere métricas estandarizadas que permitan comparaciones objetivas. Las métricas fundamentales se basan en la distancia de edición de Levenshtein.</span></p>
|
||||
<h4><span lang="ES">Distancia de Levenshtein</span></h4>
|
||||
<p class="MsoNormal"><span lang="ES">La distancia de Levenshtein (Levenshtein, 1966) entre dos cadenas es el número mínimo de operaciones de edición (inserción, eliminación, sustitución) necesarias para transformar una cadena en otra. Formalmente, para dos cadenas <i>a</i> y <i>b</i>:</span></p>
|
||||
<p class="MsoNormal"><span lang="ES">$$d(a,b) = \min(\text{inserciones} + \text{eliminaciones} + \text{sustituciones})$$</span></p>
|
||||
<p class="MsoNormal" style="text-align:center"><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>d</mi><mo stretchy="false">(</mo><mi>a</mi><mo>,</mo><mi>b</mi><mo stretchy="false">)</mo><mo>=</mo><mo>min</mo><mo stretchy="false">(</mo><mtext>inserciones</mtext><mo>+</mo><mtext>eliminaciones</mtext><mo>+</mo><mtext>sustituciones</mtext><mo stretchy="false">)</mo></mrow></math></p>
|
||||
<p class="MsoNormal"><span lang="ES">Esta métrica es fundamental para calcular tanto CER como WER.</span></p>
|
||||
<h4><span lang="ES">Character Error Rate (CER)</span></h4>
|
||||
<p class="MsoNormal"><span lang="ES">El CER mide el error a nivel de carácter y se calcula como:</span></p>
|
||||
<p class="MsoNormal"><span lang="ES">$$CER = \frac{S + D + I}{N}$$</span></p>
|
||||
<p class="MsoNormal" style="text-align:center"><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>C</mi><mi>E</mi><mi>R</mi><mo>=</mo><mfrac><mrow><mi>S</mi><mo>+</mo><mi>D</mi><mo>+</mo><mi>I</mi></mrow><mrow><mi>N</mi></mrow></mfrac></mrow></math></p>
|
||||
<p class="MsoNormal"><span lang="ES">Donde:</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES">S = número de sustituciones de caracteres</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES">D = número de eliminaciones de caracteres</span></p>
|
||||
@@ -4695,7 +4695,7 @@ _Toc14106979"><span style="mso-bookmark:_Toc437515557"><span lang="ES" style="te
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES"><b>Búsqueda y archivo</b>: CER < 5% puede ser suficiente</span></p>
|
||||
<h4><span lang="ES">Word Error Rate (WER)</span></h4>
|
||||
<p class="MsoNormal"><span lang="ES">El WER mide el error a nivel de palabra, utilizando la misma fórmula pero considerando palabras como unidades:</span></p>
|
||||
<p class="MsoNormal"><span lang="ES">$$WER = \frac{S_w + D_w + I_w}{N_w}$$</span></p>
|
||||
<p class="MsoNormal" style="text-align:center"><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>W</mi><mi>E</mi><mi>R</mi><mo>=</mo><mfrac><mrow><msub><mi>S</mi><mi>w</mi></msub><mo>+</mo><msub><mi>D</mi><mi>w</mi></msub><mo>+</mo><msub><mi>I</mi><mi>w</mi></msub></mrow><mrow><msub><mi>N</mi><mi>w</mi></msub></mrow></mfrac></mrow></math></p>
|
||||
<p class="MsoNormal"><span lang="ES">El WER es generalmente mayor que el CER, ya que un solo error de carácter puede invalidar una palabra completa. La relación típica es WER ≈ 2-3 × CER para texto en español.</span></p>
|
||||
<h4><span lang="ES">Otras Métricas Complementarias</span></h4>
|
||||
<p class="MsoNormal"><span lang="ES"><b>Precision y Recall a nivel de palabra</b>: Útiles cuando se evalúa la capacidad del sistema para detectar palabras específicas.</span></p>
|
||||
@@ -4808,16 +4808,18 @@ _Toc14106979"><span style="mso-bookmark:_Toc437515557"><span lang="ES" style="te
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES">Parámetros de regularización (dropout, weight decay)</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES"><b>Umbrales de decisión en tiempo de inferencia</b> (relevante para este trabajo)</span></p>
|
||||
<p class="MsoNormal"><span lang="ES">El problema de HPO puede formalizarse como:</span></p>
|
||||
<p class="MsoNormal"><span lang="ES">$$\lambda^* = \arg\min_{\lambda \in \Lambda} \mathcal{L}(M_\lambda, D_{val})$$</span></p>
|
||||
<p class="MsoNormal" style="text-align:center"><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><msup><mi>λ</mi><mo>*</mo></msup><mo>=</mo><mi>\arg</mi><msub><mo>min</mo><mrow><mi>λ</mi><mo>∈</mo><mi>Λ</mi></mrow></msub><mi>ℒ</mi><mo stretchy="false">(</mo><msub><mi>M</mi><mi>λ</mi></msub><mo>,</mo><msub><mi>D</mi><mrow><mi>v</mi><mi>a</mi><mi>l</mi></mrow></msub><mo stretchy="false">)</mo></mrow></math></p>
|
||||
<p class="MsoNormal"><span lang="ES">Donde:</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES">$\lambda$ es un vector de hiperparámetros</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES">$\Lambda$ es el espacio de búsqueda</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES">$M_\lambda$ es el modelo configurado con $\lambda$</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES">$\mathcal{L}$ es la función de pérdida</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES">$D_{val}$ es el conjunto de validación</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES"><math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>λ</mi></mrow></math> es un vector de hiperparámetros</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES"><math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>Λ</mi></mrow></math> es el espacio de búsqueda</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES"><math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><msub><mi>M</mi><mi>λ</mi></msub></mrow></math> es el modelo configurado con <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>λ</mi></mrow></math></span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES"><math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>ℒ</mi></mrow></math> es la función de pérdida</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES"><math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><msub><mi>D</mi><mrow><mi>v</mi><mi>a</mi><mi>l</mi></mrow></msub></mrow></math> es el conjunto de validación</span></p>
|
||||
<h4><span lang="ES">Métodos de Optimización</span></h4>
|
||||
<p class="MsoNormal"><span lang="ES"><b>Grid Search (Búsqueda en rejilla)</b>:</span></p>
|
||||
<p class="MsoNormal"><span lang="ES">El método más simple consiste en evaluar todas las combinaciones posibles de valores discretizados de los hiperparámetros. Para $k$ hiperparámetros con $n$ valores cada uno, requiere $n^k$ evaluaciones.</span></p>
|
||||
|
||||
El método más simple consiste en evaluar todas las combinaciones posibles de valores discretizados de los hiperparámetros. Para <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>k</mi></mrow></math> hiperparámetros con <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>n</mi></mrow></math> valores cada uno, requiere <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><msup><mi>n</mi><mi>k</mi></msup></mrow></math> evaluaciones.
|
||||
|
||||
<p class="MsoNormal"><span lang="ES">Ventajas:</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES">Exhaustivo y reproducible</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES">Fácil de paralelizar</span></p>
|
||||
@@ -4841,12 +4843,18 @@ _Toc14106979"><span style="mso-bookmark:_Toc437515557"><span lang="ES" style="te
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES"><b>Random Forests</b>: Manejan bien espacios de alta dimensión y variables categóricas</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES"><b>Tree-structured Parzen Estimator (TPE)</b>: Modela densidades en lugar de la función objetivo</span></p>
|
||||
<h4><span lang="ES">Tree-structured Parzen Estimator (TPE)</span></h4>
|
||||
<p class="MsoNormal"><span lang="ES">TPE, propuesto por Bergstra et al. (2011) e implementado en Optuna, es particularmente efectivo para HPO. En lugar de modelar $p(y|\lambda)$ directamente, TPE modela:</span></p>
|
||||
<p class="MsoNormal"><span lang="ES">$$p(\lambda|y) = \begin{cases} l(\lambda) & \text{si } y < y^<i> \\ g(\lambda) & \text{si } y \geq y^</i> \end{cases}$$</span></p>
|
||||
<p class="MsoNormal"><span lang="ES">Donde $y^*$ es un umbral (típicamente el percentil 15-25 de las observaciones), $l(\lambda)$ es la densidad de hiperparámetros con buen rendimiento, y $g(\lambda)$ es la densidad de hiperparámetros con mal rendimiento.</span></p>
|
||||
|
||||
TPE, propuesto por Bergstra et al. (2011) e implementado en Optuna, es particularmente efectivo para HPO. En lugar de modelar <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>p</mi><mo stretchy="false">(</mo><mi>y</mi><mo stretchy="false">|</mo><mi>λ</mi><mo stretchy="false">)</mo></mrow></math> directamente, TPE modela:
|
||||
|
||||
<p class="MsoNormal" style="text-align:center"><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>p</mi><mo stretchy="false">(</mo><mi>λ</mi><mo stretchy="false">|</mo><mi>y</mi><mo stretchy="false">)</mo><mo>=</mo><mrow><mo fence="true" form="prefix" stretchy="true">{</mo><mtable><mtr><mtd columnalign="left"><mi>l</mi><mo stretchy="false">(</mo><mi>λ</mi><mo stretchy="false">)</mo></mtd><mtd columnalign="left"><mtext>si </mtext><mi>y</mi><mo><</mo><msup><mi>y</mi><mo>*</mo></msup></mtd></mtr><mtr><mtd columnalign="left"><mi>g</mi><mo stretchy="false">(</mo><mi>λ</mi><mo stretchy="false">)</mo></mtd><mtd columnalign="left"><mtext>si </mtext><mi>y</mi><mo>≥</mo><msup><mi>y</mi><mo>*</mo></msup></mtd></mtr></mtable></mrow></mrow></math></p>
|
||||
|
||||
Donde <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><msup><mi>y</mi><mo>*</mo></msup></mrow></math> es un umbral (típicamente el percentil 15-25 de las observaciones), <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>l</mi><mo stretchy="false">(</mo><mi>λ</mi><mo stretchy="false">)</mo></mrow></math> es la densidad de hiperparámetros con buen rendimiento, y <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>g</mi><mo stretchy="false">(</mo><mi>λ</mi><mo stretchy="false">)</mo></mrow></math> es la densidad de hiperparámetros con mal rendimiento.
|
||||
|
||||
<p class="MsoNormal"><span lang="ES">La función de adquisición Expected Improvement se aproxima como:</span></p>
|
||||
<p class="MsoNormal"><span lang="ES">$$EI(\lambda) \propto \frac{l(\lambda)}{g(\lambda)}$$</span></p>
|
||||
<p class="MsoNormal"><span lang="ES">Configuraciones con alta probabilidad bajo $l$ y baja probabilidad bajo $g$ tienen mayor Expected Improvement.</span></p>
|
||||
<p class="MsoNormal" style="text-align:center"><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>E</mi><mi>I</mi><mo stretchy="false">(</mo><mi>λ</mi><mo stretchy="false">)</mo><mo>∝</mo><mfrac><mrow><mi>l</mi><mo stretchy="false">(</mo><mi>λ</mi><mo stretchy="false">)</mo></mrow><mrow><mi>g</mi><mo stretchy="false">(</mo><mi>λ</mi><mo stretchy="false">)</mo></mrow></mfrac></mrow></math></p>
|
||||
|
||||
Configuraciones con alta probabilidad bajo <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>l</mi></mrow></math> y baja probabilidad bajo <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>g</mi></mrow></math> tienen mayor Expected Improvement.
|
||||
|
||||
<p class="MsoNormal"><span lang="ES"><b>Ventajas de TPE</b>:</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES">Maneja naturalmente espacios condicionales (hiperparámetros que dependen de otros)</span></p>
|
||||
<p class="MsoListParagraphCxSpMiddle" style="margin-left:36pt;text-indent:-18pt"><span lang="ES" style="font-family:Symbol">·</span><span lang="ES" style="font-size:7pt"> </span><span lang="ES">Eficiente para espacios de alta dimensión</span></p>
|
||||
|
||||
Reference in New Issue
Block a user