TTS: Text-to-Speech for All

TTS: Text-to-Speech for all.

TTS is a library for advanced Text-to-Speech generation. It's built on the latest research, was designed to achieve the best trade-off among ease-of-training, speed and quality. TTS comes with pretrained models, tools for measuring dataset quality and already used in 20+ languages for products and research projects.

📢 English Voice Samples and SoundCloud playlist

👨‍🍳 TTS training recipes

📄 Text-to-Speech paper collection

💬 Where to ask questions

Please use our dedicated channels for questions and discussion. Help is much more valuable if it's shared publicly, so that more people can benefit from it.

TypePlatforms
🚨 Bug Reports[GitHub Issue Tracker]
FAQTTS/Wiki
🎁 Feature Requests & Ideas[GitHub Issue Tracker]
👩‍💻 Usage Questions[Discourse Forum]
🗯 General Discussion[Discourse Forum] and [Matrix Channel]

🔗 Links and Resources

TypeLinks
💾 InstallationTTS/README.md
👩🏾‍🏫 Tutorials and ExamplesTTS/Wiki
🚀 Released ModelsTTS/Wiki
💻 Docker ImageRepository by @synesthesiam
🖥️ Demo ServerTTS/server
🤖 Running TTS on TerminalTTS/README.md
How to contributeTTS/README.md

🥇 TTS Performance

"Mozilla*" and "Judy*" are our models. Details...

Features

  • High performance Deep Learning models for Text2Speech tasks.
    • Text2Spec models (Tacotron, Tacotron2, Glow-TTS, SpeedySpeech).
    • Speaker Encoder to compute speaker embeddings efficiently.
    • Vocoder models (MelGAN, Multiband-MelGAN, GAN-TTS, ParallelWaveGAN, WaveGrad, WaveRNN)
  • Fast and efficient model training.
  • Detailed training logs on console and Tensorboard.
  • Support for multi-speaker TTS.
  • Efficient Multi-GPUs training.
  • Ability to convert PyTorch models to Tensorflow 2.0 and TFLite for inference.
  • Released models in PyTorch, Tensorflow and TFLite.
  • Tools to curate Text2Speech datasets underdataset_analysis.
  • Demo server for model testing.
  • Notebooks for extensive model benchmarking.
  • Modular (but not too much) code base enabling easy testing for new ideas.

Implemented Models

Text-to-Spectrogram

Attention Methods

  • Guided Attention: paper
  • Forward Backward Decoding: paper
  • Graves Attention: paper
  • Double Decoder Consistency: blog

Speaker Encoder

Vocoders

You can also help us implement more models. Some TTS related work can be found here.

Install TTS

TTS supports python >= 3.6, <3.9.

If you are only interested in synthesizing speech with the released TTS models, installing from PyPI is the easiest option.

pip install TTS

If you plan to code or train models, clone TTS and install it locally.

git clone https://github.com/mozilla/TTS
pip install -e .

Directory Structure

|- notebooks/       (Jupyter Notebooks for model evaluation, parameter selection and data analysis.)
|- utils/           (common utilities.)
|- TTS
    |- bin/             (folder for all the executables.)
      |- train*.py                  (train your target model.)
      |- distribute.py              (train your TTS model using Multiple GPUs.)
      |- compute_statistics.py      (compute dataset statistics for normalization.)
      |- convert*.py                (convert target torch model to TF.)
    |- tts/             (text to speech models)
        |- layers/          (model layer definitions)
        |- models/          (model definitions)
        |- tf/              (Tensorflow 2 utilities and model implementations)
        |- utils/           (model specific utilities.)
    |- speaker_encoder/ (Speaker Encoder models.)
        |- (same)
    |- vocoder/         (Vocoder models.)
        |- (same)

Sample Model Output

Below you see Tacotron model state after 16K iterations with batch-size 32 with LJSpeech dataset.

"Recent research at Harvard has shown meditating for as little as 8 weeks can actually increase the grey matter in the parts of the brain responsible for emotional regulation and learning."

Audio examples: soundcloud

example_output

Datasets and Data-Loading

TTS provides a generic dataloader easy to use for your custom dataset. You just need to write a simple function to format the dataset. Check datasets/preprocess.py to see some examples. After that, you need to set dataset fields in config.json.

Some of the public datasets that we successfully applied TTS:

Example: Synthesizing Speech on Terminal Using the Released Models.

After the installation, TTS provides a CLI interface for synthesizing speech using pre-trained models. You can either use your own model or the release models under the TTS project.

Listing released TTS models.

tts --list_models

Run a tts and a vocoder model from the released model list. (Simply copy and paste the full model names from the list as arguments for the command below.)

tts --text "Text for TTS" \
    --model_name "<type>/<language>/<dataset>/<model_name>" \
    --vocoder_name "<type>/<language>/<dataset>/<model_name>" \
    --out_path folder/to/save/output/

Run your own TTS model (Using Griffin-Lim Vocoder)

tts --text "Text for TTS" \
    --model_path path/to/model.pth.tar \
    --config_path path/to/config.json \
    --out_path output/path/speech.wav

Run your own TTS and Vocoder models

tts --text "Text for TTS" \
    --model_path path/to/config.json \
    --config_path path/to/model.pth.tar \
    --out_path output/path/speech.wav \
    --vocoder_path path/to/vocoder.pth.tar \
    --vocoder_config_path path/to/vocoder_config.json

Note: You can use ./TTS/bin/synthesize.py if you prefer running tts from the TTS project folder.

Example: Training and Fine-tuning LJ-Speech Dataset

Here you can find a CoLab notebook for a hands-on example, training LJSpeech. Or you can manually follow the guideline below.

To start with, split metadata.csv into train and validation subsets respectively metadata_train.csv and metadata_val.csv. Note that for text-to-speech, validation performance might be misleading since the loss value does not directly measure the voice quality to the human ear and it also does not measure the attention module performance. Therefore, running the model with new sentences and listening to the results is the best way to go.

shuf metadata.csv > metadata_shuf.csv
head -n 12000 metadata_shuf.csv > metadata_train.csv
tail -n 1100 metadata_shuf.csv > metadata_val.csv

To train a new model, you need to define your own config.json to define model details, trainin configuration and more (check the examples). Then call the corressponding train script.

For instance, in order to train a tacotron or tacotron2 model on LJSpeech dataset, follow these steps.

python TTS/bin/train_tacotron.py --config_path TTS/tts/configs/config.json

To fine-tune a model, use --restore_path.

python TTS/bin/train_tacotron.py --config_path TTS/tts/configs/config.json --restore_path /path/to/your/model.pth.tar

To continue an old training run, use --continue_path.

python TTS/bin/train_tacotron.py --continue_path /path/to/your/run_folder/

For multi-GPU training, call distribute.py. It runs any provided train script in multi-GPU setting.

CUDA_VISIBLE_DEVICES="0,1,4" python TTS/bin/distribute.py --script train_tacotron.py --config_path TTS/tts/configs/config.json

Each run creates a new output folder accomodating used config.json, model checkpoints and tensorboard logs.

In case of any error or intercepted execution, if there is no checkpoint yet under the output folder, the whole folder is going to be removed.

You can also enjoy Tensorboard, if you point Tensorboard argument--logdir to the experiment folder.

Contribution Guidelines

This repository is governed by Mozilla's code of conduct and etiquette guidelines. For more details, please read the Mozilla Community Participation Guidelines.

  1. Create a new branch.
  2. Implement your changes.
  3. (if applicable) Add Google Style docstrings.
  4. (if applicable) Implement a test case under tests folder.
  5. (Optional but Prefered) Run tests.
./run_tests.sh
  1. Run the linter.
pip install pylint cardboardlint
cardboardlinter --refspec master
  1. Send a PR to dev branch, explain what the change is about.
  2. Let us discuss until we make it perfect :).
  3. We merge it to the dev branch once things look good.

Feel free to ping us at any step you need help using our communication channels.

Collaborative Experimentation Guide

If you like to use TTS to try a new idea and like to share your experiments with the community, we urge you to use the following guideline for a better collaboration. (If you have an idea for better collaboration, let us know)

  • Create a new branch.
  • Open an issue pointing your branch.
  • Explain your idea and experiment.
  • Share your results regularly. (Tensorboard log files, audio results, visuals etc.)

Major TODOs

Acknowledgement

Author: Mozilla
Source Code: https://github.com/mozilla/TTS 
License: MPL-2.0 License

#python #text  #deep-learning 

What is GEEK

Buddha Community

TTS: Text-to-Speech for All
Vincent Lab

Vincent Lab

1605177504

Text to Speech in Node.js

In this video, I will be showing you how to turn text into speech in Node.js

#javascript #text to speech #javascript api #text to speech app #node.js text to speech #javascript text to speech

Navigating Between DOM Nodes in JavaScript

In the previous chapters you've learnt how to select individual elements on a web page. But there are many occasions where you need to access a child, parent or ancestor element. See the JavaScript DOM nodes chapter to understand the logical relationships between the nodes in a DOM tree.

DOM node provides several properties and methods that allow you to navigate or traverse through the tree structure of the DOM and make changes very easily. In the following section we will learn how to navigate up, down, and sideways in the DOM tree using JavaScript.

Accessing the Child Nodes

You can use the firstChild and lastChild properties of the DOM node to access the first and last direct child node of a node, respectively. If the node doesn't have any child element, it returns null.

Example

<div id="main">
    <h1 id="title">My Heading</h1>
    <p id="hint"><span>This is some text.</span></p>
</div>

<script>
var main = document.getElementById("main");
console.log(main.firstChild.nodeName); // Prints: #text

var hint = document.getElementById("hint");
console.log(hint.firstChild.nodeName); // Prints: SPAN
</script>

Note: The nodeName is a read-only property that returns the name of the current node as a string. For example, it returns the tag name for element node, #text for text node, #comment for comment node, #document for document node, and so on.

If you notice the above example, the nodeName of the first-child node of the main DIV element returns #text instead of H1. Because, whitespace such as spaces, tabs, newlines, etc. are valid characters and they form #text nodes and become a part of the DOM tree. Therefore, since the <div> tag contains a newline before the <h1> tag, so it will create a #text node.

To avoid the issue with firstChild and lastChild returning #text or #comment nodes, you could alternatively use the firstElementChild and lastElementChild properties to return only the first and last element node, respectively. But, it will not work in IE 9 and earlier.

Example

<div id="main">
    <h1 id="title">My Heading</h1>
    <p id="hint"><span>This is some text.</span></p>
</div>

<script>
var main = document.getElementById("main");
alert(main.firstElementChild.nodeName); // Outputs: H1
main.firstElementChild.style.color = "red";

var hint = document.getElementById("hint");
alert(hint.firstElementChild.nodeName); // Outputs: SPAN
hint.firstElementChild.style.color = "blue";
</script>

Similarly, you can use the childNodes property to access all child nodes of a given element, where the first child node is assigned index 0. Here's an example:

Example

<div id="main">
    <h1 id="title">My Heading</h1>
    <p id="hint"><span>This is some text.</span></p>
</div>

<script>
var main = document.getElementById("main");

// First check that the element has child nodes 
if(main.hasChildNodes()) {
    var nodes = main.childNodes;
    
    // Loop through node list and display node name
    for(var i = 0; i < nodes.length; i++) {
        alert(nodes[i].nodeName);
    }
}
</script>

The childNodes returns all child nodes, including non-element nodes like text and comment nodes. To get a collection of only elements, use children property instead.

Example

<div id="main">
    <h1 id="title">My Heading</h1>
    <p id="hint"><span>This is some text.</span></p>
</div>

<script>
var main = document.getElementById("main");

// First check that the element has child nodes 
if(main.hasChildNodes()) {
    var nodes = main.children;
    
    // Loop through node list and display node name
    for(var i = 0; i < nodes.length; i++) {
        alert(nodes[i].nodeName);
    }
}
</script>

#javascript 

Cómo construir un detector de noticias falsas en Python

Detección de noticias falsas en Python

Explorar el conjunto de datos de noticias falsas, realizar análisis de datos como nubes de palabras y ngramas, y ajustar el transformador BERT para construir un detector de noticias falsas en Python usando la biblioteca de transformadores.

Las noticias falsas son la transmisión intencional de afirmaciones falsas o engañosas como noticias, donde las declaraciones son deliberadamente engañosas.

Los periódicos, tabloides y revistas han sido reemplazados por plataformas de noticias digitales, blogs, fuentes de redes sociales y una plétora de aplicaciones de noticias móviles. Las organizaciones de noticias se beneficiaron del mayor uso de las redes sociales y las plataformas móviles al proporcionar a los suscriptores información actualizada al minuto.

Los consumidores ahora tienen acceso instantáneo a las últimas noticias. Estas plataformas de medios digitales han aumentado en importancia debido a su fácil conexión con el resto del mundo y permiten a los usuarios discutir y compartir ideas y debatir temas como la democracia, la educación, la salud, la investigación y la historia. Las noticias falsas en las plataformas digitales son cada vez más populares y se utilizan con fines de lucro, como ganancias políticas y financieras.

¿Qué tan grande es este problema?

Debido a que Internet, las redes sociales y las plataformas digitales son ampliamente utilizadas, cualquiera puede propagar información inexacta y sesgada. Es casi imposible evitar la difusión de noticias falsas. Hay un aumento tremendo en la distribución de noticias falsas, que no se restringe a un sector como la política sino que incluye deportes, salud, historia, entretenimiento y ciencia e investigación.

La solución

Es vital reconocer y diferenciar entre noticias falsas y veraces. Un método es hacer que un experto decida y verifique cada pieza de información, pero esto lleva tiempo y requiere experiencia que no se puede compartir. En segundo lugar, podemos utilizar herramientas de aprendizaje automático e inteligencia artificial para automatizar la identificación de noticias falsas.

La información de noticias en línea incluye varios datos en formato no estructurado (como documentos, videos y audio), pero aquí nos concentraremos en las noticias en formato de texto. Con el progreso del aprendizaje automático y el procesamiento del lenguaje natural , ahora podemos reconocer el carácter engañoso y falso de un artículo o declaración.

Se están realizando varios estudios y experimentos para detectar noticias falsas en todos los medios.

Nuestro objetivo principal de este tutorial es:

  • Explore y analice el conjunto de datos de noticias falsas.
  • Cree un clasificador que pueda distinguir noticias falsas con la mayor precisión posible.

Aquí está la tabla de contenido:

  • Introducción
  • ¿Qué tan grande es este problema?
  • La solución
  • Exploración de datos
    • Distribución de Clases
  • Limpieza de datos para análisis
  • Análisis exploratorio de datos
    • Nube de una sola palabra
    • Bigrama más frecuente (combinación de dos palabras)
    • Trigrama más frecuente (combinación de tres palabras)
  • Creación de un clasificador mediante el ajuste fino de BERT
    • Preparación de datos
    • Tokenización del conjunto de datos
    • Cargar y ajustar el modelo
    • Evaluación del modelo
  • Apéndice: Creación de un archivo de envío para Kaggle
  • Conclusión

Exploración de datos

En este trabajo, utilizamos el conjunto de datos de noticias falsas de Kaggle para clasificar artículos de noticias no confiables como noticias falsas. Disponemos de un completo dataset de entrenamiento que contiene las siguientes características:

  • id: identificación única para un artículo de noticias
  • title: título de un artículo periodístico
  • author: autor de la noticia
  • text: texto del artículo; podría estar incompleto
  • label: una etiqueta que marca el artículo como potencialmente no confiable denotado por 1 (poco confiable o falso) o 0 (confiable).

Es un problema de clasificación binaria en el que debemos predecir si una determinada noticia es fiable o no.

Si tiene una cuenta de Kaggle, simplemente puede descargar el conjunto de datos del sitio web y extraer el archivo ZIP.

También cargué el conjunto de datos en Google Drive y puede obtenerlo aquí o usar la gdownbiblioteca para descargarlo automáticamente en Google Colab o cuadernos de Jupyter:

$ pip install gdown
# download from Google Drive
$ gdown "https://drive.google.com/uc?id=178f_VkNxccNidap-5-uffXUW475pAuPy&confirm=t"
Downloading...
From: https://drive.google.com/uc?id=178f_VkNxccNidap-5-uffXUW475pAuPy&confirm=t
To: /content/fake-news.zip
100% 48.7M/48.7M [00:00<00:00, 74.6MB/s]

Descomprimiendo los archivos:

$ unzip fake-news.zip

Aparecerán tres archivos en el directorio de trabajo actual: train.csv, test.csvy submit.csv, que usaremos train.csven la mayor parte del tutorial.

Instalando las dependencias requeridas:

$ pip install transformers nltk pandas numpy matplotlib seaborn wordcloud

Nota: si se encuentra en un entorno local, asegúrese de instalar PyTorch para GPU, diríjase a esta página para una instalación adecuada.

Importemos las bibliotecas esenciales para el análisis:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

El corpus y los módulos NLTK deben instalarse mediante el descargador NLTK estándar:

import nltk
nltk.download('stopwords')
nltk.download('wordnet')

El conjunto de datos de noticias falsas comprende títulos y textos de artículos originales y ficticios de varios autores. Importemos nuestro conjunto de datos:

# load the dataset
news_d = pd.read_csv("train.csv")
print("Shape of News data:", news_d.shape)
print("News data columns", news_d.columns)

Producción:

 Shape of News data: (20800, 5)
 News data columns Index(['id', 'title', 'author', 'text', 'label'], dtype='object')

Así es como se ve el conjunto de datos:

# by using df.head(), we can immediately familiarize ourselves with the dataset. 
news_d.head()

Producción:

id	title	author	text	label
0	0	House Dem Aide: We Didn’t Even See Comey’s Let...	Darrell Lucus	House Dem Aide: We Didn’t Even See Comey’s Let...	1
1	1	FLYNN: Hillary Clinton, Big Woman on Campus - ...	Daniel J. Flynn	Ever get the feeling your life circles the rou...	0
2	2	Why the Truth Might Get You Fired	Consortiumnews.com	Why the Truth Might Get You Fired October 29, ...	1
3	3	15 Civilians Killed In Single US Airstrike Hav...	Jessica Purkiss	Videos 15 Civilians Killed In Single US Airstr...	1
4	4	Iranian woman jailed for fictional unpublished...	Howard Portnoy	Print \nAn Iranian woman has been sentenced to...	1

Tenemos 20.800 filas, que tienen cinco columnas. Veamos algunas estadísticas de la textcolumna:

#Text Word startistics: min.mean, max and interquartile range

txt_length = news_d.text.str.split().str.len()
txt_length.describe()

Producción:

count    20761.000000
mean       760.308126
std        869.525988
min          0.000000
25%        269.000000
50%        556.000000
75%       1052.000000
max      24234.000000
Name: text, dtype: float64

Estadísticas de la titlecolumna:

#Title statistics 

title_length = news_d.title.str.split().str.len()
title_length.describe()

Producción:

count    20242.000000
mean        12.420709
std          4.098735
min          1.000000
25%         10.000000
50%         13.000000
75%         15.000000
max         72.000000
Name: title, dtype: float64

Las estadísticas para los conjuntos de entrenamiento y prueba son las siguientes:

  • El textatributo tiene un conteo de palabras más alto con un promedio de 760 palabras y un 75% con más de 1000 palabras.
  • El titleatributo es una declaración breve con un promedio de 12 palabras, y el 75% de ellas tiene alrededor de 15 palabras.

Nuestro experimento sería con el texto y el título juntos.

Distribución de Clases

Parcelas de conteo para ambas etiquetas:

sns.countplot(x="label", data=news_d);
print("1: Unreliable")
print("0: Reliable")
print("Distribution of labels:")
print(news_d.label.value_counts());

Producción:

1: Unreliable
0: Reliable
Distribution of labels:
1    10413
0    10387
Name: label, dtype: int64

Distribución de etiquetas

print(round(news_d.label.value_counts(normalize=True),2)*100);

Producción:

1    50.0
0    50.0
Name: label, dtype: float64

La cantidad de artículos no confiables (falsos o 1) es 10413, mientras que la cantidad de artículos confiables (confiables o 0) es 10387. Casi el 50% de los artículos son falsos. Por lo tanto, la métrica de precisión medirá qué tan bien funciona nuestro modelo al construir un clasificador.

Limpieza de datos para análisis

En esta sección, limpiaremos nuestro conjunto de datos para hacer algunos análisis:

  • Elimina las filas y columnas que no uses.
  • Realizar imputación de valor nulo.
  • Eliminar caracteres especiales.
  • Elimina las palabras vacías.
# Constants that are used to sanitize the datasets 

column_n = ['id', 'title', 'author', 'text', 'label']
remove_c = ['id','author']
categorical_features = []
target_col = ['label']
text_f = ['title', 'text']
# Clean Datasets
import nltk
from nltk.corpus import stopwords
import re
from nltk.stem.porter import PorterStemmer
from collections import Counter

ps = PorterStemmer()
wnl = nltk.stem.WordNetLemmatizer()

stop_words = stopwords.words('english')
stopwords_dict = Counter(stop_words)

# Removed unused clumns
def remove_unused_c(df,column_n=remove_c):
    df = df.drop(column_n,axis=1)
    return df

# Impute null values with None
def null_process(feature_df):
    for col in text_f:
        feature_df.loc[feature_df[col].isnull(), col] = "None"
    return feature_df

def clean_dataset(df):
    # remove unused column
    df = remove_unused_c(df)
    #impute null values
    df = null_process(df)
    return df

# Cleaning text from unused characters
def clean_text(text):
    text = str(text).replace(r'http[\w:/\.]+', ' ')  # removing urls
    text = str(text).replace(r'[^\.\w\s]', ' ')  # remove everything but characters and punctuation
    text = str(text).replace('[^a-zA-Z]', ' ')
    text = str(text).replace(r'\s\s+', ' ')
    text = text.lower().strip()
    #text = ' '.join(text)    
    return text

## Nltk Preprocessing include:
# Stop words, Stemming and Lemmetization
# For our project we use only Stop word removal
def nltk_preprocess(text):
    text = clean_text(text)
    wordlist = re.sub(r'[^\w\s]', '', text).split()
    #text = ' '.join([word for word in wordlist if word not in stopwords_dict])
    #text = [ps.stem(word) for word in wordlist if not word in stopwords_dict]
    text = ' '.join([wnl.lemmatize(word) for word in wordlist if word not in stopwords_dict])
    return  text

En el bloque de código de arriba:

  • Hemos importado NLTK, que es una plataforma famosa para desarrollar aplicaciones de Python que interactúan con el lenguaje humano. A continuación, importamos repara expresiones regulares.
  • Importamos palabras vacías desde nltk.corpus. Cuando trabajamos con palabras, particularmente cuando consideramos la semántica, a veces necesitamos eliminar palabras comunes que no agregan ningún significado significativo a una declaración, como "but", "can", "we", etc.
  • PorterStemmerse utiliza para realizar palabras derivadas con NLTK. Los lematizadores despojan a las palabras de sus afijos morfológicos, dejando únicamente la raíz de la palabra.
  • Importamos WordNetLemmatizer()de la biblioteca NLTK para la lematización. La lematización es mucho más eficaz que la derivación . Va más allá de la reducción de palabras y evalúa todo el léxico de un idioma para aplicar el análisis morfológico a las palabras, con el objetivo de eliminar los extremos flexivos y devolver la forma base o de diccionario de una palabra, conocida como lema.
  • stopwords.words('english')permítanos ver la lista de todas las palabras vacías en inglés admitidas por NLTK.
  • remove_unused_c()La función se utiliza para eliminar las columnas no utilizadas.
  • Imputamos valores nulos con Noneel uso de la null_process()función.
  • Dentro de la función clean_dataset(), llamamos remove_unused_c()y null_process()funciones. Esta función es responsable de la limpieza de datos.
  • Para limpiar texto de caracteres no utilizados, hemos creado la clean_text()función.
  • Para el preprocesamiento, solo utilizaremos la eliminación de palabras vacías. Creamos la nltk_preprocess()función para ese propósito.

Preprocesando el texty title:

# Perform data cleaning on train and test dataset by calling clean_dataset function
df = clean_dataset(news_d)
# apply preprocessing on text through apply method by calling the function nltk_preprocess
df["text"] = df.text.apply(nltk_preprocess)
# apply preprocessing on title through apply method by calling the function nltk_preprocess
df["title"] = df.title.apply(nltk_preprocess)
# Dataset after cleaning and preprocessing step
df.head()

Producción:

title	text	label
0	house dem aide didnt even see comeys letter ja...	house dem aide didnt even see comeys letter ja...	1
1	flynn hillary clinton big woman campus breitbart	ever get feeling life circle roundabout rather...	0
2	truth might get fired	truth might get fired october 29 2016 tension ...	1
3	15 civilian killed single u airstrike identified	video 15 civilian killed single u airstrike id...	1
4	iranian woman jailed fictional unpublished sto...	print iranian woman sentenced six year prison ...	1

Análisis exploratorio de datos

En esta sección realizaremos:

  • Análisis Univariante : Es un análisis estadístico del texto. Usaremos la nube de palabras para ese propósito. Una nube de palabras es un enfoque de visualización de datos de texto donde el término más común se presenta en el tamaño de fuente más considerable.
  • Análisis bivariado : Bigram y Trigram se utilizarán aquí. Según Wikipedia: " un n-grama es una secuencia contigua de n elementos de una muestra determinada de texto o habla. Según la aplicación, los elementos pueden ser fonemas, sílabas, letras, palabras o pares de bases. Los n-gramas normalmente se recopilan de un corpus de texto o de voz".

Nube de una sola palabra

Las palabras más frecuentes aparecen en negrita y de mayor tamaño en una nube de palabras. Esta sección creará una nube de palabras para todas las palabras del conjunto de datos.

Se usará la función de la biblioteca de WordCloudwordcloud() y generate()se utilizará para generar la imagen de la nube de palabras:

from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt

# initialize the word cloud
wordcloud = WordCloud( background_color='black', width=800, height=600)
# generate the word cloud by passing the corpus
text_cloud = wordcloud.generate(' '.join(df['text']))
# plotting the word cloud
plt.figure(figsize=(20,30))
plt.imshow(text_cloud)
plt.axis('off')
plt.show()

Producción:

WordCloud para todos los datos de noticias falsas

Nube de palabras solo para noticias confiables:

true_n = ' '.join(df[df['label']==0]['text']) 
wc = wordcloud.generate(true_n)
plt.figure(figsize=(20,30))
plt.imshow(wc)
plt.axis('off')
plt.show()

Producción:

Nube de palabras para noticias confiables

Nube de palabras solo para noticias falsas:

fake_n = ' '.join(df[df['label']==1]['text'])
wc= wordcloud.generate(fake_n)
plt.figure(figsize=(20,30))
plt.imshow(wc)
plt.axis('off')
plt.show()

Producción:

Nube de palabras para noticias falsas

Bigrama más frecuente (combinación de dos palabras)

Un N-grama es una secuencia de letras o palabras. Un unigrama de carácter se compone de un solo carácter, mientras que un bigrama comprende una serie de dos caracteres. De manera similar, los N-gramas de palabras se componen de una serie de n palabras. La palabra "unidos" es un 1 gramo (unigrama). La combinación de las palabras "estado unido" es de 2 gramos (bigrama), "ciudad de nueva york" es de 3 gramos.

Grafiquemos el bigrama más común en las noticias confiables:

def plot_top_ngrams(corpus, title, ylabel, xlabel="Number of Occurences", n=2):
  """Utility function to plot top n-grams"""
  true_b = (pd.Series(nltk.ngrams(corpus.split(), n)).value_counts())[:20]
  true_b.sort_values().plot.barh(color='blue', width=.9, figsize=(12, 8))
  plt.title(title)
  plt.ylabel(ylabel)
  plt.xlabel(xlabel)
  plt.show()
plot_top_ngrams(true_n, 'Top 20 Frequently Occuring True news Bigrams', "Bigram", n=2)

Top bigramas sobre noticias falsas

El bigrama más común en las noticias falsas:

plot_top_ngrams(fake_n, 'Top 20 Frequently Occuring Fake news Bigrams', "Bigram", n=2)

Top bigramas sobre noticias falsas

Trigrama más frecuente (combinación de tres palabras)

El trigrama más común en noticias confiables:

plot_top_ngrams(true_n, 'Top 20 Frequently Occuring True news Trigrams', "Trigrams", n=3)

El trigrama más común en las noticias falsas

Para noticias falsas ahora:

plot_top_ngrams(fake_n, 'Top 20 Frequently Occuring Fake news Trigrams', "Trigrams", n=3)

Trigramas más comunes en Fake news

Los gráficos anteriores nos dan algunas ideas sobre cómo se ven ambas clases. En la siguiente sección, usaremos la biblioteca de transformadores para construir un detector de noticias falsas.

Creación de un clasificador mediante el ajuste fino de BERT

Esta sección tomará código ampliamente del tutorial BERT de ajuste fino para hacer un clasificador de noticias falsas utilizando la biblioteca de transformadores. Entonces, para obtener información más detallada, puede dirigirse al tutorial original .

Si no instaló transformadores, debe:

$ pip install transformers

Importemos las bibliotecas necesarias:

import torch
from transformers.file_utils import is_tf_available, is_torch_available, is_torch_tpu_available
from transformers import BertTokenizerFast, BertForSequenceClassification
from transformers import Trainer, TrainingArguments
import numpy as np
from sklearn.model_selection import train_test_split

import random

Queremos que nuestros resultados sean reproducibles incluso si reiniciamos nuestro entorno:

def set_seed(seed: int):
    """
    Helper function for reproducible behavior to set the seed in ``random``, ``numpy``, ``torch`` and/or ``tf`` (if
    installed).

    Args:
        seed (:obj:`int`): The seed to set.
    """
    random.seed(seed)
    np.random.seed(seed)
    if is_torch_available():
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)
        # ^^ safe to call this function even if cuda is not available
    if is_tf_available():
        import tensorflow as tf

        tf.random.set_seed(seed)

set_seed(1)

El modelo que vamos a utilizar es el bert-base-uncased:

# the model we gonna train, base uncased BERT
# check text classification models here: https://huggingface.co/models?filter=text-classification
model_name = "bert-base-uncased"
# max sequence length for each document/sentence sample
max_length = 512

Cargando el tokenizador:

# load the tokenizer
tokenizer = BertTokenizerFast.from_pretrained(model_name, do_lower_case=True)

Preparación de datos

Limpiemos ahora los NaNvalores de las columnas text, authory :title

news_df = news_d[news_d['text'].notna()]
news_df = news_df[news_df["author"].notna()]
news_df = news_df[news_df["title"].notna()]

A continuación, crear una función que tome el conjunto de datos como un marco de datos de Pandas y devuelva las divisiones de entrenamiento/validación de textos y etiquetas como listas:

def prepare_data(df, test_size=0.2, include_title=True, include_author=True):
  texts = []
  labels = []
  for i in range(len(df)):
    text = df["text"].iloc[i]
    label = df["label"].iloc[i]
    if include_title:
      text = df["title"].iloc[i] + " - " + text
    if include_author:
      text = df["author"].iloc[i] + " : " + text
    if text and label in [0, 1]:
      texts.append(text)
      labels.append(label)
  return train_test_split(texts, labels, test_size=test_size)

train_texts, valid_texts, train_labels, valid_labels = prepare_data(news_df)

La función anterior toma el conjunto de datos en un tipo de marco de datos y los devuelve como listas divididas en conjuntos de entrenamiento y validación. Establecer include_titleen Truesignifica que agregamos la titlecolumna a la textque vamos a usar para el entrenamiento, establecer include_authoren Truesignifica que también agregamos authoral texto.

Asegurémonos de que las etiquetas y los textos tengan la misma longitud:

print(len(train_texts), len(train_labels))
print(len(valid_texts), len(valid_labels))

Producción:

14628 14628
3657 3657

Tokenización del conjunto de datos

Usemos el tokenizador BERT para tokenizar nuestro conjunto de datos:

# tokenize the dataset, truncate when passed `max_length`, 
# and pad with 0's when less than `max_length`
train_encodings = tokenizer(train_texts, truncation=True, padding=True, max_length=max_length)
valid_encodings = tokenizer(valid_texts, truncation=True, padding=True, max_length=max_length)

Convertir las codificaciones en un conjunto de datos de PyTorch:

class NewsGroupsDataset(torch.utils.data.Dataset):
    def __init__(self, encodings, labels):
        self.encodings = encodings
        self.labels = labels

    def __getitem__(self, idx):
        item = {k: torch.tensor(v[idx]) for k, v in self.encodings.items()}
        item["labels"] = torch.tensor([self.labels[idx]])
        return item

    def __len__(self):
        return len(self.labels)

# convert our tokenized data into a torch Dataset
train_dataset = NewsGroupsDataset(train_encodings, train_labels)
valid_dataset = NewsGroupsDataset(valid_encodings, valid_labels)

Cargar y ajustar el modelo

Usaremos BertForSequenceClassificationpara cargar nuestro modelo de transformador BERT:

# load the model
model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2)

Establecemos num_labelsa 2 ya que es una clasificación binaria. A continuación, la función es una devolución de llamada para calcular la precisión en cada paso de validación:

from sklearn.metrics import accuracy_score

def compute_metrics(pred):
  labels = pred.label_ids
  preds = pred.predictions.argmax(-1)
  # calculate accuracy using sklearn's function
  acc = accuracy_score(labels, preds)
  return {
      'accuracy': acc,
  }

Vamos a inicializar los parámetros de entrenamiento:

training_args = TrainingArguments(
    output_dir='./results',          # output directory
    num_train_epochs=1,              # total number of training epochs
    per_device_train_batch_size=10,  # batch size per device during training
    per_device_eval_batch_size=20,   # batch size for evaluation
    warmup_steps=100,                # number of warmup steps for learning rate scheduler
    logging_dir='./logs',            # directory for storing logs
    load_best_model_at_end=True,     # load the best model when finished training (default metric is loss)
    # but you can specify `metric_for_best_model` argument to change to accuracy or other metric
    logging_steps=200,               # log & save weights each logging_steps
    save_steps=200,
    evaluation_strategy="steps",     # evaluate each `logging_steps`
)

Configuré el valor per_device_train_batch_sizeen 10, pero debe configurarlo tan alto como su GPU pueda caber. Establecer el logging_stepsy save_stepsen 200, lo que significa que vamos a realizar una evaluación y guardar los pesos del modelo en cada 200 pasos de entrenamiento.

Puede consultar  esta página  para obtener información más detallada sobre los parámetros de entrenamiento disponibles.

Instanciamos el entrenador:

trainer = Trainer(
    model=model,                         # the instantiated Transformers model to be trained
    args=training_args,                  # training arguments, defined above
    train_dataset=train_dataset,         # training dataset
    eval_dataset=valid_dataset,          # evaluation dataset
    compute_metrics=compute_metrics,     # the callback that computes metrics of interest
)

Entrenamiento del modelo:

# train the model
trainer.train()

El entrenamiento tarda unas horas en finalizar, dependiendo de su GPU. Si está en la versión gratuita de Colab, debería tomar una hora con NVIDIA Tesla K80. Aquí está la salida:

***** Running training *****
  Num examples = 14628
  Num Epochs = 1
  Instantaneous batch size per device = 10
  Total train batch size (w. parallel, distributed & accumulation) = 10
  Gradient Accumulation steps = 1
  Total optimization steps = 1463
 [1463/1463 41:07, Epoch 1/1]
Step	Training Loss	Validation Loss	Accuracy
200		0.250800		0.100533		0.983867
400		0.027600		0.043009		0.993437
600		0.023400		0.017812		0.997539
800		0.014900		0.030269		0.994258
1000	0.022400		0.012961		0.998086
1200	0.009800		0.010561		0.998633
1400	0.007700		0.010300		0.998633
***** Running Evaluation *****
  Num examples = 3657
  Batch size = 20
Saving model checkpoint to ./results/checkpoint-200
Configuration saved in ./results/checkpoint-200/config.json
Model weights saved in ./results/checkpoint-200/pytorch_model.bin
<SNIPPED>
***** Running Evaluation *****
  Num examples = 3657
  Batch size = 20
Saving model checkpoint to ./results/checkpoint-1400
Configuration saved in ./results/checkpoint-1400/config.json
Model weights saved in ./results/checkpoint-1400/pytorch_model.bin

Training completed. Do not forget to share your model on huggingface.co/models =)

Loading best model from ./results/checkpoint-1400 (score: 0.010299865156412125).
TrainOutput(global_step=1463, training_loss=0.04888018785440506, metrics={'train_runtime': 2469.1722, 'train_samples_per_second': 5.924, 'train_steps_per_second': 0.593, 'total_flos': 3848788517806080.0, 'train_loss': 0.04888018785440506, 'epoch': 1.0})

Evaluación del modelo

Dado que load_best_model_at_endestá configurado en True, los mejores pesos se cargarán cuando se complete el entrenamiento. Vamos a evaluarlo con nuestro conjunto de validación:

# evaluate the current model after training
trainer.evaluate()

Producción:

***** Running Evaluation *****
  Num examples = 3657
  Batch size = 20
 [183/183 02:11]
{'epoch': 1.0,
 'eval_accuracy': 0.998632759092152,
 'eval_loss': 0.010299865156412125,
 'eval_runtime': 132.0374,
 'eval_samples_per_second': 27.697,
 'eval_steps_per_second': 1.386}

Guardando el modelo y el tokenizador:

# saving the fine tuned model & tokenizer
model_path = "fake-news-bert-base-uncased"
model.save_pretrained(model_path)
tokenizer.save_pretrained(model_path)

Aparecerá una nueva carpeta que contiene la configuración del modelo y los pesos después de ejecutar la celda anterior. Si desea realizar una predicción, simplemente use el from_pretrained()método que usamos cuando cargamos el modelo, y ya está listo.

A continuación, hagamos una función que acepte el texto del artículo como argumento y devuelva si es falso o no:

def get_prediction(text, convert_to_label=False):
    # prepare our text into tokenized sequence
    inputs = tokenizer(text, padding=True, truncation=True, max_length=max_length, return_tensors="pt").to("cuda")
    # perform inference to our model
    outputs = model(**inputs)
    # get output probabilities by doing softmax
    probs = outputs[0].softmax(1)
    # executing argmax function to get the candidate label
    d = {
        0: "reliable",
        1: "fake"
    }
    if convert_to_label:
      return d[int(probs.argmax())]
    else:
      return int(probs.argmax())

Tomé un ejemplo de test.csvque el modelo nunca vio para realizar inferencias, lo verifiqué y es un artículo real de The New York Times:

real_news = """
Tim Tebow Will Attempt Another Comeback, This Time in Baseball - The New York Times",Daniel Victor,"If at first you don’t succeed, try a different sport. Tim Tebow, who was a Heisman   quarterback at the University of Florida but was unable to hold an N. F. L. job, is pursuing a career in Major League Baseball. <SNIPPED>
"""

El texto original está en el entorno de Colab si desea copiarlo, ya que es un artículo completo. Vamos a pasarlo al modelo y ver los resultados:

get_prediction(real_news, convert_to_label=True)

Producción:

reliable

Apéndice: Creación de un archivo de envío para Kaggle

En esta sección, predeciremos todos los artículos en el test.csvpara crear un archivo de envío para ver nuestra precisión en la prueba establecida en la competencia Kaggle :

# read the test set
test_df = pd.read_csv("test.csv")
# make a copy of the testing set
new_df = test_df.copy()
# add a new column that contains the author, title and article content
new_df["new_text"] = new_df["author"].astype(str) + " : " + new_df["title"].astype(str) + " - " + new_df["text"].astype(str)
# get the prediction of all the test set
new_df["label"] = new_df["new_text"].apply(get_prediction)
# make the submission file
final_df = new_df[["id", "label"]]
final_df.to_csv("submit_final.csv", index=False)

Después de concatenar el autor, el título y el texto del artículo, pasamos la get_prediction()función a la nueva columna para llenar la labelcolumna, luego usamos to_csv()el método para crear el archivo de envío para Kaggle. Aquí está mi puntaje de presentación:

Puntuación de envío

Obtuvimos una precisión del 99,78 % y del 100 % en las tablas de clasificación privadas y públicas. ¡Eso es genial!

Conclusión

Muy bien, hemos terminado con el tutorial. Puede consultar esta página para ver varios parámetros de entrenamiento que puede modificar.

Si tiene un conjunto de datos de noticias falsas personalizado para ajustarlo, simplemente tiene que pasar una lista de muestras al tokenizador como lo hicimos nosotros, no cambiará ningún otro código después de eso.

Consulta el código completo aquí , o el entorno de Colab aquí .

Comment créer un détecteur de fausses nouvelles en Python

Détection de fausses nouvelles en Python

Explorer l'ensemble de données de fausses nouvelles, effectuer une analyse de données telles que des nuages ​​​​de mots et des ngrams, et affiner le transformateur BERT pour créer un détecteur de fausses nouvelles en Python à l'aide de la bibliothèque de transformateurs.

Les fausses nouvelles sont la diffusion intentionnelle d'allégations fausses ou trompeuses en tant que nouvelles, où les déclarations sont délibérément mensongères.

Les journaux, les tabloïds et les magazines ont été supplantés par les plateformes d'actualités numériques, les blogs, les flux de médias sociaux et une pléthore d'applications d'actualités mobiles. Les organes de presse ont profité de l'utilisation accrue des médias sociaux et des plates-formes mobiles en fournissant aux abonnés des informations de dernière minute.

Les consommateurs ont désormais un accès instantané aux dernières nouvelles. Ces plateformes de médias numériques ont gagné en importance en raison de leur connectivité facile au reste du monde et permettent aux utilisateurs de discuter et de partager des idées et de débattre de sujets tels que la démocratie, l'éducation, la santé, la recherche et l'histoire. Les fausses informations sur les plateformes numériques deviennent de plus en plus populaires et sont utilisées à des fins lucratives, telles que des gains politiques et financiers.

Quelle est la taille de ce problème ?

Parce qu'Internet, les médias sociaux et les plateformes numériques sont largement utilisés, n'importe qui peut propager des informations inexactes et biaisées. Il est presque impossible d'empêcher la diffusion de fausses nouvelles. Il y a une énorme augmentation de la diffusion de fausses nouvelles, qui ne se limite pas à un secteur comme la politique, mais comprend le sport, la santé, l'histoire, le divertissement, la science et la recherche.

La solution

Il est essentiel de reconnaître et de différencier les informations fausses des informations exactes. Une méthode consiste à demander à un expert de décider et de vérifier chaque élément d'information, mais cela prend du temps et nécessite une expertise qui ne peut être partagée. Deuxièmement, nous pouvons utiliser des outils d'apprentissage automatique et d'intelligence artificielle pour automatiser l'identification des fausses nouvelles.

Les informations d'actualité en ligne incluent diverses données de format non structuré (telles que des documents, des vidéos et de l'audio), mais nous nous concentrerons ici sur les informations au format texte. Avec les progrès de l'apprentissage automatique et du traitement automatique du langage naturel , nous pouvons désormais reconnaître le caractère trompeur et faux d'un article ou d'une déclaration.

Plusieurs études et expérimentations sont menées pour détecter les fake news sur tous les supports.

Notre objectif principal de ce tutoriel est :

  • Explorez et analysez l'ensemble de données Fake News.
  • Construisez un classificateur capable de distinguer les fausses nouvelles avec autant de précision que possible.

Voici la table des matières :

  • introduction
  • Quelle est la taille de ce problème ?
  • La solution
  • Exploration des données
    • Répartition des cours
  • Nettoyage des données pour l'analyse
  • Analyse exploratoire des données
    • Nuage à un seul mot
    • Bigramme le plus fréquent (combinaison de deux mots)
    • Trigramme le plus fréquent (combinaison de trois mots)
  • Construire un classificateur en affinant le BERT
    • Préparation des données
    • Tokénisation de l'ensemble de données
    • Chargement et réglage fin du modèle
    • Évaluation du modèle
  • Annexe : Création d'un fichier de soumission pour Kaggle
  • Conclusion

Exploration des données

Dans ce travail, nous avons utilisé l'ensemble de données sur les fausses nouvelles de Kaggle pour classer les articles d'actualité non fiables comme fausses nouvelles. Nous disposons d'un jeu de données d'entraînement complet contenant les caractéristiques suivantes :

  • id: identifiant unique pour un article de presse
  • title: titre d'un article de presse
  • author: auteur de l'article de presse
  • text: texte de l'article ; pourrait être incomplet
  • label: une étiquette qui marque l'article comme potentiellement non fiable, notée 1 (non fiable ou faux) ou 0 (fiable).

Il s'agit d'un problème de classification binaire dans lequel nous devons prédire si une nouvelle particulière est fiable ou non.

Si vous avez un compte Kaggle, vous pouvez simplement télécharger l'ensemble de données à partir du site Web et extraire le fichier ZIP.

J'ai également téléchargé l'ensemble de données dans Google Drive, et vous pouvez l'obtenir ici , ou utiliser la gdownbibliothèque pour le télécharger automatiquement dans les blocs-notes Google Colab ou Jupyter :

$ pip install gdown
# download from Google Drive
$ gdown "https://drive.google.com/uc?id=178f_VkNxccNidap-5-uffXUW475pAuPy&confirm=t"
Downloading...
From: https://drive.google.com/uc?id=178f_VkNxccNidap-5-uffXUW475pAuPy&confirm=t
To: /content/fake-news.zip
100% 48.7M/48.7M [00:00<00:00, 74.6MB/s]

Décompressez les fichiers :

$ unzip fake-news.zip

Trois fichiers apparaîtront dans le répertoire de travail actuel : train.csv, test.csv, et submit.csv, que nous utiliserons train.csvdans la majeure partie du didacticiel.

Installation des dépendances requises :

$ pip install transformers nltk pandas numpy matplotlib seaborn wordcloud

Remarque : Si vous êtes dans un environnement local, assurez-vous d'installer PyTorch pour GPU, rendez-vous sur cette page pour une installation correcte.

Importons les bibliothèques essentielles pour l'analyse :

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

Les corpus et modules NLTK doivent être installés à l'aide du téléchargeur NLTK standard :

import nltk
nltk.download('stopwords')
nltk.download('wordnet')

L'ensemble de données sur les fausses nouvelles comprend les titres et le texte d'articles originaux et fictifs de divers auteurs. Importons notre jeu de données :

# load the dataset
news_d = pd.read_csv("train.csv")
print("Shape of News data:", news_d.shape)
print("News data columns", news_d.columns)

Sortir:

 Shape of News data: (20800, 5)
 News data columns Index(['id', 'title', 'author', 'text', 'label'], dtype='object')

Voici à quoi ressemble l'ensemble de données :

# by using df.head(), we can immediately familiarize ourselves with the dataset. 
news_d.head()

Sortir:

id	title	author	text	label
0	0	House Dem Aide: We Didn’t Even See Comey’s Let...	Darrell Lucus	House Dem Aide: We Didn’t Even See Comey’s Let...	1
1	1	FLYNN: Hillary Clinton, Big Woman on Campus - ...	Daniel J. Flynn	Ever get the feeling your life circles the rou...	0
2	2	Why the Truth Might Get You Fired	Consortiumnews.com	Why the Truth Might Get You Fired October 29, ...	1
3	3	15 Civilians Killed In Single US Airstrike Hav...	Jessica Purkiss	Videos 15 Civilians Killed In Single US Airstr...	1
4	4	Iranian woman jailed for fictional unpublished...	Howard Portnoy	Print \nAn Iranian woman has been sentenced to...	1

Nous avons 20 800 lignes, qui ont cinq colonnes. Voyons quelques statistiques de la textcolonne :

#Text Word startistics: min.mean, max and interquartile range

txt_length = news_d.text.str.split().str.len()
txt_length.describe()

Sortir:

count    20761.000000
mean       760.308126
std        869.525988
min          0.000000
25%        269.000000
50%        556.000000
75%       1052.000000
max      24234.000000
Name: text, dtype: float64

Statistiques pour la titlecolonne :

#Title statistics 

title_length = news_d.title.str.split().str.len()
title_length.describe()

Sortir:

count    20242.000000
mean        12.420709
std          4.098735
min          1.000000
25%         10.000000
50%         13.000000
75%         15.000000
max         72.000000
Name: title, dtype: float64

Les statistiques pour les ensembles d'entraînement et de test sont les suivantes :

  • L' textattribut a un nombre de mots plus élevé avec une moyenne de 760 mots et 75% ayant plus de 1000 mots.
  • L' titleattribut est une courte déclaration avec une moyenne de 12 mots, et 75% d'entre eux sont d'environ 15 mots.

Notre expérience porterait à la fois sur le texte et le titre.

Répartition des cours

Compter les parcelles pour les deux étiquettes :

sns.countplot(x="label", data=news_d);
print("1: Unreliable")
print("0: Reliable")
print("Distribution of labels:")
print(news_d.label.value_counts());

Sortir:

1: Unreliable
0: Reliable
Distribution of labels:
1    10413
0    10387
Name: label, dtype: int64

Distribution d'étiquettes

print(round(news_d.label.value_counts(normalize=True),2)*100);

Sortir:

1    50.0
0    50.0
Name: label, dtype: float64

Le nombre d'articles non fiables (faux ou 1) est de 10413, tandis que le nombre d'articles dignes de confiance (fiables ou 0) est de 10387. Près de 50% des articles sont faux. Par conséquent, la métrique de précision mesurera la performance de notre modèle lors de la construction d'un classificateur.

Nettoyage des données pour l'analyse

Dans cette section, nous allons nettoyer notre ensemble de données pour effectuer une analyse :

  • Supprimez les lignes et les colonnes inutilisées.
  • Effectuez une imputation de valeur nulle.
  • Supprimer les caractères spéciaux.
  • Supprimez les mots vides.
# Constants that are used to sanitize the datasets 

column_n = ['id', 'title', 'author', 'text', 'label']
remove_c = ['id','author']
categorical_features = []
target_col = ['label']
text_f = ['title', 'text']
# Clean Datasets
import nltk
from nltk.corpus import stopwords
import re
from nltk.stem.porter import PorterStemmer
from collections import Counter

ps = PorterStemmer()
wnl = nltk.stem.WordNetLemmatizer()

stop_words = stopwords.words('english')
stopwords_dict = Counter(stop_words)

# Removed unused clumns
def remove_unused_c(df,column_n=remove_c):
    df = df.drop(column_n,axis=1)
    return df

# Impute null values with None
def null_process(feature_df):
    for col in text_f:
        feature_df.loc[feature_df[col].isnull(), col] = "None"
    return feature_df

def clean_dataset(df):
    # remove unused column
    df = remove_unused_c(df)
    #impute null values
    df = null_process(df)
    return df

# Cleaning text from unused characters
def clean_text(text):
    text = str(text).replace(r'http[\w:/\.]+', ' ')  # removing urls
    text = str(text).replace(r'[^\.\w\s]', ' ')  # remove everything but characters and punctuation
    text = str(text).replace('[^a-zA-Z]', ' ')
    text = str(text).replace(r'\s\s+', ' ')
    text = text.lower().strip()
    #text = ' '.join(text)    
    return text

## Nltk Preprocessing include:
# Stop words, Stemming and Lemmetization
# For our project we use only Stop word removal
def nltk_preprocess(text):
    text = clean_text(text)
    wordlist = re.sub(r'[^\w\s]', '', text).split()
    #text = ' '.join([word for word in wordlist if word not in stopwords_dict])
    #text = [ps.stem(word) for word in wordlist if not word in stopwords_dict]
    text = ' '.join([wnl.lemmatize(word) for word in wordlist if word not in stopwords_dict])
    return  text

Dans le bloc de code ci-dessus :

  • Nous avons importé NLTK, qui est une plate-forme célèbre pour développer des applications Python qui interagissent avec le langage humain. Ensuite, nous importons repour regex.
  • Nous importons des mots vides à partir de nltk.corpus. Lorsque nous travaillons avec des mots, en particulier lorsque nous considérons la sémantique, nous devons parfois éliminer les mots courants qui n'ajoutent aucune signification significative à une déclaration, tels que "but", "can", "we", etc.
  • PorterStemmerest utilisé pour effectuer des mots radicaux avec NLTK. Les radicaux dépouillent les mots de leurs affixes morphologiques, laissant uniquement le radical du mot.
  • Nous importons WordNetLemmatizer()de la bibliothèque NLTK pour la lemmatisation. La lemmatisation est bien plus efficace que la radicalisation . Il va au-delà de la réduction des mots et évalue l'ensemble du lexique d'une langue pour appliquer une analyse morphologique aux mots, dans le but de supprimer simplement les extrémités flexionnelles et de renvoyer la forme de base ou de dictionnaire d'un mot, connue sous le nom de lemme.
  • stopwords.words('english')permettez-nous de regarder la liste de tous les mots vides en anglais pris en charge par NLTK.
  • remove_unused_c()La fonction est utilisée pour supprimer les colonnes inutilisées.
  • Nous imputons des valeurs nulles à Nonel'aide de la null_process()fonction.
  • A l'intérieur de la fonction clean_dataset(), nous appelons remove_unused_c()et null_process()fonctions. Cette fonction est responsable du nettoyage des données.
  • Pour nettoyer le texte des caractères inutilisés, nous avons créé la clean_text()fonction.
  • Pour le prétraitement, nous n'utiliserons que la suppression des mots vides. Nous avons créé la nltk_preprocess()fonction à cet effet.

Prétraitement de textet title:

# Perform data cleaning on train and test dataset by calling clean_dataset function
df = clean_dataset(news_d)
# apply preprocessing on text through apply method by calling the function nltk_preprocess
df["text"] = df.text.apply(nltk_preprocess)
# apply preprocessing on title through apply method by calling the function nltk_preprocess
df["title"] = df.title.apply(nltk_preprocess)
# Dataset after cleaning and preprocessing step
df.head()

Sortir:

title	text	label
0	house dem aide didnt even see comeys letter ja...	house dem aide didnt even see comeys letter ja...	1
1	flynn hillary clinton big woman campus breitbart	ever get feeling life circle roundabout rather...	0
2	truth might get fired	truth might get fired october 29 2016 tension ...	1
3	15 civilian killed single u airstrike identified	video 15 civilian killed single u airstrike id...	1
4	iranian woman jailed fictional unpublished sto...	print iranian woman sentenced six year prison ...	1

Analyse exploratoire des données

Dans cette section, nous effectuerons :

  • Analyse Univariée : C'est une analyse statistique du texte. Nous utiliserons un nuage de mots à cette fin. Un nuage de mots est une approche de visualisation des données textuelles où le terme le plus courant est présenté dans la taille de police la plus importante.
  • Analyse Bivariée : Bigramme et Trigramme seront utilisés ici. Selon Wikipedia : " un n-gramme est une séquence contiguë de n éléments d'un échantillon donné de texte ou de parole. Selon l'application, les éléments peuvent être des phonèmes, des syllabes, des lettres, des mots ou des paires de bases. Les n-grammes sont généralement collectées à partir d'un corpus textuel ou vocal ».

Nuage à un seul mot

Les mots les plus fréquents apparaissent en caractères gras et plus gros dans un nuage de mots. Cette section effectuera un nuage de mots pour tous les mots du jeu de données.

La fonction de la bibliothèque WordCloudwordcloud() sera utilisée, et la generate()est utilisée pour générer l'image du nuage de mots :

from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt

# initialize the word cloud
wordcloud = WordCloud( background_color='black', width=800, height=600)
# generate the word cloud by passing the corpus
text_cloud = wordcloud.generate(' '.join(df['text']))
# plotting the word cloud
plt.figure(figsize=(20,30))
plt.imshow(text_cloud)
plt.axis('off')
plt.show()

Sortir:

WordCloud pour toutes les fausses données de nouvelles

Nuage de mots pour les informations fiables uniquement :

true_n = ' '.join(df[df['label']==0]['text']) 
wc = wordcloud.generate(true_n)
plt.figure(figsize=(20,30))
plt.imshow(wc)
plt.axis('off')
plt.show()

Sortir:

Nuage de mots pour des nouvelles fiables

Nuage de mots pour les fake news uniquement :

fake_n = ' '.join(df[df['label']==1]['text'])
wc= wordcloud.generate(fake_n)
plt.figure(figsize=(20,30))
plt.imshow(wc)
plt.axis('off')
plt.show()

Sortir:

Nuage de mots pour les fausses nouvelles

Bigramme le plus fréquent (combinaison de deux mots)

Un N-gramme est une séquence de lettres ou de mots. Un unigramme de caractère est composé d'un seul caractère, tandis qu'un bigramme est composé d'une série de deux caractères. De même, les N-grammes de mots sont constitués d'une suite de n mots. Le mot "uni" est un 1-gramme (unigramme). La combinaison des mots "États-Unis" est un 2-gramme (bigramme), "new york city" est un 3-gramme.

Traçons le bigramme le plus courant sur les nouvelles fiables :

def plot_top_ngrams(corpus, title, ylabel, xlabel="Number of Occurences", n=2):
  """Utility function to plot top n-grams"""
  true_b = (pd.Series(nltk.ngrams(corpus.split(), n)).value_counts())[:20]
  true_b.sort_values().plot.barh(color='blue', width=.9, figsize=(12, 8))
  plt.title(title)
  plt.ylabel(ylabel)
  plt.xlabel(xlabel)
  plt.show()
plot_top_ngrams(true_n, 'Top 20 Frequently Occuring True news Bigrams', "Bigram", n=2)

Top des bigrammes sur les fake news

Le bigramme le plus courant sur les fake news :

plot_top_ngrams(fake_n, 'Top 20 Frequently Occuring Fake news Bigrams', "Bigram", n=2)

Top des bigrammes sur les fake news

Trigramme le plus fréquent (combinaison de trois mots)

Le trigramme le plus courant sur les informations fiables :

plot_top_ngrams(true_n, 'Top 20 Frequently Occuring True news Trigrams', "Trigrams", n=3)

Le trigramme le plus courant sur les fake news

Pour les fausses nouvelles maintenant :

plot_top_ngrams(fake_n, 'Top 20 Frequently Occuring Fake news Trigrams', "Trigrams", n=3)

Les trigrammes les plus courants sur les fausses nouvelles

Les tracés ci-dessus nous donnent quelques idées sur l'apparence des deux classes. Dans la section suivante, nous utiliserons la bibliothèque de transformateurs pour créer un détecteur de fausses nouvelles.

Construire un classificateur en affinant le BERT

Cette section récupèrera largement le code du tutoriel de réglage fin du BERT pour créer un classificateur de fausses nouvelles à l'aide de la bibliothèque de transformateurs. Ainsi, pour des informations plus détaillées, vous pouvez vous diriger vers le tutoriel d'origine .

Si vous n'avez pas installé de transformateurs, vous devez :

$ pip install transformers

Importons les bibliothèques nécessaires :

import torch
from transformers.file_utils import is_tf_available, is_torch_available, is_torch_tpu_available
from transformers import BertTokenizerFast, BertForSequenceClassification
from transformers import Trainer, TrainingArguments
import numpy as np
from sklearn.model_selection import train_test_split

import random

Nous voulons rendre nos résultats reproductibles même si nous redémarrons notre environnement :

def set_seed(seed: int):
    """
    Helper function for reproducible behavior to set the seed in ``random``, ``numpy``, ``torch`` and/or ``tf`` (if
    installed).

    Args:
        seed (:obj:`int`): The seed to set.
    """
    random.seed(seed)
    np.random.seed(seed)
    if is_torch_available():
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)
        # ^^ safe to call this function even if cuda is not available
    if is_tf_available():
        import tensorflow as tf

        tf.random.set_seed(seed)

set_seed(1)

Le modèle que nous allons utiliser est le bert-base-uncased:

# the model we gonna train, base uncased BERT
# check text classification models here: https://huggingface.co/models?filter=text-classification
model_name = "bert-base-uncased"
# max sequence length for each document/sentence sample
max_length = 512

Chargement du tokenizer :

# load the tokenizer
tokenizer = BertTokenizerFast.from_pretrained(model_name, do_lower_case=True)

Préparation des données

Nettoyons maintenant les NaNvaleurs des colonnes text, authoret :title

news_df = news_d[news_d['text'].notna()]
news_df = news_df[news_df["author"].notna()]
news_df = news_df[news_df["title"].notna()]

Ensuite, créez une fonction qui prend l'ensemble de données en tant que dataframe Pandas et renvoie les fractionnements de train/validation des textes et des étiquettes sous forme de listes :

def prepare_data(df, test_size=0.2, include_title=True, include_author=True):
  texts = []
  labels = []
  for i in range(len(df)):
    text = df["text"].iloc[i]
    label = df["label"].iloc[i]
    if include_title:
      text = df["title"].iloc[i] + " - " + text
    if include_author:
      text = df["author"].iloc[i] + " : " + text
    if text and label in [0, 1]:
      texts.append(text)
      labels.append(label)
  return train_test_split(texts, labels, test_size=test_size)

train_texts, valid_texts, train_labels, valid_labels = prepare_data(news_df)

La fonction ci-dessus prend l'ensemble de données dans un type de trame de données et les renvoie sous forme de listes divisées en ensembles d'apprentissage et de validation. Définir include_titlesur Truesignifie que nous ajoutons la titlecolonne à celle textque nous allons utiliser pour la formation, définir include_authorsur Truesignifie que nous ajoutons authorégalement la au texte.

Assurons-nous que les étiquettes et les textes ont la même longueur :

print(len(train_texts), len(train_labels))
print(len(valid_texts), len(valid_labels))

Sortir:

14628 14628
3657 3657

Tokénisation de l'ensemble de données

Utilisons le tokenizer BERT pour tokeniser notre jeu de données :

# tokenize the dataset, truncate when passed `max_length`, 
# and pad with 0's when less than `max_length`
train_encodings = tokenizer(train_texts, truncation=True, padding=True, max_length=max_length)
valid_encodings = tokenizer(valid_texts, truncation=True, padding=True, max_length=max_length)

Conversion des encodages en un jeu de données PyTorch :

class NewsGroupsDataset(torch.utils.data.Dataset):
    def __init__(self, encodings, labels):
        self.encodings = encodings
        self.labels = labels

    def __getitem__(self, idx):
        item = {k: torch.tensor(v[idx]) for k, v in self.encodings.items()}
        item["labels"] = torch.tensor([self.labels[idx]])
        return item

    def __len__(self):
        return len(self.labels)

# convert our tokenized data into a torch Dataset
train_dataset = NewsGroupsDataset(train_encodings, train_labels)
valid_dataset = NewsGroupsDataset(valid_encodings, valid_labels)

Chargement et réglage fin du modèle

Nous utiliserons BertForSequenceClassificationpour charger notre modèle de transformateur BERT :

# load the model
model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2)

Nous avons mis num_labelsà 2 puisqu'il s'agit d'une classification binaire. La fonction ci-dessous est un rappel pour calculer la précision à chaque étape de validation :

from sklearn.metrics import accuracy_score

def compute_metrics(pred):
  labels = pred.label_ids
  preds = pred.predictions.argmax(-1)
  # calculate accuracy using sklearn's function
  acc = accuracy_score(labels, preds)
  return {
      'accuracy': acc,
  }

Initialisons les paramètres d'entraînement :

training_args = TrainingArguments(
    output_dir='./results',          # output directory
    num_train_epochs=1,              # total number of training epochs
    per_device_train_batch_size=10,  # batch size per device during training
    per_device_eval_batch_size=20,   # batch size for evaluation
    warmup_steps=100,                # number of warmup steps for learning rate scheduler
    logging_dir='./logs',            # directory for storing logs
    load_best_model_at_end=True,     # load the best model when finished training (default metric is loss)
    # but you can specify `metric_for_best_model` argument to change to accuracy or other metric
    logging_steps=200,               # log & save weights each logging_steps
    save_steps=200,
    evaluation_strategy="steps",     # evaluate each `logging_steps`
)

J'ai réglé le per_device_train_batch_sizeà 10, mais vous devriez le régler aussi haut que votre GPU pourrait éventuellement s'adapter. En réglant le logging_stepset save_stepssur 200, cela signifie que nous allons effectuer une évaluation et enregistrer les poids du modèle à chaque étape de formation de 200.

Vous pouvez consulter  cette page  pour des informations plus détaillées sur les paramètres d'entraînement disponibles.

Instancions le formateur :

trainer = Trainer(
    model=model,                         # the instantiated Transformers model to be trained
    args=training_args,                  # training arguments, defined above
    train_dataset=train_dataset,         # training dataset
    eval_dataset=valid_dataset,          # evaluation dataset
    compute_metrics=compute_metrics,     # the callback that computes metrics of interest
)

Entraînement du modèle :

# train the model
trainer.train()

La formation prend quelques heures pour se terminer, en fonction de votre GPU. Si vous êtes sur la version gratuite de Colab, cela devrait prendre une heure avec NVIDIA Tesla K80. Voici la sortie :

***** Running training *****
  Num examples = 14628
  Num Epochs = 1
  Instantaneous batch size per device = 10
  Total train batch size (w. parallel, distributed & accumulation) = 10
  Gradient Accumulation steps = 1
  Total optimization steps = 1463
 [1463/1463 41:07, Epoch 1/1]
Step	Training Loss	Validation Loss	Accuracy
200		0.250800		0.100533		0.983867
400		0.027600		0.043009		0.993437
600		0.023400		0.017812		0.997539
800		0.014900		0.030269		0.994258
1000	0.022400		0.012961		0.998086
1200	0.009800		0.010561		0.998633
1400	0.007700		0.010300		0.998633
***** Running Evaluation *****
  Num examples = 3657
  Batch size = 20
Saving model checkpoint to ./results/checkpoint-200
Configuration saved in ./results/checkpoint-200/config.json
Model weights saved in ./results/checkpoint-200/pytorch_model.bin
<SNIPPED>
***** Running Evaluation *****
  Num examples = 3657
  Batch size = 20
Saving model checkpoint to ./results/checkpoint-1400
Configuration saved in ./results/checkpoint-1400/config.json
Model weights saved in ./results/checkpoint-1400/pytorch_model.bin

Training completed. Do not forget to share your model on huggingface.co/models =)

Loading best model from ./results/checkpoint-1400 (score: 0.010299865156412125).
TrainOutput(global_step=1463, training_loss=0.04888018785440506, metrics={'train_runtime': 2469.1722, 'train_samples_per_second': 5.924, 'train_steps_per_second': 0.593, 'total_flos': 3848788517806080.0, 'train_loss': 0.04888018785440506, 'epoch': 1.0})

Évaluation du modèle

Étant donné que load_best_model_at_endest réglé sur True, les meilleurs poids seront chargés une fois l'entraînement terminé. Évaluons-le avec notre ensemble de validation :

# evaluate the current model after training
trainer.evaluate()

Sortir:

***** Running Evaluation *****
  Num examples = 3657
  Batch size = 20
 [183/183 02:11]
{'epoch': 1.0,
 'eval_accuracy': 0.998632759092152,
 'eval_loss': 0.010299865156412125,
 'eval_runtime': 132.0374,
 'eval_samples_per_second': 27.697,
 'eval_steps_per_second': 1.386}

Enregistrement du modèle et du tokenizer :

# saving the fine tuned model & tokenizer
model_path = "fake-news-bert-base-uncased"
model.save_pretrained(model_path)
tokenizer.save_pretrained(model_path)

Un nouveau dossier contenant la configuration du modèle et les poids apparaîtra après l'exécution de la cellule ci-dessus. Si vous souhaitez effectuer une prédiction, vous utilisez simplement la from_pretrained()méthode que nous avons utilisée lorsque nous avons chargé le modèle, et vous êtes prêt à partir.

Ensuite, créons une fonction qui accepte le texte de l'article comme argument et retourne s'il est faux ou non :

def get_prediction(text, convert_to_label=False):
    # prepare our text into tokenized sequence
    inputs = tokenizer(text, padding=True, truncation=True, max_length=max_length, return_tensors="pt").to("cuda")
    # perform inference to our model
    outputs = model(**inputs)
    # get output probabilities by doing softmax
    probs = outputs[0].softmax(1)
    # executing argmax function to get the candidate label
    d = {
        0: "reliable",
        1: "fake"
    }
    if convert_to_label:
      return d[int(probs.argmax())]
    else:
      return int(probs.argmax())

J'ai pris un exemple à partir test.csvduquel le modèle n'a jamais vu effectuer d'inférence, je l'ai vérifié, et c'est un article réel du New York Times :

real_news = """
Tim Tebow Will Attempt Another Comeback, This Time in Baseball - The New York Times",Daniel Victor,"If at first you don’t succeed, try a different sport. Tim Tebow, who was a Heisman   quarterback at the University of Florida but was unable to hold an N. F. L. job, is pursuing a career in Major League Baseball. <SNIPPED>
"""

Le texte original se trouve dans l'environnement Colab si vous souhaitez le copier, car il s'agit d'un article complet. Passons-le au modèle et voyons les résultats :

get_prediction(real_news, convert_to_label=True)

Sortir:

reliable

Annexe : Création d'un fichier de soumission pour Kaggle

Dans cette section, nous allons prédire tous les articles dans le test.csvpour créer un dossier de soumission pour voir notre justesse dans le jeu de test sur le concours Kaggle :

# read the test set
test_df = pd.read_csv("test.csv")
# make a copy of the testing set
new_df = test_df.copy()
# add a new column that contains the author, title and article content
new_df["new_text"] = new_df["author"].astype(str) + " : " + new_df["title"].astype(str) + " - " + new_df["text"].astype(str)
# get the prediction of all the test set
new_df["label"] = new_df["new_text"].apply(get_prediction)
# make the submission file
final_df = new_df[["id", "label"]]
final_df.to_csv("submit_final.csv", index=False)

Après avoir concaténé l'auteur, le titre et le texte de l'article, nous passons la get_prediction()fonction à la nouvelle colonne pour remplir la labelcolonne, nous utilisons ensuite la to_csv()méthode pour créer le fichier de soumission pour Kaggle. Voici mon score de soumission :

Note de soumission

Nous avons obtenu une précision de 99,78 % et 100 % sur les classements privés et publics. C'est génial!

Conclusion

Très bien, nous avons terminé avec le tutoriel. Vous pouvez consulter cette page pour voir divers paramètres d'entraînement que vous pouvez modifier.

Si vous avez un ensemble de données de fausses nouvelles personnalisé pour un réglage fin, il vous suffit de transmettre une liste d'échantillons au tokenizer comme nous l'avons fait, vous ne modifierez plus aucun autre code par la suite.

Vérifiez le code complet ici , ou l'environnement Colab ici .

So erstellen Sie einen Fake-News-Detektor in Python

Erkennung gefälschter Nachrichten in Python

Untersuchen des Fake-News-Datensatzes, Durchführen von Datenanalysen wie Wortwolken und Ngrams und Feinabstimmen des BERT-Transformators, um einen Fake-News-Detektor in Python mithilfe der Transformer-Bibliothek zu erstellen.

Fake News sind die absichtliche Verbreitung falscher oder irreführender Behauptungen als Nachrichten, bei denen die Aussagen absichtlich irreführend sind.

Zeitungen, Boulevardzeitungen und Zeitschriften wurden durch digitale Nachrichtenplattformen, Blogs, Social-Media-Feeds und eine Vielzahl mobiler Nachrichtenanwendungen ersetzt. Nachrichtenorganisationen profitierten von der zunehmenden Nutzung sozialer Medien und mobiler Plattformen, indem sie ihren Abonnenten minutenaktuelle Informationen lieferten.

Die Verbraucher haben jetzt sofortigen Zugriff auf die neuesten Nachrichten. Diese digitalen Medienplattformen haben aufgrund ihrer einfachen Anbindung an den Rest der Welt an Bedeutung gewonnen und ermöglichen es den Benutzern, Ideen zu diskutieren und auszutauschen und Themen wie Demokratie, Bildung, Gesundheit, Forschung und Geschichte zu debattieren. Gefälschte Nachrichten auf digitalen Plattformen werden immer beliebter und werden für Profitzwecke wie politische und finanzielle Gewinne verwendet.

Wie groß ist dieses Problem?

Da das Internet, soziale Medien und digitale Plattformen weit verbreitet sind, kann jeder ungenaue und voreingenommene Informationen verbreiten. Die Verbreitung von Fake News lässt sich kaum verhindern. Es gibt einen enormen Anstieg bei der Verbreitung falscher Nachrichten, die nicht auf einen Sektor wie Politik beschränkt sind, sondern Sport, Gesundheit, Geschichte, Unterhaltung sowie Wissenschaft und Forschung umfassen.

Die Lösung

Es ist wichtig, falsche und richtige Nachrichten zu erkennen und zu unterscheiden. Eine Methode besteht darin, einen Experten entscheiden zu lassen und alle Informationen auf Fakten zu überprüfen, aber dies kostet Zeit und erfordert Fachwissen, das nicht geteilt werden kann. Zweitens können wir Tools für maschinelles Lernen und künstliche Intelligenz verwenden, um die Identifizierung von gefälschten Nachrichten zu automatisieren.

Online-Nachrichteninformationen umfassen verschiedene unstrukturierte Formatdaten (wie Dokumente, Videos und Audio), aber wir konzentrieren uns hier auf Nachrichten im Textformat. Mit dem Fortschritt des maschinellen Lernens und der Verarbeitung natürlicher Sprache können wir jetzt den irreführenden und falschen Charakter eines Artikels oder einer Aussage erkennen.

Mehrere Studien und Experimente werden durchgeführt, um Fake News in allen Medien aufzudecken.

Unser Hauptziel dieses Tutorials ist:

  • Untersuchen und analysieren Sie den Fake-News-Datensatz.
  • Erstellen Sie einen Klassifikator, der gefälschte Nachrichten so genau wie möglich unterscheiden kann.

Hier das Inhaltsverzeichnis:

  • Einführung
  • Wie groß ist dieses Problem?
  • Die Lösung
  • Datenexploration
    • Verteilung der Klassen
  • Datenbereinigung für die Analyse
  • Explorative Datenanalyse
    • Ein-Wort-Wolke
    • Häufigstes Bigram (Zwei-Wort-Kombination)
    • Häufigstes Trigramm (Drei-Wort-Kombination)
  • Aufbau eines Klassifikators durch Feinabstimmung von BERT
    • Datenaufbereitung
    • Tokenisieren des Datensatzes
    • Laden und Feintuning des Modells
    • Modellbewertung
  • Anhang: Erstellen einer Übermittlungsdatei für Kaggle
  • Fazit

Datenexploration

In dieser Arbeit haben wir den Fake-News-Datensatz von Kaggle verwendet , um nicht vertrauenswürdige Nachrichtenartikel als Fake News zu klassifizieren. Wir verfügen über einen vollständigen Trainingsdatensatz mit den folgenden Merkmalen:

  • id: eindeutige ID für einen Nachrichtenartikel
  • title: Titel eines Nachrichtenartikels
  • author: Autor des Nachrichtenartikels
  • text: Text des Artikels; könnte unvollständig sein
  • label: ein Etikett, das den Artikel als potenziell unzuverlässig markiert, gekennzeichnet durch 1 (unzuverlässig oder gefälscht) oder 0 (zuverlässig).

Es ist ein binäres Klassifizierungsproblem, bei dem wir vorhersagen müssen, ob eine bestimmte Nachricht zuverlässig ist oder nicht.

Wenn Sie ein Kaggle-Konto haben, können Sie den Datensatz einfach von der dortigen Website herunterladen und die ZIP-Datei entpacken.

Ich habe den Datensatz auch in Google Drive hochgeladen, und Sie können ihn hier herunterladen oder die gdownBibliothek verwenden, um ihn automatisch in Google Colab- oder Jupyter-Notebooks herunterzuladen:

$ pip install gdown
# download from Google Drive
$ gdown "https://drive.google.com/uc?id=178f_VkNxccNidap-5-uffXUW475pAuPy&confirm=t"
Downloading...
From: https://drive.google.com/uc?id=178f_VkNxccNidap-5-uffXUW475pAuPy&confirm=t
To: /content/fake-news.zip
100% 48.7M/48.7M [00:00<00:00, 74.6MB/s]

Entpacken der Dateien:

$ unzip fake-news.zip

Im aktuellen Arbeitsverzeichnis werden drei Dateien angezeigt: train.csv, test.csv, und submit.csv, die wir train.csvim Großteil des Tutorials verwenden werden.

Installieren der erforderlichen Abhängigkeiten:

$ pip install transformers nltk pandas numpy matplotlib seaborn wordcloud

Hinweis: Wenn Sie sich in einer lokalen Umgebung befinden, stellen Sie sicher, dass Sie PyTorch für GPU installieren, gehen Sie zu dieser Seite für eine ordnungsgemäße Installation.

Lassen Sie uns die wesentlichen Bibliotheken für die Analyse importieren:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

Die NLTK-Korpora und -Module müssen mit dem standardmäßigen NLTK-Downloader installiert werden:

import nltk
nltk.download('stopwords')
nltk.download('wordnet')

Der Fake-News-Datensatz umfasst Original- und fiktive Artikeltitel und -texte verschiedener Autoren. Lassen Sie uns unseren Datensatz importieren:

# load the dataset
news_d = pd.read_csv("train.csv")
print("Shape of News data:", news_d.shape)
print("News data columns", news_d.columns)

Ausgabe:

 Shape of News data: (20800, 5)
 News data columns Index(['id', 'title', 'author', 'text', 'label'], dtype='object')

So sieht der Datensatz aus:

# by using df.head(), we can immediately familiarize ourselves with the dataset. 
news_d.head()

Ausgabe:

id	title	author	text	label
0	0	House Dem Aide: We Didn’t Even See Comey’s Let...	Darrell Lucus	House Dem Aide: We Didn’t Even See Comey’s Let...	1
1	1	FLYNN: Hillary Clinton, Big Woman on Campus - ...	Daniel J. Flynn	Ever get the feeling your life circles the rou...	0
2	2	Why the Truth Might Get You Fired	Consortiumnews.com	Why the Truth Might Get You Fired October 29, ...	1
3	3	15 Civilians Killed In Single US Airstrike Hav...	Jessica Purkiss	Videos 15 Civilians Killed In Single US Airstr...	1
4	4	Iranian woman jailed for fictional unpublished...	Howard Portnoy	Print \nAn Iranian woman has been sentenced to...	1

Wir haben 20.800 Zeilen, die fünf Spalten haben. Sehen wir uns einige Statistiken der textSpalte an:

#Text Word startistics: min.mean, max and interquartile range

txt_length = news_d.text.str.split().str.len()
txt_length.describe()

Ausgabe:

count    20761.000000
mean       760.308126
std        869.525988
min          0.000000
25%        269.000000
50%        556.000000
75%       1052.000000
max      24234.000000
Name: text, dtype: float64

Statistiken für die titleSpalte:

#Title statistics 

title_length = news_d.title.str.split().str.len()
title_length.describe()

Ausgabe:

count    20242.000000
mean        12.420709
std          4.098735
min          1.000000
25%         10.000000
50%         13.000000
75%         15.000000
max         72.000000
Name: title, dtype: float64

Die Statistiken für die Trainings- und Testsätze lauten wie folgt:

  • Das textAttribut hat eine höhere Wortzahl mit durchschnittlich 760 Wörtern und 75 % mit mehr als 1000 Wörtern.
  • Das titleAttribut ist eine kurze Aussage mit durchschnittlich 12 Wörtern, und 75 % davon sind ungefähr 15 Wörter.

Unser Experiment wäre mit Text und Titel zusammen.

Verteilung der Klassen

Zählplots für beide Etiketten:

sns.countplot(x="label", data=news_d);
print("1: Unreliable")
print("0: Reliable")
print("Distribution of labels:")
print(news_d.label.value_counts());

Ausgabe:

1: Unreliable
0: Reliable
Distribution of labels:
1    10413
0    10387
Name: label, dtype: int64

Verteilung von Etiketten

print(round(news_d.label.value_counts(normalize=True),2)*100);

Ausgabe:

1    50.0
0    50.0
Name: label, dtype: float64

Die Anzahl der nicht vertrauenswürdigen Artikel (gefälscht oder 1) beträgt 10413, während die Anzahl der vertrauenswürdigen Artikel (zuverlässig oder 0) 10387 beträgt. Fast 50 % der Artikel sind gefälscht. Daher misst die Genauigkeitsmetrik, wie gut unser Modell beim Erstellen eines Klassifikators abschneidet.

Datenbereinigung für die Analyse

In diesem Abschnitt werden wir unseren Datensatz bereinigen, um einige Analysen durchzuführen:

  • Löschen Sie nicht verwendete Zeilen und Spalten.
  • Führen Sie eine Nullwertimputation durch.
  • Sonderzeichen entfernen.
  • Stoppwörter entfernen.
# Constants that are used to sanitize the datasets 

column_n = ['id', 'title', 'author', 'text', 'label']
remove_c = ['id','author']
categorical_features = []
target_col = ['label']
text_f = ['title', 'text']
# Clean Datasets
import nltk
from nltk.corpus import stopwords
import re
from nltk.stem.porter import PorterStemmer
from collections import Counter

ps = PorterStemmer()
wnl = nltk.stem.WordNetLemmatizer()

stop_words = stopwords.words('english')
stopwords_dict = Counter(stop_words)

# Removed unused clumns
def remove_unused_c(df,column_n=remove_c):
    df = df.drop(column_n,axis=1)
    return df

# Impute null values with None
def null_process(feature_df):
    for col in text_f:
        feature_df.loc[feature_df[col].isnull(), col] = "None"
    return feature_df

def clean_dataset(df):
    # remove unused column
    df = remove_unused_c(df)
    #impute null values
    df = null_process(df)
    return df

# Cleaning text from unused characters
def clean_text(text):
    text = str(text).replace(r'http[\w:/\.]+', ' ')  # removing urls
    text = str(text).replace(r'[^\.\w\s]', ' ')  # remove everything but characters and punctuation
    text = str(text).replace('[^a-zA-Z]', ' ')
    text = str(text).replace(r'\s\s+', ' ')
    text = text.lower().strip()
    #text = ' '.join(text)    
    return text

## Nltk Preprocessing include:
# Stop words, Stemming and Lemmetization
# For our project we use only Stop word removal
def nltk_preprocess(text):
    text = clean_text(text)
    wordlist = re.sub(r'[^\w\s]', '', text).split()
    #text = ' '.join([word for word in wordlist if word not in stopwords_dict])
    #text = [ps.stem(word) for word in wordlist if not word in stopwords_dict]
    text = ' '.join([wnl.lemmatize(word) for word in wordlist if word not in stopwords_dict])
    return  text

Im obigen Codeblock:

  • Wir haben NLTK importiert, eine berühmte Plattform für die Entwicklung von Python-Anwendungen, die mit der menschlichen Sprache interagieren. Als nächstes importieren wir refür Regex.
  • Wir importieren Stoppwörter aus nltk.corpus. Bei der Arbeit mit Wörtern, insbesondere bei der Betrachtung der Semantik, müssen wir manchmal gebräuchliche Wörter eliminieren, die einer Aussage keine signifikante Bedeutung hinzufügen, wie z. B. "but", "can", "we", usw.
  • PorterStemmerwird verwendet, um Wortstämme mit NLTK auszuführen. Stemmer entfernen Wörter ihrer morphologischen Affixe und lassen nur den Wortstamm übrig.
  • Wir importieren WordNetLemmatizer()aus der NLTK-Bibliothek zur Lemmatisierung. Lemmatisierung ist viel effektiver als Stemmung . Es geht über die Wortreduktion hinaus und wertet das gesamte Lexikon einer Sprache aus, um eine morphologische Analyse auf Wörter anzuwenden, mit dem Ziel, nur Flexionsenden zu entfernen und die Basis- oder Wörterbuchform eines Wortes zurückzugeben, die als Lemma bekannt ist.
  • stopwords.words('english')Lassen Sie uns einen Blick auf die Liste aller englischen Stoppwörter werfen, die von NLTK unterstützt werden.
  • remove_unused_c()Funktion wird verwendet, um die unbenutzten Spalten zu entfernen.
  • Wir imputieren Nullwerte mit Noneder Verwendung der null_process()Funktion.
  • Innerhalb der Funktion clean_dataset()rufen wir remove_unused_c()und null_process()Funktionen auf. Diese Funktion ist für die Datenbereinigung zuständig.
  • Um Text von ungenutzten Zeichen zu bereinigen, haben wir die clean_text()Funktion erstellt.
  • Für die Vorverarbeitung verwenden wir nur die Entfernung von Stoppwörtern. Zu diesem Zweck haben wir die nltk_preprocess()Funktion erstellt.

Vorverarbeitung der textund title:

# Perform data cleaning on train and test dataset by calling clean_dataset function
df = clean_dataset(news_d)
# apply preprocessing on text through apply method by calling the function nltk_preprocess
df["text"] = df.text.apply(nltk_preprocess)
# apply preprocessing on title through apply method by calling the function nltk_preprocess
df["title"] = df.title.apply(nltk_preprocess)
# Dataset after cleaning and preprocessing step
df.head()

Ausgabe:

title	text	label
0	house dem aide didnt even see comeys letter ja...	house dem aide didnt even see comeys letter ja...	1
1	flynn hillary clinton big woman campus breitbart	ever get feeling life circle roundabout rather...	0
2	truth might get fired	truth might get fired october 29 2016 tension ...	1
3	15 civilian killed single u airstrike identified	video 15 civilian killed single u airstrike id...	1
4	iranian woman jailed fictional unpublished sto...	print iranian woman sentenced six year prison ...	1

Explorative Datenanalyse

In diesem Abschnitt führen wir Folgendes durch:

  • Univariate Analyse : Es ist eine statistische Analyse des Textes. Wir werden zu diesem Zweck die Wortwolke verwenden. Eine Wortwolke ist ein Visualisierungsansatz für Textdaten, bei dem der häufigste Begriff in der größten Schriftgröße dargestellt wird.
  • Bivariate Analyse : Hier werden Bigramm und Trigramm verwendet. Laut Wikipedia: „ Ein N-Gramm ist eine zusammenhängende Folge von n Elementen aus einem gegebenen Text- oder Sprachmuster. Je nach Anwendung können die Elemente Phoneme, Silben, Buchstaben, Wörter oder Basenpaare sein. Die N-Gramme werden typischerweise aus einem Text- oder Sprachkorpus gesammelt".

Ein-Wort-Wolke

Die häufigsten Wörter erscheinen fett und größer in einer Wortwolke. In diesem Abschnitt wird eine Wortwolke für alle Wörter im Datensatz erstellt.

Die Funktion der WordCloud - Bibliothek wordcloud()wird verwendet, und die generate()wird zum Generieren des Wortwolkenbildes verwendet:

from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt

# initialize the word cloud
wordcloud = WordCloud( background_color='black', width=800, height=600)
# generate the word cloud by passing the corpus
text_cloud = wordcloud.generate(' '.join(df['text']))
# plotting the word cloud
plt.figure(figsize=(20,30))
plt.imshow(text_cloud)
plt.axis('off')
plt.show()

Ausgabe:

WordCloud für die gesamten Fake-News-Daten

Wortwolke nur für zuverlässige Nachrichten:

true_n = ' '.join(df[df['label']==0]['text']) 
wc = wordcloud.generate(true_n)
plt.figure(figsize=(20,30))
plt.imshow(wc)
plt.axis('off')
plt.show()

Ausgabe:

Wortwolke für zuverlässige Nachrichten

Wortwolke nur für Fake News:

fake_n = ' '.join(df[df['label']==1]['text'])
wc= wordcloud.generate(fake_n)
plt.figure(figsize=(20,30))
plt.imshow(wc)
plt.axis('off')
plt.show()

Ausgabe:

Wortwolke für gefälschte Nachrichten

Häufigstes Bigram (Zwei-Wort-Kombination)

Ein N-Gramm ist eine Folge von Buchstaben oder Wörtern. Ein Zeichen-Unigramm besteht aus einem einzelnen Zeichen, während ein Bigramm aus einer Reihe von zwei Zeichen besteht. In ähnlicher Weise bestehen Wort-N-Gramme aus einer Reihe von n Wörtern. Das Wort "united" ist ein 1-Gramm (Unigram). Die Kombination der Wörter "United State" ist ein 2-Gramm (Bigramm), "New York City" ist ein 3-Gramm.

Lassen Sie uns das häufigste Bigramm in den zuverlässigen Nachrichten darstellen:

def plot_top_ngrams(corpus, title, ylabel, xlabel="Number of Occurences", n=2):
  """Utility function to plot top n-grams"""
  true_b = (pd.Series(nltk.ngrams(corpus.split(), n)).value_counts())[:20]
  true_b.sort_values().plot.barh(color='blue', width=.9, figsize=(12, 8))
  plt.title(title)
  plt.ylabel(ylabel)
  plt.xlabel(xlabel)
  plt.show()
plot_top_ngrams(true_n, 'Top 20 Frequently Occuring True news Bigrams', "Bigram", n=2)

Top-Bigramme zu Fake News

Das häufigste Bigramm in den Fake News:

plot_top_ngrams(fake_n, 'Top 20 Frequently Occuring Fake news Bigrams', "Bigram", n=2)

Top-Bigramme zu Fake News

Häufigstes Trigramm (Drei-Wort-Kombination)

Das häufigste Trigramm bei zuverlässigen Nachrichten:

plot_top_ngrams(true_n, 'Top 20 Frequently Occuring True news Trigrams', "Trigrams", n=3)

Das häufigste Trigramm auf Fake News

Für Fake News jetzt:

plot_top_ngrams(fake_n, 'Top 20 Frequently Occuring Fake news Trigrams', "Trigrams", n=3)

Die häufigsten Trigramme auf Fake News

Die obigen Diagramme geben uns einige Ideen, wie beide Klassen aussehen. Im nächsten Abschnitt verwenden wir die Transformers-Bibliothek , um einen Detektor für gefälschte Nachrichten zu erstellen.

Aufbau eines Klassifikators durch Feinabstimmung von BERT

In diesem Abschnitt wird ausgiebig Code aus dem BERT-Tutorial zur Feinabstimmung entnommen, um mithilfe der Transformers-Bibliothek einen Klassifikator für gefälschte Nachrichten zu erstellen. Für detailliertere Informationen können Sie also zum Original-Tutorial gehen .

Wenn Sie keine Transformatoren installiert haben, müssen Sie:

$ pip install transformers

Lassen Sie uns die erforderlichen Bibliotheken importieren:

import torch
from transformers.file_utils import is_tf_available, is_torch_available, is_torch_tpu_available
from transformers import BertTokenizerFast, BertForSequenceClassification
from transformers import Trainer, TrainingArguments
import numpy as np
from sklearn.model_selection import train_test_split

import random

Wir wollen unsere Ergebnisse reproduzierbar machen, auch wenn wir unsere Umgebung neu starten:

def set_seed(seed: int):
    """
    Helper function for reproducible behavior to set the seed in ``random``, ``numpy``, ``torch`` and/or ``tf`` (if
    installed).

    Args:
        seed (:obj:`int`): The seed to set.
    """
    random.seed(seed)
    np.random.seed(seed)
    if is_torch_available():
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)
        # ^^ safe to call this function even if cuda is not available
    if is_tf_available():
        import tensorflow as tf

        tf.random.set_seed(seed)

set_seed(1)

Das Modell, das wir verwenden werden, ist das bert-base-uncased:

# the model we gonna train, base uncased BERT
# check text classification models here: https://huggingface.co/models?filter=text-classification
model_name = "bert-base-uncased"
# max sequence length for each document/sentence sample
max_length = 512

Tokenizer laden:

# load the tokenizer
tokenizer = BertTokenizerFast.from_pretrained(model_name, do_lower_case=True)

Datenaufbereitung

Lassen Sie uns nun NaNWerte aus den Spalten text, authorund bereinigen:title

news_df = news_d[news_d['text'].notna()]
news_df = news_df[news_df["author"].notna()]
news_df = news_df[news_df["title"].notna()]

Erstellen Sie als Nächstes eine Funktion, die den Datensatz als Pandas-Datenrahmen nimmt und die Trainings-/Validierungsaufteilungen von Texten und Beschriftungen als Listen zurückgibt:

def prepare_data(df, test_size=0.2, include_title=True, include_author=True):
  texts = []
  labels = []
  for i in range(len(df)):
    text = df["text"].iloc[i]
    label = df["label"].iloc[i]
    if include_title:
      text = df["title"].iloc[i] + " - " + text
    if include_author:
      text = df["author"].iloc[i] + " : " + text
    if text and label in [0, 1]:
      texts.append(text)
      labels.append(label)
  return train_test_split(texts, labels, test_size=test_size)

train_texts, valid_texts, train_labels, valid_labels = prepare_data(news_df)

Die obige Funktion nimmt den Datensatz in einem Datenrahmentyp und gibt sie als Listen zurück, die in Trainings- und Validierungssätze aufgeteilt sind. Die Einstellung include_titleauf Truebedeutet, dass wir die titleSpalte zu dem hinzufügen, die textwir für das Training verwenden werden, die Einstellung include_authorauf bedeutet, dass wir auch die Spalte zum Text Truehinzufügen .author

Stellen wir sicher, dass die Beschriftungen und Texte die gleiche Länge haben:

print(len(train_texts), len(train_labels))
print(len(valid_texts), len(valid_labels))

Ausgabe:

14628 14628
3657 3657

Tokenisieren des Datensatzes

Verwenden wir den BERT-Tokenizer, um unseren Datensatz zu tokenisieren:

# tokenize the dataset, truncate when passed `max_length`, 
# and pad with 0's when less than `max_length`
train_encodings = tokenizer(train_texts, truncation=True, padding=True, max_length=max_length)
valid_encodings = tokenizer(valid_texts, truncation=True, padding=True, max_length=max_length)

Konvertieren der Kodierungen in einen PyTorch-Datensatz:

class NewsGroupsDataset(torch.utils.data.Dataset):
    def __init__(self, encodings, labels):
        self.encodings = encodings
        self.labels = labels

    def __getitem__(self, idx):
        item = {k: torch.tensor(v[idx]) for k, v in self.encodings.items()}
        item["labels"] = torch.tensor([self.labels[idx]])
        return item

    def __len__(self):
        return len(self.labels)

# convert our tokenized data into a torch Dataset
train_dataset = NewsGroupsDataset(train_encodings, train_labels)
valid_dataset = NewsGroupsDataset(valid_encodings, valid_labels)

Laden und Feintuning des Modells

Wir werden verwenden BertForSequenceClassification, um unser BERT-Transformatormodell zu laden:

# load the model
model = BertForSequenceClassification.from_pretrained(model_name, num_labels=2)

Wir setzen num_labelsauf 2, da es sich um eine binäre Klassifikation handelt. Die folgende Funktion ist ein Rückruf, um die Genauigkeit für jeden Validierungsschritt zu berechnen:

from sklearn.metrics import accuracy_score

def compute_metrics(pred):
  labels = pred.label_ids
  preds = pred.predictions.argmax(-1)
  # calculate accuracy using sklearn's function
  acc = accuracy_score(labels, preds)
  return {
      'accuracy': acc,
  }

Lassen Sie uns die Trainingsparameter initialisieren:

training_args = TrainingArguments(
    output_dir='./results',          # output directory
    num_train_epochs=1,              # total number of training epochs
    per_device_train_batch_size=10,  # batch size per device during training
    per_device_eval_batch_size=20,   # batch size for evaluation
    warmup_steps=100,                # number of warmup steps for learning rate scheduler
    logging_dir='./logs',            # directory for storing logs
    load_best_model_at_end=True,     # load the best model when finished training (default metric is loss)
    # but you can specify `metric_for_best_model` argument to change to accuracy or other metric
    logging_steps=200,               # log & save weights each logging_steps
    save_steps=200,
    evaluation_strategy="steps",     # evaluate each `logging_steps`
)

Ich habe den per_device_train_batch_sizeauf 10 eingestellt, aber Sie sollten ihn so hoch einstellen, wie Ihre GPU möglicherweise passen könnte. Setzen Sie logging_stepsund save_stepsauf 200, was bedeutet, dass wir eine Bewertung durchführen und die Modellgewichte bei jedem 200-Trainingsschritt speichern.

Auf dieser Seite finden Sie   detailliertere Informationen zu den verfügbaren Trainingsparametern.

Lassen Sie uns den Trainer instanziieren:

trainer = Trainer(
    model=model,                         # the instantiated Transformers model to be trained
    args=training_args,                  # training arguments, defined above
    train_dataset=train_dataset,         # training dataset
    eval_dataset=valid_dataset,          # evaluation dataset
    compute_metrics=compute_metrics,     # the callback that computes metrics of interest
)

Training des Modells:

# train the model
trainer.train()

Das Training dauert je nach GPU einige Stunden. Wenn Sie die kostenlose Version von Colab verwenden, sollte es mit NVIDIA Tesla K80 eine Stunde dauern. Hier ist die Ausgabe:

***** Running training *****
  Num examples = 14628
  Num Epochs = 1
  Instantaneous batch size per device = 10
  Total train batch size (w. parallel, distributed & accumulation) = 10
  Gradient Accumulation steps = 1
  Total optimization steps = 1463
 [1463/1463 41:07, Epoch 1/1]
Step	Training Loss	Validation Loss	Accuracy
200		0.250800		0.100533		0.983867
400		0.027600		0.043009		0.993437
600		0.023400		0.017812		0.997539
800		0.014900		0.030269		0.994258
1000	0.022400		0.012961		0.998086
1200	0.009800		0.010561		0.998633
1400	0.007700		0.010300		0.998633
***** Running Evaluation *****
  Num examples = 3657
  Batch size = 20
Saving model checkpoint to ./results/checkpoint-200
Configuration saved in ./results/checkpoint-200/config.json
Model weights saved in ./results/checkpoint-200/pytorch_model.bin
<SNIPPED>
***** Running Evaluation *****
  Num examples = 3657
  Batch size = 20
Saving model checkpoint to ./results/checkpoint-1400
Configuration saved in ./results/checkpoint-1400/config.json
Model weights saved in ./results/checkpoint-1400/pytorch_model.bin

Training completed. Do not forget to share your model on huggingface.co/models =)

Loading best model from ./results/checkpoint-1400 (score: 0.010299865156412125).
TrainOutput(global_step=1463, training_loss=0.04888018785440506, metrics={'train_runtime': 2469.1722, 'train_samples_per_second': 5.924, 'train_steps_per_second': 0.593, 'total_flos': 3848788517806080.0, 'train_loss': 0.04888018785440506, 'epoch': 1.0})

Modellbewertung

Da load_best_model_at_endauf eingestellt ist, Truewerden nach Abschluss des Trainings die besten Gewichte geladen. Lassen Sie es uns mit unserem Validierungsset auswerten:

# evaluate the current model after training
trainer.evaluate()

Ausgabe:

***** Running Evaluation *****
  Num examples = 3657
  Batch size = 20
 [183/183 02:11]
{'epoch': 1.0,
 'eval_accuracy': 0.998632759092152,
 'eval_loss': 0.010299865156412125,
 'eval_runtime': 132.0374,
 'eval_samples_per_second': 27.697,
 'eval_steps_per_second': 1.386}

Speichern des Modells und des Tokenizers:

# saving the fine tuned model & tokenizer
model_path = "fake-news-bert-base-uncased"
model.save_pretrained(model_path)
tokenizer.save_pretrained(model_path)

Nach dem Ausführen der obigen Zelle wird ein neuer Ordner mit der Modellkonfiguration und den Gewichten angezeigt. Wenn Sie eine Vorhersage durchführen möchten, verwenden Sie einfach die from_pretrained()Methode, die wir beim Laden des Modells verwendet haben, und Sie können loslegen.

Als nächstes erstellen wir eine Funktion, die den Artikeltext als Argument akzeptiert und zurückgibt, ob er gefälscht ist oder nicht:

def get_prediction(text, convert_to_label=False):
    # prepare our text into tokenized sequence
    inputs = tokenizer(text, padding=True, truncation=True, max_length=max_length, return_tensors="pt").to("cuda")
    # perform inference to our model
    outputs = model(**inputs)
    # get output probabilities by doing softmax
    probs = outputs[0].softmax(1)
    # executing argmax function to get the candidate label
    d = {
        0: "reliable",
        1: "fake"
    }
    if convert_to_label:
      return d[int(probs.argmax())]
    else:
      return int(probs.argmax())

Ich habe ein Beispiel dafür genommen test.csv, dass das Modell nie eine Inferenz durchgeführt hat, ich habe es überprüft, und es ist ein aktueller Artikel aus der New York Times:

real_news = """
Tim Tebow Will Attempt Another Comeback, This Time in Baseball - The New York Times",Daniel Victor,"If at first you don’t succeed, try a different sport. Tim Tebow, who was a Heisman   quarterback at the University of Florida but was unable to hold an N. F. L. job, is pursuing a career in Major League Baseball. <SNIPPED>
"""

Der Originaltext befindet sich in der Colab-Umgebung , wenn Sie ihn kopieren möchten, da es sich um einen vollständigen Artikel handelt. Übergeben wir es an das Modell und sehen uns die Ergebnisse an:

get_prediction(real_news, convert_to_label=True)

Ausgabe:

reliable

Anhang: Erstellen einer Übermittlungsdatei für Kaggle

In diesem Abschnitt werden wir alle Artikel vorhersagen test.csv, um eine Einreichungsdatei zu erstellen, um unsere Genauigkeit im Testsatz des Kaggle-Wettbewerbs zu sehen :

# read the test set
test_df = pd.read_csv("test.csv")
# make a copy of the testing set
new_df = test_df.copy()
# add a new column that contains the author, title and article content
new_df["new_text"] = new_df["author"].astype(str) + " : " + new_df["title"].astype(str) + " - " + new_df["text"].astype(str)
# get the prediction of all the test set
new_df["label"] = new_df["new_text"].apply(get_prediction)
# make the submission file
final_df = new_df[["id", "label"]]
final_df.to_csv("submit_final.csv", index=False)

Nachdem wir Autor, Titel und Artikeltext miteinander verkettet haben, übergeben wir die get_prediction()Funktion an die neue Spalte, um die Spalte zu füllen label, und verwenden dann die to_csv()Methode, um die Übermittlungsdatei für Kaggle zu erstellen. Hier ist mein Submission Score:

Einreichungspunktzahl

Wir haben eine Genauigkeit von 99,78 % und 100 % auf privaten und öffentlichen Bestenlisten. Das ist großartig!

Fazit

Okay, wir sind mit dem Tutorial fertig. Sie können diese Seite überprüfen , um verschiedene Trainingsparameter zu sehen, die Sie optimieren können.

Wenn Sie einen benutzerdefinierten Fake-News-Datensatz zur Feinabstimmung haben, müssen Sie einfach eine Liste von Beispielen an den Tokenizer übergeben, wie wir es getan haben, Sie werden danach keinen anderen Code mehr ändern.

Sehen Sie sich den vollständigen Code hier oder die Colab - Umgebung hier an .