NLP for ChatBots : How to Train Simple Neural Networks for Sentiment Analysis
Data File: D:/mystuff/comments.txt
Data:
i love you,pos
i hate you,neg
i kill you,neg
i love to kill you,neg
i admire you,pos
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
def tfidf(x):
vec = TfidfVectorizer()
vec.fit(x)
return vec.transform(x).toarray()
f = open('D:/mystuff/comments.txt')
lines = f.readlines()
text =[]
y=[]
for line in lines:
w = line.lower().strip().split(",")
text.append(w[0])
l=0
if w[1]=='pos':
l=1
y.append(l)
X = tfidf(text)
Y = np.c_[y]
print(X)
[[0. 0. 0. 0.861037 0. 0.50854232] [0. 0.90275015 0. 0. 0. 0.43016528] [0. 0. 0.861037 0. 0. 0.50854232] [0. 0. 0.50733821 0.50733821 0.62883263 0.29964212] [0.90275015 0. 0. 0. 0. 0.43016528]]
Print(Y)
[[1] [0] [0] [0] [1]]
def mse(y,ycap): return ((y-ycap)**2).mean()
def sigmoid(x): return 1/(1+np.exp(-x))
def derivative(x): return x*(1-x)
np.random.seed(101)
W1 = np.random.random((6,9))
W2 = np.random.random((9,1))
print(W1)
[[0.51639863 0.57066759 0.02847423 0.17152166 0.68527698 0.83389686 0.30696622 0.89361308 0.72154386] [0.18993895 0.55422759 0.35213195 0.1818924 0.78560176 0.96548322 0.23235366 0.08356143 0.60354842] [0.72899276 0.27623883 0.68530633 0.51786747 0.04848454 0.13786924 0.18696743 0.9943179 0.5206654 ] [0.57878954 0.73481906 0.54196177 0.91315356 0.80792015 0.40299783 0.35722434 0.95287671 0.34363158] [0.86509982 0.83027771 0.53816145 0.92246937 0.09714648 0.10284749 0.7015073 0.89047987 0.1595603 ] [0.27557254 0.67249153 0.16430312 0.70137114 0.48763522 0.68067777 0.52154819 0.04339669 0.2239366 ]]
print(W2)
[[0.57520509] [0.12043366] [0.50011671] [0.13800957] [0.0528084 ] [0.17827692] [0.44236813] [0.87758732] [0.94926413]]
pe=0 j=0 conv = 0.000000001 for i in range(100000): l1 = sigmoid(X.dot(W1)) l2 = sigmoid(l1.dot(W2)) e2 = Y-l2 ce = mse(Y,l2) if i%50 ==0: print("Current Error ", ce) diff = abs(pe-ce) if diff<=conv: print("Training completed with ", i+1 , " iterations ") j=1 break d2 = e2*derivative(l2) e1 = d2.dot(W2.T) d1 = e1*derivative(l1) W1 += X.T.dot(d1) W2 += l1.T.dot(d2) pe=ce if j==0: print("Training not completed. ")
Current Error 1.2986065698996415e-05 Current Error 1.2929648731037894e-05 Current Error 1.2873704747295813e-05 Current Error 1.2818227898072753e-05 Current Error 1.2763212429079132e-05 Current Error 1.270865267950278e-05 Current Error 1.2654543080128585e-05 Current Error 1.2600878151500743e-05 Current Error 1.2547652502128916e-05 Current Error 1.249486082673763e-05 Current Error 1.2442497904559593e-05 Current Error 1.2390558597665709e-05 Current Error 1.2339037849334168e-05 Current Error 1.2287930682461896e-05 Current Error 1.2237232198009065e-05 Current Error 1.2186937573481663e-05 Training completed with 13264 iterations
print(W1)
[[ 0.45603253 -0.62798423 -1.32964013 -1.43802362 0.49706007 0.54483843 0.31845807 2.16208593 2.21925376] [ 0.22947915 1.38267337 1.56981474 1.49024135 0.89741654 1.15530848 0.19881147 -1.48134687 -1.43274049] [ 0.77142345 1.44387767 2.23154546 2.02825123 0.26937912 0.52286594 0.14791142 -1.36866899 -2.3683224 ] [ 0.47004479 -0.77242135 -1.26186218 -1.25233567 0.71075634 0.104229 0.37518345 1.97398988 1.91398429] [ 0.91721091 1.62965816 1.76484005 1.98120147 0.17339261 0.34648761 0.66978598 -0.82839166 -2.04960283] [ 0.20165153 -0.08539685 -0.63926595 -0.33355789 0.48797489 0.56822437 0.5136963 -0.07133713 0.24117176]]
print(W2)
[[-0.21504125] [-2.45982184] [-3.83090743] [-3.7231175 ] [ 0.21219171] [-0.31003557] [ 0.1346712 ] [ 3.97735277] [ 5.25740624]]
today session java is not uploaded plz upload
ReplyDeleteIt was really great blog for me, thanks for sharing. Data science classes in pune
ReplyDeleteReally nice post thanks for sharing it. Find the best data science training in Gurgaon
ReplyDeletewonderfull post thanks for sharing of post Data Science Classes in Pune
ReplyDeleteEach member gets singular attention from our experts. In each batch, just twenty students are allowed, so you can book your seat in advance. What makes our training program different from others is its content. Professionals and Data Scientists who have extensive experience in this arena design the educational program. Our experts have significant expertise in this field and develop the course that ensures the each member is equipped with skill and knowledge of being a data scientist. On account of any uncertainty, you can get it solved by our experts. We lead regular tests and debates to assess knowledge of students and prepare them for a successful career. You can get hands-on experience with the live projects.
ReplyDeleteFor More Info: Data Science Training in Gurgaon
Thanks for posting such a Useful information .You have done a great job.
ReplyDeletedata science online training
data science training
data science training in hyderabad
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging. Online Data Science Training in Pune, Mumbai, Delhi NCR
ReplyDeleteYou have worked superbly with your site
ReplyDeletedata science course
I really like your writing style, great date, thank you for posting.
ReplyDeletedata scientist course in delhi
This information is really awesome thanks for sharing most valuable information.
ReplyDeleteData Science
Python
Selenium
ETL Testing
AWS
I really liked your blog. Well-written and informative blog. Keep sharing more.
ReplyDeleteData Science Course in Hyderabad
thanks for share
ReplyDeleteai training in aurangabad