聊天产生器
Is Artificial Intelligence(AI) making us lazy or efficient?
人工智能(AI)使我們變得懶惰還是高效?
I think it’s making us efficient. Due to COVID-19, people are more often found interacting with their peers via social media and text messages. For instance, my push notifications are up by 37%, and positively enough I have reconnected with my school friends, old friends per se. However, this arose a problem of constantly sticking to my phone and suffering from Nomophobia and Phantom vibration syndrome.
我認為這使我們高效。 由于使用了COVID-19,人們更常通過社交媒體和短信與同齡人互動。 例如,我的推送通知增加了37%,而且很肯定的是,我與學校朋友,舊朋友本身重新建立了聯系。 但是,這引起了一個問題,就是不斷粘在我的手機上,并患有恐懼癥和幻影振動綜合癥。
Nomophobia — a term describing a growing fear in today’s world — the fear of being without a mobile device, or beyond mobile phone contact. The Post Office commissioned YouGov, a research organization, to look at anxieties suffered by mobile phone users. The study found that about 58 percent of men and 47 percent of women suffer from the phobia, and an additional 9 percent feel stressed when their mobile phones are off. The study sampled 2,163 people. Read more here.
恐懼癥(Nomophobia)是描述當今世界日益增長的恐懼感的一種術語,它表示擔心沒有移動設備或無法與手機通話。 郵局委托研究機構YouGov調查手機用戶遭受的焦慮。 研究發現,大約58%的男性和47%的女性患有恐怖癥,另外9%的人在關閉手機后會感到壓力。 該研究對2,163人進行了抽樣。 在這里。
Phantom vibration syndrome — where you think your phone is vibrating but it’s not — has been around only since the mobile age. Nearly 90 percent of college undergrads in a 2012 study said they felt phantom vibrations. Get more insights here.
幻影振動綜合癥(您認為手機在振動,但實際上并沒有),僅在移動時代出現了。 在2012年的一項研究中,將近90%的大學本科生表示他們感到幻影般的振動。 在此處獲取更多見解。
I was indeed almost always on the phone, and even while sleeping, I used to wake up hastily to check my phone as well, and being an introvert I love to sleep. So, I decided to make AI work for me.
實際上,我幾乎一直在打電話,即使在睡覺的時候,我也常常匆匆醒來,也要檢查我的手機,而且我性格內向,喜歡睡覺。 因此,我決定讓AI為我工作。
With Recurrent Neural Networks (RNN), I decided to train my machine to generate automatic replies, trained based on my personal chats/replies/forwards, etc.
借助遞歸神經網絡(RNN),我決定訓練我的機器以生成自動答復,并根據我的個人聊天/答復/轉發等進行訓練。
On the corollary, there are many fledgling chatbots trained on the humongous text. However, they lack the human touch and the word and sentence formations one uses while texting. While sending short messages, for instance — fewer people write “See you Later” and my personal network uses “c u l8r”- both of which convey the same message, but with different semantic and syntactic structuring.
結果是,有許多剛起步的聊天機器人已經接受了龐大的文本培訓。 但是,它們缺乏人的觸覺,并且在發短信時不會使用單詞和句子的形式。 例如,在發送短消息時-更少的人寫“稍后再見”,而我的個人網絡使用“ cu8r”-兩者都傳達相同的消息,但語義和句法結構不同。
數據集: (Dataset :)
I have 881 text messages which are basically interactions between 11 different participants from India(most of them), Germany, and the USA. Due to this time difference, not all are active at once. Few are more gregarious, few more tacit. So this data is a perfect mix of human interactions — sarcastic and sassy — replies, which are more prominent in taking.
我有881條短信,基本上是來自印度(其中大多數),德國和美國的11個不同參與者之間的互動。 由于該時間差,并非所有功能都同時處于活動狀態。 很少有合群的,很少是默認的。 因此,這些數據完美地融合了人類互動(嘲諷和野蠻)的回答,在回答中更為突出。
The main reason for training on this data is — it’s the most active group in my network and more close to me as I do not want to sound like a bot when I am “replying”.
對此數據進行培訓的主要原因是-它是我網絡中最活躍的組,并且與我更接近,因為我不想在“回復”時聽起來像個機器人。
聊天 (LETS TALK)
# importing necessary librariesimport numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import nltk
import string
import unidecode
import random
import torch
After importing we need to have a GPU as RNN or any deep learning neural network requires heavy computing and takes a long time on CPU. additionally, GPUs have additional advantages over CPUs, these include having more computational units and having a higher bandwidth to retrieve from memory.
導入后,我們需要將GPU用作RNN或任何深度學習神經網絡都需要大量計算,并且在CPU上花費很長時間。 此外,GPU與CPU相比還具有其他優勢,包括具有更多的計算單元和更高的帶寬以從內存中檢索。
train_on_gpu = torch.cuda.is_available()if(train_on_gpu):
print('Training on GPU!')
else:
print('No GPU available, training on CPU; consider making n_epochs very small.')
This code will tell you if you have a GPU or not. even if you don't have one, it is just going to take a longer, but still gives you results.
該代碼將告訴您是否有GPU。 即使您沒有,也將花費更長的時間,但仍然可以為您帶來結果。
train_df = pd.read_csv("WhatsappChat.csv")author = train_df["Content"]
This is how the data frame looks like, I have worked on some data processing and Exploratory Data Analysis to bring it in this formation. The code to change WhatsApp chat into a similar pandas data frame visit here. As I am training it on the content of the chats, we will just be working on that column.
這就是數據框架的樣子,我已經進行了一些數據處理和探索性數據分析,以使其形成這種形式。 將WhatsApp聊天更改為類似熊貓數據框架的代碼請訪問這里。 當我在聊天內容上對其進行培訓時,我們將僅在該列上進行工作。
text = list(author)def joinStrings(text):
return ' '.join(string for string in text)
text = joinStrings(text)
# text = [item for sublist in author[:5].values for item in sublist]
len(text.split())test_sentence = text.lower().split()trigrams = [([test_sentence[i], test_sentence[i + 1]], test_sentence[i + 2])
for i in range(len(test_sentence) - 2)]
chunk_len=len(trigrams)
print(trigrams[:3])
after joining and making the content as a huge text data, I am training the data based on tri-gram as most of the replies — at least in my network — are sized at 3 words in reply.
加入內容并使之成為巨大的文本數據之后,我將根據tri-gram訓練數據,因為大多數答復(至少在我的網絡中)的答復大小為3個字。
Since to Train an RNN, I need a vocabulary size, so that my replies don't go out of bounds.
自從訓練RNN以來,我需要一個詞匯量,以便我的回答不會超出范圍。
vocab = set(test_sentence)voc_len=len(vocab)
word_to_ix = {word: i for i, word in enumerate(vocab)}# making input and their respective replies
inp=[]
tar=[]
for context, target in trigrams:
context_idxs = torch.tensor([word_to_ix[w] for w in context], dtype=torch.long)
inp.append(context_idxs)
targ = torch.tensor([word_to_ix[target]], dtype=torch.long)
tar.append(targ)
RNN (RNN)
It’s time we define our neural network class and see what it can do for us.
是時候定義神經網絡類了,看看它能為我們做什么。
class RNN(nn.Module):def __init__(self, input_size, hidden_size, output_size, n_layers=1):
super(RNN, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.n_layers = n_layers
self.encoder = nn.Embedding(input_size, hidden_size)
self.gru = nn.GRU(hidden_size*2, hidden_size, n_layers,batch_first=True,
bidirectional=False)
self.decoder = nn.Linear(hidden_size, output_size)
def forward(self, input, hidden):
input = self.encoder(input.view(1, -1))
output, hidden = self.gru(input.view(1, 1, -1), hidden)
output = self.decoder(output.view(1, -1))
return output, hiddendef init_hidden(self):
return Variable(torch.zeros(self.n_layers, 1, self.hidden_size))
Here is a class RNN, a general object-oriented programming approach to instantiate objects and their respective methods or functions for faster execution.
這是類RNN,這是一種通用的面向對象的編程方法,用于實例化對象及其各自的方法或函數,以便更快地執行。
The def forward() function is our forward pass or feed-forward network and connection of the neural network. And the def init_hidden() is a variable instantiate for hidden layers.
def forward()函數是我們的前向通過或前饋網絡以及神經網絡的連接。 def init_hidden()是用于隱藏層的變量實例。
def train(inp, target):hidden = decoder.init_hidden().cuda()
decoder.zero_grad()
loss = 0
for c in range(chunk_len):
output, hidden = decoder(inp[c].cuda(), hidden)
loss += criterion(output, target[c].cuda())loss.backward()
decoder_optimizer.step()return loss.data.item() / chunk_len
Now we need to reduce loss to get the optimized reply and check the accuracy of the model. The above code gives us data loss in whole backpropagation.
現在我們需要減少損失以獲得優化的答復并檢查模型的準確性。 上面的代碼使我們在整個反向傳播過程中都丟失了數據。
import time, mathdef time_since(since):s = time.time() - since
m = math.floor(s / 60)
s -= m * 60
return '%dm %ds' % (m, s)
This is a simple function to check how much time does it take to run the program or time taken to train the model.
這是一個簡單的功能,用于檢查運行程序需要多少時間或訓練模型需要的時間。
n_epochs = 50print_every = 10
plot_every = 10
hidden_size = 100
n_layers = 1
lr = 0.015decoder = RNN(voc_len, hidden_size, voc_len, n_layers)
decoder_optimizer = torch.optim.Adam(decoder.parameters(), lr=lr)
criterion = nn.CrossEntropyLoss()start = time.time()
all_losses = []
loss_avg = 0
if(train_on_gpu):
decoder.cuda()
for epoch in range(1, n_epochs + 1):
loss = train(inp,tar)
loss_avg += lossif epoch % print_every == 0:
print('[%s (%d %d%%) %.4f]' % (time_since(start), epoch, epoch / n_epochs * 50, loss))
# print(evaluate('ge', 200), '\n')if epoch % plot_every == 0:
all_losses.append(loss_avg / plot_every)
loss_avg = 0
This is where the magic happens — training the chat and learning what to reply for 50 times I let the machine read the chat and let me know what is the best reply to the message. Also, prints the loss incurred after 10 epochs and time took to execute.
這就是魔術發生的地方–訓練聊天并學習50次答復,讓機器閱讀聊天并讓我知道對消息的最佳答復是什么。 同樣,打印10個歷元和執行時間之后的損失。
import matplotlib.pyplot as pltimport matplotlib.ticker as ticker
%matplotlib inlineplt.figure()
plt.plot(all_losses)
This plots the losses for those who — like me — appreciate plots and likes visual representations then numbers.
對于那些像我一樣欣賞情節,喜歡視覺表示再喜歡數字的人,這會畫出損失。
def evaluate(prime_str='this process', predict_len=100, temperature=0.8):hidden = decoder.init_hidden().cuda()for p in range(predict_len):
prime_input = torch.tensor([word_to_ix[w] for w in prime_str.split()], dtype=torch.long).cuda()
inp = prime_input[-2:] #last two words as input
output, hidden = decoder(inp, hidden)
# Sample from the network as a multinomial distribution
output_dist = output.data.view(-1).div(temperature).exp()
top_i = torch.multinomial(output_dist, 1)[0]
# Add predicted word to string and use as next input
predicted_word = list(word_to_ix.keys())[list(word_to_ix.values()).index(top_i)]
prime_str += " " + predicted_word
# inp = torch.tensor(word_to_ix[predicted_word], dtype=torch.long)return prime_str
We need to define an evaluation function to check if we are getting any tangible replies generated. It takes the prime string, length of the sentences, and temperature which takes care of the missing words if any new message comes.
我們需要定義一個評估函數,以檢查是否生成了任何切實的答復。 它采用素數字符串,句子的長度和溫度,如果有任何新消息出現,它將處理丟失的單詞。
print(evaluate('trip pe',11, temperature=1))# outputtrip pe shuru ? dekh na😅. bumble to sanky bhi use kar sakta
Voila! There is a message generated and it makes less sense, but tangible words. Interestingly, it learned the smileys as well. And we use a huge amount of emoticons in our chats.
瞧! 生成了一條消息,它意義不大,但卻是實話。 有趣的是,它也學會了笑臉。 而且我們在聊天中使用了大量的表情符號。
未來的工作 (Future work)
Now, all I need is work on APIs to embed this code in the WhatsApp chat, let it train in a span of a month, and generate the messages — then I don’t look at my phone. This will cure my sleep cycle and leverage me in interacting with people around me than on my phone. Hopefully with increased epochs, say 100 and more data over time this will give fewer errors and more personalized replies which will trick my friends into wondering whether I’m a BOT or replying with my conscience.
現在,我需要做的就是將這些代碼嵌入到WhatsApp聊天中的API上,讓它在一個月的時間內進行訓練并生成消息-然后我就不會看手機了。 這可以改善我的睡眠周期,并可以使我與周圍的人互動(而不是通過手機)。 希望隨著時代的增加,例如隨著時間的推移增加100個數據,這將減少錯誤并提供更多個性化的答復,這將使我的朋友迷惑不解,我是BOT還是出于良心而回覆。
If you are interested, you can get the code here.
如果您有興趣,可以在此處獲取代碼。
Do let me know if you think this method lacks some ideas, or how I can optimize it further to get to being a humanlike BOT and let this AI take over my communication.
請讓我知道您是否認為這種方法缺乏一些想法,或者我如何進一步優化它以成為一個像人一樣的BOT,并讓該AI接管我的交流。
翻譯自: https://medium.com/the-innovation/chat-generator-d61cc5a1d1df
總結
- 上一篇: 一加Ace2高清海报图放出 冰河蓝、浩瀚
- 下一篇: 苹果供应商开始在印度生产 AirPods