Google Cloud Execs Highlight India’s Role in GenAI Revolution

India is emerging as a significant hub for startup innovation. And it is basically driven by the rapid adoption of generative artificial intelligence (GenAI). Google Cloud executives Chen Goldberg and Subram Natarajan lately discussed how Indian startups are leveraging it to drive value and the challenges. Google recognizes the vibrant entrepreneurial ecosystem of India and […]

Nasscom and Meta Partner to Launch Open-Source and Gen AI Startup Challenge

The National Association of Software and Service Companies (NASSCOM) has partnered with Meta to launch the ‘Open-Source Generative AI Grand Challenge.’ The initiative is mainly aimed to inspire startups and developers in utilizing open-source and generative AI technologies in the creation of solutions which are equipped with significant socio-economic impact. The Centre for Development of […]

Unlocking the Power of Hugging Face for NLP Tasks

The field of Natural Language Processing (NLP) has seen significant advancements in recent years, largely driven by the development of sophisticated models capable of understanding and generating human language. One of the key players in this revolution is Hugging Face, an open-source AI company that provides state-of-the-art models for a wide range of NLP tasks. Hugging Face’s Transformers library has become the go-to resource for developers and researchers looking to implement powerful NLP solutions.

Inbound-leads-automatically-with-ai. These models are trained on vast amounts of data and fine-tuned to achieve exceptional performance on specific tasks. The platform also provides tools and resources to help users fine-tune these models on their own datasets, making it highly versatile and user-friendly.

In this blog, we’ll delve into how to use the Hugging Face library to perform several NLP tasks. We’ll explore how to set up the environment, and then walk through examples of sentiment analysis, zero-shot classification, text generation, summarization, and translation. By the end of this blog, you’ll have a solid understanding of how to leverage Hugging Face models to tackle various NLP challenges.

Setting Up the Environment

First, we need to install the Hugging Face Transformers library, which provides access to a wide range of pre-trained models. You can install it using the following command:

!pip install transformers

This library simplifies the process of working with advanced NLP models, allowing you to focus on building your application rather than dealing with the complexities of model training and optimization.

Task 1: Sentiment Analysis

Sentiment analysis determines the emotional tone behind a body of text, identifying it as positive, negative, or neutral. Here’s how it’s done using Hugging Face:

from transformers import pipeline
classifier = pipeline("sentiment-analysis", token = access_token, model='distilbert-base-uncased-finetuned-sst-2-english')
classifier("This is by far the best product I have ever used; it exceeded all my expectations.")

In this example, we use the sentiment-analysis pipeline to classify the sentiments of sentences, determining whether they are positive or negative.

Classifying one single sentence
Classifying multiple sentences

Task 2: Zero-Shot Classification

Zero-shot classification allows the model to classify text into categories without any prior training on those specific categories. Here’s an example:

classifier = pipeline("zero-shot-classification")
classifier(
"Photosynthesis is the process by which green plants use sunlight to synthesize nutrients from carbon dioxide and water.",
candidate_labels=["education", "science", "business"],
)

The zero-shot-classification pipeline classifies the given text into one of the provided labels. In this case, it correctly identifies the text as being related to “science”.

Zero-Shot Classification

Task 3: Text Generation

In this task, we explore text generation using a pre-trained model. The code snippet below demonstrates how to generate text using the GPT-2 model:

generator = pipeline("text-generation", model="distilgpt2")
generator(
"Just finished an amazing book",
max_length=40, num_return_sequences=2,
)

Here, we use the pipeline function to create a text generation pipeline with the distilgpt2 model. We provide a prompt (“Just finished an amazing book”) and specify the maximum length of the generated text. The result is a continuation of the provided prompt.

Text generation model

Task 4: Text Summarization

Next, we use Hugging Face to summarize a long text. The following code shows how to summarize a piece of text using the BART model:

summarizer = pipeline("summarization")
text = """
San Francisco, officially the City and County of San Francisco, is a commercial and cultural center in the northern region of the U.S. state of California. San Francisco is the fourth most populous city in California and the 17th most populous in the United States, with 808,437 residents as of 2022.
"""
summary = summarizer(text, max_length=50, min_length=25, do_sample=False)
print(summary)

The summarization pipeline is used here, and we pass a lengthy piece of text about San Francisco. The model returns a concise summary of the input text.

Text Summarization

Task 5: Translation

In the final task, we demonstrate how to translate text from one language to another. The code snippet below shows how to translate French text to English using the Helsinki-NLP model:

translator = pipeline("translation", model="Helsinki-NLP/opus-mt-fr-en")
translation = translator("L'engagement de l'entreprise envers l'innovation et l'excellence est véritablement inspirant.")
print(translation)

Here, we use the translation pipeline with the Helsinki-NLP/opus-mt-fr-en model. The French input text is translated into English, showcasing the model’s ability to understand and translate between languages.

Text Translation — French to English Language

Conclusion

The Hugging Face library offers powerful tools for a variety of NLP tasks. By using simple pipelines, we can perform sentiment analysis, zero-shot classification, text generation, summarization, and translation with just a few lines of code. This notebook serves as an excellent starting point for exploring the capabilities of Hugging Face models in NLP projects.

Feel free to experiment with different models and tasks to see the full potential of Hugging Face in action!

This brings us to the end of this article. I hope you have understood everything clearly. Make sure you practice as much as possible.

If you wish to check out more resources related to Data Science, Machine Learning, and Deep Learning, you can refer to my GitHub account.

You can connect with me on LinkedIn — Ravjot Singh.

P.S. Claps and follows are highly appreciated.


Unlocking the Power of Hugging Face for NLP Tasks was originally published in Becoming Human: Artificial Intelligence Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.

Meta Releases Llama 3.1: New Era of Accessible and Customizable AI

Most of the tech giants are aiming to commercialize artificial intelligence (AI). Mark Zuckerberg is taking a different path. His Meta has just unveiled the latest version of its large language model called Llama 3.1. It is made available for free and marks a significant shift in the AI landscape. The exact cost of developing […]

ISRO Celebrates National Space Day with AI and Geospatial Innovation Challenge

India’s space sector has taken a leap forward. Indian Space Research Organisation (ISRO) lately announced ISRO Immersion Startup Challenge initiative in collaboration with IIIT Hyderabad’s National Remote Sensing Centre (NRSC). The event is a significant milestone amid celebration of National Space Day. It is aimed to push the boundaries of space exploration with the help […]

Cohere Announces Layoffs Following $500 Million Funding Round

Generative AI startup Cohere has surprisingly announced layoffs of its 20 employees just after a day of successfully securing a $500 million in Series D funding. Its current valuation is $5.5 billion and the job cuts are 5% of its 400-person workforce. However, the step highlights complex and contradictory dynamics of startups and tech industry. […]

Comparing ANN and CNN on CIFAR-10: A Comprehensive Analysis

Are you curious about how different neural networks stack up against each other? In this blog, we dive into an exciting comparison between Artificial Neural Networks (ANN) and Convolutional Neural Networks (CNN) using the popular CIFAR-10 dataset. We’ll break down the key concepts, architectural differences, and real-world applications of ANNs and CNNs. Join us as we uncover which model reigns supreme for image classification tasks and why. Let’s get started!

Dataset Overview

The CIFAR-10 dataset is a widely-used dataset for machine learning and computer vision tasks. It consists of 60,000 32×32 color images in 10 different classes, with 50,000 training images and 10,000 test images. The classes are airplanes, cars, birds, cats, deer, dogs, frogs, horses, ships, and trucks. This blog explores the performance of Artificial Neural Networks (ANN) and Convolutional Neural Networks (CNN) on the CIFAR-10 dataset.

Sample dataset

What is ANN?

Artificial Neural Networks (ANN) are computational models inspired by the human brain. They consist of interconnected groups of artificial neurons (nodes) that process information using a connectionist approach. ANNs are used for a variety of tasks, including classification, regression, and pattern recognition.

Principles of ANN

  • Layers: ANNs consist of input, hidden, and output layers.
  • Neurons: Each layer has multiple neurons that process inputs and produce outputs.
  • Activation Functions: Functions like ReLU or Sigmoid introduce non-linearity, enabling the network to learn complex patterns.
  • Backpropagation: The learning process involves adjusting weights based on the error gradient.

ANN Architecture

ANN = models.Sequential([
layers.Flatten(input_shape=(32, 32, 3)),
layers.Dense(3000, activation='relu'),
layers.Dense(1000, activation='relu'),
layers.Dense(10, activation='sigmoid')
])
ANN.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'

What is CNN?

Convolutional Neural Networks (CNN) are specialized ANNs designed for processing structured grid data, like images. They are particularly effective for tasks involving spatial hierarchies, such as image classification and object detection.

Principles of CNN

  • Convolutional Layers: These layers apply convolutional filters to the input to extract features.
  • Pooling Layers: Pooling layers reduce the spatial dimensions, retaining important information while reducing computational load.
  • Fully Connected Layers: After convolutional and pooling layers, fully connected layers are used to make final predictions.

CNN Architecture

CNN = models.Sequential([
layers.Conv2D(input_shape=(32, 32, 3), filters=32, kernel_size=(3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(2000, activation='relu'),
layers.Dense(1000, activation='relu'),
layers.Dense(10, activation='softmax')
])
CNN.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

Training and Evaluation

Both models were trained for 10 epochs on the CIFAR-10 dataset. The ANN model uses dense layers and is simpler, while the CNN model uses convolutional and pooling layers, making it more complex and suitable for image data.

ANN.fit(X_train, y_train, epochs=10)
ANN.evaluate(X_test, y_test)

CNN.fit(X_train, y_train, epochs=10)
CNN.evaluate(X_test, y_test)
Training ANN Model
Training CNN Model

Results Comparison

The evaluation results for both models show the accuracy and loss on the test data.

ANN Evaluation

  • Accuracy: 0.4960
  • Loss: 1.4678
Test Data Evaluation for ANN Model

CNN Evaluation

  • Accuracy: 0.7032
  • Loss: 0.8321
Test Data Evaluation for CNN Model

The CNN significantly outperforms the ANN in terms of accuracy and loss.

Confusion Matrices and Classification Reports

To further analyze the models’ performance, confusion matrices and classification reports were generated.

ANN Confusion Matrix and Report

y_pred_ann = ANN.predict(X_test)
y_pred_labels_ann = [np.argmax(i) for i in y_pred_ann]
plot_confusion_matrix(y_test, y_pred_labels_ann, "Confusion Matrix for ANN")
print("Classification Report for ANN:")
print(classification_report(y_test, y_pred_labels_ann))

CNN Confusion Matrix and Report

y_pred_cnn = CNN.predict(X_test)
y_pred_labels_cnn = [np.argmax(i) for i in y_pred_cnn]
plot_confusion_matrix(y_test, y_pred_labels_cnn, "Confusion Matrix for CNN")
print("Classification Report for CNN:")
print(classification_report(y_test, y_pred_labels_cnn))

Conclusion

The CNN model outperforms the ANN model on the CIFAR-10 dataset due to its ability to capture spatial hierarchies and local patterns in the image data. While ANNs are powerful for general tasks, CNNs are specifically designed for image-related tasks, making them more effective for this application.

In summary, for image classification tasks like those in the CIFAR-10 dataset, CNNs offer a significant performance advantage over ANNs due to their specialized architecture tailored for processing visual data.

This brings us to the end of this article. I hope you have understood everything clearly. Make sure you practice as much as possible.

If you wish to check out more resources related to Data Science, Machine Learning and Deep Learning you can refer to my Github account.

You can connect with me on LinkedIn — RAVJOT SINGH.

P.S. Claps and follows are highly appreciated.


Comparing ANN and CNN on CIFAR-10: A Comprehensive Analysis was originally published in Becoming Human: Artificial Intelligence Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.

How a Tiny Japanese Startup is Transforming Dating with AI

Work often consumes most of our time in today’s digital world. Finding genuine companionship is gradually becoming a growing challenge. Japan’s 52-year-old factory worker Chiharu Shimoda faced the same following divorce some two years ago. He turned his focus on a dating app to find a mate. He exchanged messages with several potential partners and […]

Dive Deeper into AI with 150+ New Advanced Tutorials at Chatbots Life!

I hope you’re doing well! I’ve got some exciting news to share that I think you’ll really appreciate.

Our sister publication, Chatbots Life, has just undergone a major redesign. Why should you care? Well, it’s all about making your life as an AI enthusiast easier and more productive.

Here’s What’s New:

Even the most seasoned AI professionals can benefit from a broader knowledge base and knowing how to blend different disciplines.

Here is how Chatbots Life can help.

Odds are that you are a subject area expert and are able to extract value from AI in your field of expertise.

Chatbots Life helps you level up other important areas. It helps you become more interdisciplinary, an AI Polymath.

Becoming an AI Polymath

Here is how easily you can master multiple domains:

  • Master AI in Just 10 Minutes a Day: Our tutorials are crafted to fit into your busy schedule, giving you powerful insights without the overload.
  • Curated for Professionals: Whether you’re in tech, marketing, finance, or any other field, our content is tailored to make AI understandable and useful for you.
  • Learn from the Best: Join a community of over 100,000 professionals from leading companies like Google, Amazon, and Microsoft who are already mastering AI with us.

Our new series of over 150+ tutorials, which are being launched over the next 90 days, offers valuable insights into areas you might be less familiar with:

  • Product Development
  • Roadmaps
  • Product Marketing
  • Creative, Videos, Music, Art, Writing
  • UX Design & Websites
  • SEO, Sales, Marketing,
  • Communication
  • Productivity

Exclusive Free Gifts for New Subscribers! 🎁

Sign up today and get instant access to:

  • How to Use AI for Anything: A Personal & Professional Guide to AI.
  • 🧠AI SecondBrain: We created a Second Brain in Notion that can be powered by AI.
  • 🎨 Complete Midjourney Guide: Unlock the power of AI image generation with our comprehensive Notion guide.
  • 🛠️ Massive List of AI Tools & AI Resources & Free Tutorials

Ready to Take the Next Level with AI?

Sign up and start mastering AI in just 10 minutes a day. It’s free, it’s packed with value, and it’s designed with your career growth in mind.

Don’t miss out on this opportunity to enhance your skills and stay ahead in the AI revolution.

Cheers

Stefan

>>> Learn more at: ChatbotsLife.com


🔥Dive Deeper into AI with 150+ New Advanced Tutorials at Chatbots Life! was originally published in Becoming Human: Artificial Intelligence Magazine on Medium, where people are continuing the conversation by highlighting and responding to this story.