-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUse_NN.py
104 lines (81 loc) · 3.58 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
94
95
96
97
98
99
100
101
102
103
104
"""
This file is used to use the predict the trained neural network and get
predictions for new input. The input an be given in the last line while
calling use_neural_network() method.
@Author Sanjay Haresh Khatwani ([email protected])
@Author Savitha Jayasankar ([email protected])
@Author Saurabh Parekh ([email protected])
"""
import create_feature_sets
import tensorflow as tf
import os
# Build the structure of the neural network exactly same as the
# train_and_test.py, so that the input features can be run through the neural
# network.
number_nodes_HL1 = 100
number_nodes_HL2 = 100
number_nodes_HL3 = 100
x = tf.placeholder('float', [None, 23])
y = tf.placeholder('float')
with tf.name_scope("HiddenLayer1"):
hidden_1_layer = {'number_of_neurons': number_nodes_HL1,
'layer_weights': tf.Variable(
tf.random_normal([23, number_nodes_HL1])),
'layer_biases': tf.Variable(
tf.random_normal([number_nodes_HL1]))}
with tf.name_scope("HiddenLayer2"):
hidden_2_layer = {'number_of_neurons': number_nodes_HL2,
'layer_weights': tf.Variable(
tf.random_normal(
[number_nodes_HL1, number_nodes_HL2])),
'layer_biases': tf.Variable(
tf.random_normal([number_nodes_HL2]))}
with tf.name_scope("HiddenLayer3"):
hidden_3_layer = {'number_of_neurons': number_nodes_HL3,
'layer_weights': tf.Variable(
tf.random_normal(
[number_nodes_HL2, number_nodes_HL3])),
'layer_biases': tf.Variable(
tf.random_normal([number_nodes_HL3]))}
with tf.name_scope("OutputLayer"):
output_layer = {'number_of_neurons': None,
'layer_weights': tf.Variable(
tf.random_normal([number_nodes_HL3, 2])),
'layer_biases': tf.Variable(tf.random_normal([2])),}
# Nothing changes in this method as well.
def neural_network_model(data):
l1 = tf.add(tf.matmul(data, hidden_1_layer['layer_weights']),
hidden_1_layer['layer_biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1, hidden_2_layer['layer_weights']),
hidden_2_layer['layer_biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2, hidden_3_layer['layer_weights']),
hidden_3_layer['layer_biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3, output_layer['layer_weights']) + output_layer[
'layer_biases']
return output
saver = tf.train.Saver()
def use_neural_network(input_data):
"""
In this method we restore the model created previously and obtain a
prediction for an input sentence.
:param input_data:
:return:
"""
prediction = neural_network_model(x)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver.restore(sess, os.path.join(os.getcwd(),
'model\sarcasm_model.ckpt'))
features = create_feature_sets.extractFeatureOfASentence(input_data)
result = (sess.run(tf.argmax(prediction.eval(feed_dict={x: [
features]}), 1)))
if result[0] == 0:
print('Sarcastic:', input_data)
elif result[0] == 1:
print('Regular:', input_data)
# Supply the sentence to be tested below as a parameter in the method call.
if __name__ == '__main__':
use_neural_network("Going to the gym surely makes you fit, in a same way standing in a garage make you a car!")