|
a |
|
b/tf_utils.py |
|
|
1 |
import pandas |
|
|
2 |
# from pattern.en import sentiment |
|
|
3 |
# import HTMLParser |
|
|
4 |
import re |
|
|
5 |
import pandas as pd |
|
|
6 |
import tensorflow as tf |
|
|
7 |
from collections import Counter |
|
|
8 |
from nltk.corpus import stopwords |
|
|
9 |
import string |
|
|
10 |
from collections import OrderedDict |
|
|
11 |
from nltk import bigrams |
|
|
12 |
from nltk.tokenize import word_tokenize |
|
|
13 |
import matplotlib.pyplot as plt |
|
|
14 |
import numpy as np |
|
|
15 |
# import plotly.plotly as py |
|
|
16 |
|
|
|
17 |
# import pandas as pd |
|
|
18 |
# import matplotlib.pyplot as plt |
|
|
19 |
import numpy as np |
|
|
20 |
from sklearn.metrics import recall_score, precision_score, accuracy_score |
|
|
21 |
import math |
|
|
22 |
from sklearn.feature_extraction.text import CountVectorizer |
|
|
23 |
from sklearn.model_selection import train_test_split |
|
|
24 |
from sklearn.feature_extraction.text import TfidfVectorizer |
|
|
25 |
from sklearn.naive_bayes import MultinomialNB |
|
|
26 |
from sklearn.metrics import confusion_matrix |
|
|
27 |
from sklearn.feature_selection import RFE |
|
|
28 |
import requests |
|
|
29 |
from bs4 import BeautifulSoup |
|
|
30 |
# import numpy as np |
|
|
31 |
# import matplotlib.pyplot as plt |
|
|
32 |
# from matplotlib import style |
|
|
33 |
# style.use("ggplot") |
|
|
34 |
import os |
|
|
35 |
|
|
|
36 |
|
|
|
37 |
def load_dataset(): |
|
|
38 |
X = pd.read_csv("C:\Shashank Reddy\DataSet_Final.csv", encoding="ISO-8859-1").fillna(0) |
|
|
39 |
|
|
|
40 |
Y = pd.read_csv("C:\Shashank Reddy\FinalTreatment.csv", encoding="ISO-8859-1").fillna(0) |
|
|
41 |
|
|
|
42 |
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2) |
|
|
43 |
|
|
|
44 |
|
|
|
45 |
# test_size = 20 percent |
|
|
46 |
|
|
|
47 |
return X_train,X_test,y_train,y_test |
|
|
48 |
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
def convert_to_one_hot(labels,C): |
|
|
52 |
""" |
|
|
53 |
Creates a matrix where the i-th row corresponds to the ith class number and the jth column |
|
|
54 |
corresponds to the jth training example. So if example j had a label i. Then entry (i,j) |
|
|
55 |
will be 1. |
|
|
56 |
|
|
|
57 |
Arguments: |
|
|
58 |
labels -- vector containing the labels |
|
|
59 |
C -- number of classes, the depth of the one hot dimension |
|
|
60 |
|
|
|
61 |
Returns: |
|
|
62 |
one_hot -- one hot matrix |
|
|
63 |
""" |
|
|
64 |
# Create a tf.constant equal to C (depth), name it 'C'. (approx. 1 line) |
|
|
65 |
C = tf.constant(C, name='C') |
|
|
66 |
|
|
|
67 |
# Use tf.one_hot, be careful with the axis (approx. 1 line) |
|
|
68 |
one_hot_matrix = tf.one_hot(indices=labels, depth=C, axis=0) |
|
|
69 |
|
|
|
70 |
# Create the session (approx. 1 line) |
|
|
71 |
sess = tf.Session() |
|
|
72 |
|
|
|
73 |
# Run the session (approx. 1 line) |
|
|
74 |
one_hot = sess.run(one_hot_matrix) |
|
|
75 |
|
|
|
76 |
# Close the session (approx. 1 line). See method 1 above. |
|
|
77 |
sess.close() |
|
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
return one_hot |
|
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
def create_placeholders(n_x,n_y): |
|
|
86 |
X = tf.placeholder(tf.float32, [n_x, None], name="X") |
|
|
87 |
Y = tf.placeholder(tf.float32, [n_y, None], name="Y") |
|
|
88 |
|
|
|
89 |
return X,Y |
|
|
90 |
|
|
|
91 |
|
|
|
92 |
def initialize_parameters(): |
|
|
93 |
""" |
|
|
94 |
Initializes parameters to build a neural network with tensorflow. The shapes are: |
|
|
95 |
W1 : [12, 14] |
|
|
96 |
b1 : [12, 1] |
|
|
97 |
W2 : [12, 12] |
|
|
98 |
b2 : [12, 1] |
|
|
99 |
W3 : [10,12] |
|
|
100 |
b3 : [10,1] |
|
|
101 |
W4 : [9, 12] |
|
|
102 |
b4 : [9, 1] |
|
|
103 |
|
|
|
104 |
Returns: |
|
|
105 |
parameters -- a dictionary of tensors containing W1, b1, W2, b2, W3, b3 |
|
|
106 |
""" |
|
|
107 |
|
|
|
108 |
|
|
|
109 |
W1 = tf.get_variable("W1", [12, 14], initializer=tf.contrib.layers.xavier_initializer()) |
|
|
110 |
b1 = tf.get_variable("b1", [12, 1], initializer=tf.zeros_initializer()) |
|
|
111 |
W2 = tf.get_variable("W2", [12, 12], initializer=tf.contrib.layers.xavier_initializer()) |
|
|
112 |
b2 = tf.get_variable("b2", [12, 1], initializer=tf.zeros_initializer()) |
|
|
113 |
W3 = tf.get_variable("W3", [10, 12], initializer=tf.contrib.layers.xavier_initializer()) |
|
|
114 |
b3 = tf.get_variable("b3", [10, 1], initializer=tf.zeros_initializer()) |
|
|
115 |
W4 = tf.get_variable("W4", [9, 12], initializer=tf.contrib.layers.xavier_initializer()) |
|
|
116 |
b4 = tf.get_variable("b4", [9, 1], initializer=tf.zeros_initializer()) |
|
|
117 |
|
|
|
118 |
|
|
|
119 |
parameters = {"W1": W1, |
|
|
120 |
"b1": b1, |
|
|
121 |
"W2": W2, |
|
|
122 |
"b2": b2, |
|
|
123 |
"W3": W3, |
|
|
124 |
"b3": b3, |
|
|
125 |
"W4":W4, |
|
|
126 |
"b4":b4} |
|
|
127 |
|
|
|
128 |
return parameters |
|
|
129 |
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
def forward_propagation(X, parameters): |
|
|
133 |
""" |
|
|
134 |
Implements the forward propagation for the model: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SOFTMAX |
|
|
135 |
|
|
|
136 |
Arguments: |
|
|
137 |
X -- input dataset placeholder, of shape (input size, number of examples) |
|
|
138 |
parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3" |
|
|
139 |
the shapes are given in initialize_parameters |
|
|
140 |
|
|
|
141 |
Returns: |
|
|
142 |
Z3 -- the output of the last LINEAR unit |
|
|
143 |
""" |
|
|
144 |
|
|
|
145 |
# Retrieve the parameters from the dictionary "parameters" |
|
|
146 |
W1 = parameters['W1'] |
|
|
147 |
b1 = parameters['b1'] |
|
|
148 |
W2 = parameters['W2'] |
|
|
149 |
b2 = parameters['b2'] |
|
|
150 |
W3 = parameters['W3'] |
|
|
151 |
b3 = parameters['b3'] |
|
|
152 |
W4 = parameters['W4'] |
|
|
153 |
b4 = parameters['b4'] |
|
|
154 |
|
|
|
155 |
# Numpy Equivalents: |
|
|
156 |
Z1 = tf.add(tf.matmul(W1, X), b1) # Z1 = np.dot(W1, X) + b1 |
|
|
157 |
A1 = tf.nn.relu(Z1) # A1 = relu(Z1) |
|
|
158 |
Z2 = tf.add(tf.matmul(W2, A1), b2) # Z2 = np.dot(W2, a1) + b2 |
|
|
159 |
A2 = tf.nn.relu(Z2) # A2 = relu(Z2) |
|
|
160 |
Z3 = tf.add(tf.matmul(W3, A2), b3) |
|
|
161 |
# Z3 = np.dot(W3,Z2) + b3 |
|
|
162 |
|
|
|
163 |
|
|
|
164 |
return Z3 |
|
|
165 |
|
|
|
166 |
|
|
|
167 |
def compute_cost(Z3, Y): |
|
|
168 |
""" |
|
|
169 |
Computes the cost |
|
|
170 |
|
|
|
171 |
Arguments: |
|
|
172 |
Z3 -- output of forward propagation (output of the last LINEAR unit), of shape (9, number of examples) |
|
|
173 |
Y -- "true" labels vector placeholder, same shape as Z3 |
|
|
174 |
|
|
|
175 |
Returns: |
|
|
176 |
cost - Tensor of the cost function |
|
|
177 |
""" |
|
|
178 |
|
|
|
179 |
# to fit the tensorflow requirement for tf.nn.softmax_cross_entropy_with_logits(...,...) |
|
|
180 |
logits = tf.transpose(Z3) |
|
|
181 |
labels = tf.transpose(Y) |
|
|
182 |
|
|
|
183 |
|
|
|
184 |
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels)) |
|
|
185 |
|
|
|
186 |
|
|
|
187 |
return cost |
|
|
188 |
|
|
|
189 |
|
|
|
190 |
def random_mini_batches(X, Y, mini_batch_size): |
|
|
191 |
""" |
|
|
192 |
Creates a list of random minibatches from (X, Y) |
|
|
193 |
|
|
|
194 |
Arguments: |
|
|
195 |
X -- input data, of shape (input size, number of examples) |
|
|
196 |
Y -- true "label" vector (1 for blue dot / 0 for red dot), of shape (1, number of examples) |
|
|
197 |
mini_batch_size -- size of the mini-batches, integer |
|
|
198 |
|
|
|
199 |
Returns: |
|
|
200 |
mini_batches -- list of synchronous (mini_batch_X, mini_batch_Y) |
|
|
201 |
""" |
|
|
202 |
|
|
|
203 |
|
|
|
204 |
m = X.shape[1] # number of training examples |
|
|
205 |
mini_batches = [] |
|
|
206 |
|
|
|
207 |
# Step 1: Shuffle (X, Y) |
|
|
208 |
permutation = list(np.random.permutation(m)) |
|
|
209 |
shuffled_X = X.iloc[:, permutation] |
|
|
210 |
shuffled_Y = Y[:, permutation].reshape((9,m)) |
|
|
211 |
|
|
|
212 |
# Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case. |
|
|
213 |
num_complete_minibatches = math.floor( |
|
|
214 |
m / mini_batch_size) # number of mini batches of size mini_batch_size in your partitionning |
|
|
215 |
for k in range(0, num_complete_minibatches): |
|
|
216 |
|
|
|
217 |
mini_batch_X = shuffled_X.iloc[:, k * mini_batch_size: (k + 1) * mini_batch_size] |
|
|
218 |
mini_batch_Y = shuffled_Y[:, k * mini_batch_size:(k + 1) * mini_batch_size] |
|
|
219 |
|
|
|
220 |
mini_batch = (mini_batch_X, mini_batch_Y) |
|
|
221 |
mini_batches.append(mini_batch) |
|
|
222 |
|
|
|
223 |
# Handling the end case (last mini-batch < mini_batch_size) |
|
|
224 |
if m % mini_batch_size != 0: |
|
|
225 |
|
|
|
226 |
mini_batch_X = shuffled_X.iloc[:, (m - (mini_batch_size * math.floor(m / mini_batch_size))): m] |
|
|
227 |
mini_batch_Y = shuffled_Y[:, (m - (mini_batch_size * math.floor(m / mini_batch_size))): m] |
|
|
228 |
|
|
|
229 |
mini_batch = (mini_batch_X, mini_batch_Y) |
|
|
230 |
mini_batches.append(mini_batch) |
|
|
231 |
|
|
|
232 |
return mini_batches |
|
|
233 |
|