VOOZH about

URL: https://www.geeksforgeeks.org/nlp/fake-news-detection-model-using-tensorflow-in-python/

⇱ Fake News Detection Model using TensorFlow in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Fake News Detection Model using TensorFlow in Python

Last Updated : 23 Jul, 2025

Fake news is a type of misinformation that can mislead readers, influence public opinion, and even damage reputations. Detecting fake news prevents its spread and protects individuals and organizations. Media outlets often use these models to help filter and verify content, ensuring that the news shared with the public is accurate.

In this article we'll build a deep learning model using TensorFlow in Python to detect fake news from text.

Implementation of Fake News Detection Model

We will be building the model with following steps to make our model:

1. Importing Libraries

The libraries we will be using are numpy, pandas, scikit learn and tenserflow

2. Importing the Dataset

We will be using fake news dataset, which contains News text and corresponding label (FAKE or REAL). Dataset can be downloaded from this link.

Output: 

👁 Fake News Detection Model using TensorFlow
Dataset

3. Preprocessing Dataset

As we can see the dataset contains one unnamed column. So we drop that column from the dataset.

Output:

👁 Fake News Detection Model using TensorFlow
Cleaned Dataset

Now that data is cleaned we can go for data encoding.

4. Data Encoding

It converts the categorical column (label in out case) into numerical values.

  • le.fit(data['label']): Fits the encoder on the 'label' column to learn the unique categories.
  • data['label'] = le.transform(data['label']): Transforms the categorical labels into numerical format (0 for REAL, 1 for FAKE).


5. Variables Setup

These are some variables required to be setup for the model training.


6. Tokenization 

This process divides a large piece of continuous text into distinct units or tokens. Here we use columns separately for a temporal basis as a pipeline just for good accuracy.

  • tokenizer1.fit_on_texts(title): Fits the tokenizer on the 'title' column to create a vocabulary.
  • pad_sequences(sequences1): Pads the sequences to ensure they all have the same length.


7. Splitting Data for Training and Testing

  • training_sequences1, test_sequences1: Splits the tokenized and padded data into training and testing sets.
  • training_labels, test_labels: Splits the corresponding labels into training and testing labels.


8. Reshaping Data for LSTM

We will be using LSTM(Long Short Term Memory) model for prediction and for that we need to reshape padded sequence. We are converting it into np.array() as we need training and test sequences into NumPy arrays which are required by TensorFlow models.

9. Generating Word Embedding

Embeddings allows words with similar meanings to have a similar representation. Here each individual word is represented as real-valued vectors in a predefined vector space. For that we will be using glove.6B.50d.txt.

  • !wget: Downloads the pre-trained GloVe embeddings from the following link.
  • !unzip: Unzips the downloaded file containing the GloVe embeddings.

Output:

👁 Screenshot-2025-03-19-161425
Glove Embeddings

Now that our glove embeddings are downloaded we can use them for word embedding.

10. Model Architecture

Here we use the TensorFlow embedding technique with Keras Embedding Layer where we map original input data into some set of real-valued dimensions.

  • Embedding: The embedding layer uses pre-trained GloVe embeddings.
  • Conv1D: A 1D convolutional layer to detect patterns in the text.
  • LSTM(64): An LSTM layer to capture long-term dependencies in the data.

Output : 

👁 Fake News Detection Model using TensorFlow
Model Summary

Now that our model architecture is ready we can use this to train our model.

11. Training the Model

Output: 

👁 Fake News Detection Model using TensorFlow
Trained model

For each epoch training accuracy improves reaching around 97% by the 50th epoch while the validation accuracy is around 75%. The validation loss gradually decreases, indicating that the model is learning from the data but it also shows signs of some overfitting as the validation accuracy is lower than the training accuracy. To avoid this we can further fine tune the model.

12. Sample Prediction

We will test model accuracy with a sample of text to see how our model is working.

Output: 

This news is False

As we can see our model is working fine and now can be used to detect of any information is fake or not.

By following these steps we successfully built a fake news detection model using TensorFlow in Python. This model can be further improved by fine-tuning the hyperparameters, trying different architectures or using more advanced techniques like attention mechanisms. In real-world applications such models can be integrated into news websites or social media platforms to automatically flag fake news.

Comment

Explore