-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse_nn.py
93 lines (79 loc) · 3.39 KB
/
use_nn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import tensorflow as tf
import pickle
import numpy as np
import time
import nltk
# import flask_demo as f
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
mihir="positive"
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_classes = 2
batch_size = 64
hm_epochs = 10
x = tf.placeholder('float')
y = tf.placeholder('float')
hidden_1_layer = {'f_fum':n_nodes_hl1,
'weight':tf.Variable(tf.random_normal([5057, n_nodes_hl1])),# instead of 5057 write your input data size
'bias':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'f_fum':n_nodes_hl2,
'weight':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
'bias':tf.Variable(tf.random_normal([n_nodes_hl2]))}
output_layer = {'f_fum':None,
'weight':tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])),
'bias':tf.Variable(tf.random_normal([n_classes])),}
def neural_network_model(data):
l1 = tf.add(tf.matmul(data,hidden_1_layer['weight']), hidden_1_layer['bias'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,hidden_2_layer['weight']), hidden_2_layer['bias'])
l2 = tf.nn.relu(l2)
output = tf.matmul(l2,output_layer['weight']) + output_layer['bias']
return output
saver = tf.train.Saver()
def getTokens(input):
tokensBySlash = str(input.encode('utf-8')).split('/') #get tokens after splitting by slash
allTokens = []
for i in tokensBySlash:
tokens = str(i).split('-') #get tokens after splitting by dash
tokensByDot = []
for j in range(0,len(tokens)):
tempTokens = str(tokens[j]).split('.') #get tokens after splitting by dot
tokensByDot = tokensByDot + tempTokens
allTokens = allTokens + tokens + tokensByDot
allTokens = list(set(allTokens)) #remove redundant tokens
if 'com' in allTokens:
allTokens.remove('com') #removing .com since it occurs a lot of times and it should not be included in our features
#print(allTokens)
return allTokens
def use_neural_network(input_data):
prediction = neural_network_model(x)
with open('lexicon.pickle','rb') as f:
lexicon = pickle.load(f)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver.restore(sess,".\model.ckpt")
current_words = getTokens(input_data.lower())
current_words = [lemmatizer.lemmatize(i) for i in current_words]
features = np.zeros(len(lexicon))
for word in current_words:
if word.lower() in lexicon:
index_value = lexicon.index(word.lower())
# OR DO +=1, test both
features[index_value] += 1
features = np.array(list(features))
# pos: [1,0] , argmax: 0
# neg: [0,1] , argmax: 1
result = (sess.run(tf.argmax(prediction.eval(feed_dict={x:[features]}),1)))
print(prediction.eval(feed_dict={x:[features]}))
if result[0] == 0:
print('Positive:',input_data)
return "positive"
elif result[0] == 1:
print('Negative:',input_data)
return "negative"
use_neural_network("crackspider.us/toolbar/install.php?pack=exe")
#use_neural_network(f.output)
#if f.output!="":
# import flask_demo as f