Showing posts with label Machine Learning. Show all posts
Showing posts with label Machine Learning. Show all posts

Saturday, June 26, 2021

Create Question And Answer NLP Model With Bert

Hello,

Recently I worked on POC for chatbot where I evaluated Question Answering with Bert. Here in this blog we will see how you can create Question, Answering with Bert.

What is Bert?

According to team, who developed Bert

BERT stands for Bidirectional Encoder Representations from Transformers. It is designed to pre-train deep bidirectional representations from unlabelled text by jointly conditioning on both left and right context. As a result, the pre-trained BERT model can be fine-tuned with just one additional output layer to create state-of-the-art models for a wide range of NLP tasks.”

Bert is pertained on massive dataset and large corpus of unlabelled text. That's the hidden power of Bert as it uses knowledge gained from pre-training and apply it to dataset given. 

For this POC we used HuggingFace's transformers. So first you have to install transformers. Using this model you can get advantage of pre trained data and then you can pass your reference text to it and this model will try to find answers from it.

pip install transformers

or 

pip3 install transformers

Because this models are very big and it takes time to load and download. Let's first save it. Create model.py file and add following code.

from transformers import BertForQuestionAnswering

from transformers import BertTokenizer

BertForQuestionAnswering.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad').save_pretrained('./trainedModel')

BertTokenizer.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad').save_pretrained('./trainedModel')

Now execute it with python command. It will create trainedModel directory and save model there with all required files. Now we can load this pertained saved model.

from transformers import BertForQuestionAnswering

from transformers import BertTokenizer

import torch

bertPreTrainedModel = BertForQuestionAnswering.from_pretrained('./trainedModel')

tokenizer = BertTokenizer.from_pretrained('./trainedModel')

encoded = tokenizer.encode('YOUR_QUESTION', 'YOUR_REFERENCE_TEXT')

tokens = tokenizer.convert_ids_to_tokens(encoded)

sepLocation = encoded.index(tokenizer.sep_token_id)

first_seg_len, second_seg_len = sepLocation + 1, len(encoded) - (sepLocation + 1)

seg_embedding = [0] * first_seg_len + [1] * second_seg_len

modelScores = bertPreTrainedModel(torch.tensor([encoded]), token_type_ids=torch.tensor([seg_embedding]))

ans_start_loc, ans_end_loc = torch.argmax(modelScores[0]), torch.argmax(modelScores[len(modelScores)-1])

result = ' '.join(tokens[ans_start_loc:ans_end_loc + 1])

result = result.replace(' ##', '')

Here you will get your answer in result. This way you can develop your model using BERT and transformers.

Tuesday, April 4, 2017

Chatbots should be Smarter

Recently I published a blog on Why business will need a chat bot. You can read it here.

Why Each Business Will Need A Chat bot

In that blog I mentioned that this bots need to be very very smart to interact with humans. The algorithms should be powerful to have conversation with humans because humans will do every possible tricks to trap bots and sometime it fails. Survival of fittest, rule of Darwin is applicable to AI as well. Only strong and intelligent algorithm will survive. If you are creating bot make sure you make it really smart, then only it will survive.

Lets start blog by one incident I recently had with bots. I was working with Amazon AWS and faced some issue in it and solved it my self so as per my practice I immediately published a blog and published on twitter. Following is my tweet.


And I got reply from AWS twitter bot.


Technically bot is not wrong as I mentioned word "not working" in my tweet but that was my blog title but it understood as an issue and replied with suggestion so it's not complete fail but as I mentioned bots need to be smart enough to identify what exactly customer needs and reply to it.

Along with Artificial Intelligence a chat bot needs Emotional Intelligence (EI) to understand human emotions. As we move into the future, AI needs humanizing qualities to improve the way it interacts with us, meets our needs for information, and even controls the other technology around us. As AI becomes more ubiquitous, society needs chat bots to become emotionally intelligent if they are to be used to their full capabilities. Humanizing the chat bot is the next step toward a more connected and integrated future. Blending technology with human capabilities such as emotion and empathy can revolutionize the workplace and other facets of life. In my opinion, smarter AI is one of the most exciting prospects of the future.


Thursday, March 16, 2017

Why Your Business Needs A Chat bot

Recently we have been discussing to add a chatbot for our new venture and that lead me to write this article on why a business will need a chatbot and what are the advantages of it.

What is a Chat bot?


In a very simple definition, a chat bot is a computer program designed to simulate conversation with human users, especially over the Internet.


So yes a chat bot is nothing but a computer program which can handle communication with users over the internet using the smart and artificial intelligence programmed in it by the developers. So in a way chat bot is your virtual customer care executive which does not exits in real life but it's in computer. Many of the businesses and website have real time talk to customer care on their website. You start a chat and in few moments you got a reply from one of the executive. But may times it's not human it's bot replying to your queries and helping you in finding a solutions to your problem. It talks like human, it behaves like human and it lead conversions like human but the fact is it's chat bot program which is replying to you. Many of the businesses are using the chat bots successfully and many of the companies are developing chat bots. Real challenge right now in chat bot development is that you have to make it really really smart to handle all types of conversation and with the increasing use of Artificial intelligence, machine learning and deep learning, natural language processing chat bots are becoming smarter and smarter. Even smarter than human as well. 

One of the biggest trends in social media, specially social messaging apps, are bots and bot stores. Technology analysts predicted that bot stores will be the biggest thing since the rise of app stores. in 2017 we will see many developments on chat bots. Most of the businesses will opt for chat bot services to improve their business practices and customer care. Here are some reasons on Why Your Business Needs a Chat Bot

1) 24 X 7 Customer care


Computer do not eat, computer do not sleep, it needs only constant power to be on for 24 X 7 hours. So if you are using chat bot for the customer care, your customers will get 24 X 7 hours services. They can contact you any time and your bot will reply to them on behalf of you. This way your customer will be much satisfied and you can attend customers queries across the globe and different time zones. You don't need a call center and team of executives to handle it and it's cost effective too as you don't have to pay salary to customer care executives and do not need to invest in customer care center infrastructure, just invest in a chat bot and it will work for you all the time.

2) Better Customer Engagements with Buying Influence


Most of the companies selling online their services and products are using chat bots customer engagement. A customer browsing through your websites, products and services online on your website and chat bot can give more options to your customers based on customer preference and it will be more interactive experience to your customers and users as they feel it as a very personalized approach and they will be happy with this and you will get better customer engagements with this. Also chat bot can ask for the user preferences like what style they want and what brand they want and based on that, it will suggest the buying options thus influencing customer with more buying options.

3) Boost Social Media Engagement


In April, Facebook announced it would be opening up its increasingly influential Messenger platform in beta — and allowing brands to start using chatbots there. Not only can this help brands to achieve better sales, but it’s also a natural way with which to bolster social media engagement. By deploying a useful chatbot on Facebook, you’ll be far more likely to attract users on to your business page – where you are hopefully already bending over backwards in order to drive sales and engage with consumers on a personal level.


4) Simplify Your Business Processes


A chat bot is not just for the customer care and sales, it can even help you with your businesses. A custom chat bot designed for your business can help you simplify your business processes and let you be more productive in your business. A chat bot can also take business decisions based on certain parameters and help you in maintaining flow of your business.

In a way, chat bot is going to take industry by storm in upcoming years and chat bot will act as


  • Your lawyer
  • Your personal stylist
  • Your personal assistant
  • Your doctor
  • Your financial advisor
  • Your teacher
  • Your Accountant


and many more you can imagine.

Thursday, March 9, 2017

Top 5 Real World Examples of Machine Learning

What is Machine Learning?





Machine learning is a type of artificial intelligence (AI) that provides computers with the ability to learn without being explicitly programmed. Machine learning focuses on the development of computer programs that can change when exposed to new data.  Evolved from the study of pattern recognition and computational learning theory in artificial intelligence, machine learning explores the study and construction of algorithms that can learn from and make predictions on data. The process of machine learning is similar to that of data mining. Both systems search through data to look for patterns. 

Machine learning is now widely used by many software manufactures. Most of the IT companies are now investing in Machine learning. Developers are working on creating complex algorithms to enhance machine learning in systems and provide more intelligence to end users. Here in this blog we will see five real world examples of machine learning.

1) Word prediction and corrections in Office 365 by Microsoft


Just when you thought Google's AI products are the best, Microsoft strikes back with AI in Microsoft Word that blows away Google Docs. The recent version of office 365 has mind blowing machine learning added in word prediction and corrections. It just don't correct it but it will also learn from your way of writing and learns the words which you use frequently. It's really awesome. You can read more about it on following link.


2) Facebook and Machine Learning


Facebook builds its business by learning about its users and packaging their data for advertisers. It then reinvests this money into offering us new, useful functionality – currently video and shopping - which it also uses to learn even more about us. Facebook achieve its goals of providing greater convenience to users, and enabling them to learn more about us. You can read more at following link.


3) JP Moragan Software COIN



The program, called COIN, for Contract Intelligence, does the mind-numbing job of interpreting commercial-loan agreements that, until the project went online in June, consumed 360,000 hours of work each year by lawyers and loan officers. The software reviews documents in seconds. COIN is just the start for the biggest U.S. bank. The firm recently set up technology hubs for teams specializing in big data, robotics and cloud infrastructure to find new sources of revenue, while reducing expenses and risks. Read more at below link.



4) Google's Self Driving Cars


Google's self-driving cars can tour you around the streets of Mountain View, California. Google has mapped 2,000 miles of road. The US road network has 4 million miles of road. Google's team uses machine learning algorithms to create models of other people on the road. Every single mile of driving is logged, and that data fed into computers that classify how different types of objects act in all these different situations. While some driver behavior could be hardcoded in ("When the lights turn green, cars go"), they don't exclusively program that logic, but learn it from actual driver behavior. You can read more about it on following link.


5) Google Maps use Machine Learning to Predict Parking Difficulty


Google Maps now tackles parking problems as well. Google quietly launched a new parking feature for Google Maps on Android across 25 major US cities. If you are in these metro areas, you will now see a red parking sign that indicates limited parking availability to help you plan your trip. The interesting part of this update is that it does not rely on internet-connected parking meters; which often provide incomplete or wrong information due to illegal parkers or those who depart early from their spot. Instead, Google Maps combined crowdsourced data and relatively simple machine learning algorithms to classify parking difficulty. You can read more about it at following link.