Created
January 24, 2017 20:53
-
-
Save ugik/40e05c9701ae0ad61e369279e0e74f9f to your computer and use it in GitHub Desktop.
text ANN functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import time | |
# compute sigmoid nonlinearity | |
def sigmoid(x): | |
output = 1/(1+np.exp(-x)) | |
return output | |
# convert output of sigmoid function to its derivative | |
def sigmoid_output_to_derivative(output): | |
return output*(1-output) | |
def clean_up_sentence(sentence): | |
# tokenize the pattern | |
sentence_words = nltk.word_tokenize(sentence) | |
# stem each word | |
sentence_words = [stemmer.stem(word.lower()) for word in sentence_words] | |
return sentence_words | |
# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence | |
def bow(sentence, words, show_details=False): | |
# tokenize the pattern | |
sentence_words = clean_up_sentence(sentence) | |
# bag of words | |
bag = [0]*len(words) | |
for s in sentence_words: | |
for i,w in enumerate(words): | |
if w == s: | |
bag[i] = 1 | |
if show_details: | |
print ("found in bag: %s" % w) | |
return(np.array(bag)) | |
def think(sentence, show_details=False): | |
x = bow(sentence.lower(), words, show_details) | |
if show_details: | |
print ("sentence:", sentence, "\n bow:", x) | |
# input layer is our bag of words | |
l0 = x | |
# matrix multiplication of input and hidden layer | |
l1 = sigmoid(np.dot(l0, synapse_0)) | |
# output layer | |
l2 = sigmoid(np.dot(l1, synapse_1)) | |
return l2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment