Monday 30 September 2019

neural network 8 chatbot

#chatbot cheat sheet

{"intents": [
        {"tag": "greeting",
         "patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day", "Whats up"],
         "responses": ["Hello!", "Good to see you again!", "Hi there, how can I help?"],
         "context_set": ""
        },
        {"tag": "goodbye",
         "patterns": ["cya", "See you later", "Goodbye", "I am Leaving", "Have a Good day"],
         "responses": ["Sad to see you go :(", "Talk to you later", "Goodbye!"],
         "context_set": ""
        },
        {"tag": "age",
         "patterns": ["how old", "how old is tim", "what is your age", "how old are you", "age?"],
         "responses": ["I am 18 years old!", "18 years young!"],
         "context_set": ""
        },
        {"tag": "name",
         "patterns": ["what is your name", "what should I call you", "whats your name?"],
         "responses": ["You can call me Tim.", "I'm Tim!", "I'm Tim aka Tech With Tim."],
         "context_set": ""
        },
        {"tag": "shop",
         "patterns": ["Id like to buy something", "whats on the menu", "what do you reccommend?", "could i get something to eat"],
         "responses": ["We sell chocolate chip cookies for $2!", "Cookies are on the menu!"],
         "context_set": ""
        },
        {"tag": "hours",
         "patterns": ["when are you guys open", "what are your hours", "hours of operation"],
         "responses": ["We are open 7am-4pm Monday-Friday!"],
         "context_set": ""
        }
   ]
}

--------------------------------------
#cmd
#tflearn doesn't work with python 3.7, uninstall and reinstall python 3.6.x instead

pip install nltk
pip install numpy
pip install tflearn
pip install tensorflow

--------------------------------
#pycharm
#load data

import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()
import numpy
import tflearn
import tensorflow
import random
import json

with open('intents.json') as file:
    data = json.load(file)

#print(data['intents'])
words = []
labels = []
docs_pattern = []
docs_tag = []

for intent in data['intents']:
    for pattern in intent['patterns']:
        #get all the words in pattern
        wds = nltk.word_tokenize(pattern)
        #copy word list to words[]
        words.extend(wds)
        #used for input of neural network
        docs_pattern.append(wds)
        #copy cosresponding category, used as output of neural network
        docs_tag.append(intent['tag'])
        #copy categories into labels[]
        if(intent['tag'] not in labels):
            labels.append(intent['tag'])

#format words so that they don't contain symbols...
words = [stemmer.stem(w.lower()) for w in words if w != '?']
#remove duplicate words, sort
words = sorted(list(set(words)))
labels = sorted(labels)
#one hot encoding, if input entry word exists in dictionary, mark 1 else 0
training = []
output = []
#output default [0,0,0...] length = # of categories
out_empty =[0 for _ in range(len(labels))]
#print(words, labels, out_empty)
#print(docs_pattern, docs_tag)

--------------------------------
#logs

#words (AI input dictionary)

['a', 'ag', 'am', 'anyon', 'ar', 'buy', 'cal', 'could', 'cya', 'day', 'do', 'eat', 'get', 'good', 'goodby', 'guy', 'hav', 'hello', 'hi', 'hour', 'how', 'i', 'id', 'is', 'lat', 'leav', 'lik', 'menu', 'nam', 'of', 'old', 'on', 'op', 'reccommend', 'see', 'should', 'someth', 'the', 'ther', 'tim', 'to', 'up', 'what', 'when', 'yo', 'you']

#labels (AI output categories)

['age', 'goodbye', 'greeting', 'hours', 'name', 'shop']

#doc_patterns (training data input, from AI cheat sheet)

[['Hi'], ['How', 'are', 'you'], ['Is', 'anyone', 'there', '?'], ['Hello'], ['Good', 'day'], ['Whats', 'up'], ['cya'], ['See', 'you', 'later'], ['Goodbye'], ['I', 'am', 'Leaving'], ['Have', 'a', 'Good', 'day'], ['how', 'old'], ['how', 'old', 'is', 'tim'], ['what', 'is', 'your', 'age'], ['how', 'old', 'are', 'you'], ['age', '?'], ['what', 'is', 'your', 'name'], ['what', 'should', 'I', 'call', 'you'], ['whats', 'your', 'name', '?'], ['Id', 'like', 'to', 'buy', 'something'], ['whats', 'on', 'the', 'menu'], ['what', 'do', 'you', 'reccommend', '?'], ['could', 'i', 'get', 'something', 'to', 'eat'], ['when', 'are', 'you', 'guys', 'open'], ['what', 'are', 'your', 'hours'], ['hours', 'of', 'operation']]

#doc_tags (corresponding training data output)

['greeting', 'greeting', 'greeting', 'greeting', 'greeting', 'greeting', 'goodbye', 'goodbye', 'goodbye', 'goodbye', 'goodbye', 'age', 'age', 'age', 'age', 'age', 'name', 'name', 'name', 'shop', 'shop', 'shop', 'shop', 'hours', 'hours', 'hours']

------------------------------------------
#pycharm
#translate data to machine

for n, doc in enumerate(docs_pattern):
    bag = []
    #clean input words
    wd_ = [stemmer.stem(w) for w in doc]
    #one hot encoding, training input, look through dictionary,
    # mark the words exist in user entry as 1, the rest 0
    for w in words:
        if w in wd_:
            bag.append(1)
        else:
            bag.append(0)

    output_ = out_empty[:]
    #the training output, mark 1 category, the rest 0
    output_[labels.index(docs_tag[n])] = 1

    training.append(bag)
    output.append(output_)

training = numpy.array(training)
output = numpy.array(output)
#print(training, output)

------------------------------------
#logs

#training (each row represent the AI dictionary, if word exists in input data, 1, else 0)

[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 1]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 1 0 1]
 [0 0 0 ... 0 1 0]
 [0 0 0 ... 0 0 0]]

#output (each input sentence belongs to 1 of the output categories)

[[0 0 1 0 0 0]
 [0 0 1 0 0 0]
 [0 0 1 0 0 0]
 [0 0 1 0 0 0]
 [0 0 1 0 0 0]
 [0 0 1 0 0 0]
 [0 1 0 0 0 0]
 [0 1 0 0 0 0]
 [0 1 0 0 0 0]
 [0 1 0 0 0 0]
 [0 1 0 0 0 0]
 [1 0 0 0 0 0]
 [1 0 0 0 0 0]
 [1 0 0 0 0 0]
 [1 0 0 0 0 0]
 [1 0 0 0 0 0]
 [0 0 0 0 1 0]
 [0 0 0 0 1 0]
 [0 0 0 0 1 0]
 [0 0 0 0 0 1]
 [0 0 0 0 0 1]
 [0 0 0 0 0 1]
 [0 0 0 0 0 1]
 [0 0 0 1 0 0]
 [0 0 0 1 0 0]
 [0 0 0 1 0 0]]

--------------------------------------------------
#pycharm
#train, generate model

tensorflow.reset_default_graph()
input AI dictionary 45 words
hidden layer 1 8 neuron
hidden layer 2 8 neuron
output 6 category. (softmax format [0.05,0.05,0.75,0.05,0.05,0.05] sum of all = 1)

#input shape
net = tflearn.input_data(shape=[None, len(training[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(output[0]), activation='softmax')
net = tflearn.regression(net)

model = tflearn.DNN(net)
model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)
model.save('model.tflearn')

--------------------------------------
#logs
#AI becomes intelligent after training

Training Step: 3997  | total loss: [1m [32m0.01108 [0m [0m | time: 0.001s
| Adam | epoch: 1000 | loss: 0.01108 - acc: 0.9999 -- iter: 08/26
[A [ATraining Step: 3998  | total loss: [1m [32m0.01098 [0m [0m | time: 0.002s
| Adam | epoch: 1000 | loss: 0.01098 - acc: 0.9999 -- iter: 16/26
[A [ATraining Step: 3999  | total loss: [1m [32m0.01092 [0m [0m | time: 0.003s
| Adam | epoch: 1000 | loss: 0.01092 - acc: 1.0000 -- iter: 24/26
[A [ATraining Step: 4000  | total loss: [1m [32m0.01149 [0m [0m | time: 0.004s
| Adam | epoch: 1000 | loss: 0.01149 - acc: 1.0000 -- iter: 26/26

-----------------------------------------
reference:

https://www.youtube.com/watch?v=wypVcNIH6D4&t=41s
https://www.youtube.com/watch?v=ON5pGUJDNow
https://www.youtube.com/watch?v=PzzHOvpqDYs

No comments:

Post a Comment