File size: 12,937 Bytes
382124c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import numpy as np

"""
################################################################################################
###################  METHODs: SIGMOID and DERIVATIVE OF SIGMOID ################################
################################################################################################
"""

def sigmoid(vec):
    evec = 1 + np.exp(-vec)
    return 1/evec
    
def d_sigmoid(output_of_gate):
    return output_of_gate*(1-output_of_gate)
    
"""
################################################################################################
###################  METHODs: ReLU AND DERIVATE OF ReLU ########################################
################################################################################################
"""

def relu(vec_x):
    relu_x = vec_x.copy()
    relu_x[vec_x < 0] = 0
    return relu_x

def lrelu(vec_x):
    relu_x = vec_x.copy()
    relu_x[vec_x < 0] = relu_x[vec_x < 0]/100
    return relu_x

def d_relu(vec_x):
    d_relu_x = vec_x.copy()
    d_relu_x[vec_x > 0] = 1
    d_relu_x[vec_x <= 0] = 0
    return d_relu_x

def d_lrelu(vec_x):
    d_relu_x = vec_x.copy()
    d_relu_x[vec_x > 0] = 1
    d_relu_x[vec_x <= 0] = 0.01
    return d_relu_x
    
"""
################################################################################################
##################  IMPLEMENTATION OF NEURAL NETWORK  ##########################################
################################################################################################
"""

class NN:
    def __init__(self, input_dimension, hidden_layer_size, outer_relu = True, keep_prob = 1.0):
        # d: Input feature dimension i.e. the dimension of the edge feature vectors
        # n: Hidden layer size
        
        # TODO: Add Bias terms
        self.n = hidden_layer_size
        self.d = input_dimension

        rand_init_range = 1e-2
        self.W = np.random.uniform(-rand_init_range, rand_init_range, (self.n, self.d))
        self.B1 = np.random.uniform(-rand_init_range, rand_init_range, (self.n, 1))

        rand_init_range = 1e-1
        self.U = np.random.uniform(-rand_init_range, rand_init_range, (self.n, 1))
        self.B2 = np.random.uniform(-rand_init_range, rand_init_range, (1, 1))

        # Apply relu or sigmoid at the output layer
        # If relu is applied it will be assumed that log is applied to the 
        #   feature before passing it to the network
        # Else in case of outer sigmoid 
        #   log is applied after the neural network
        self.outer_relu = outer_relu


        # Learning Rates
        self.etaW = None
        self.etaB1 = None
        
        self.etaU = None
        self.etaB2 = None
        
        self.version = 'h1'
        
        # Dropout
        self.keep_prob = keep_prob
        self.dropout_prob = 1 - keep_prob
        self.r1 = np.ones((input_dimension, 1)) # One hot for input layer
        self.r2 = np.ones(self.B1.shape) # one hot for hidden layer
        
        self.training_time = True

    def new_dropout(self):
        self.r1 = np.random.binomial(1, self.keep_prob, size=self.r1.shape)
        self.r2 = np.random.binomial(1, self.keep_prob, size=self.r2.shape)
    def ForTraining(self):
        self.training_time = True
    def ForTesting(self):
        self.training_time = False
    def Forward_Prop(self, x):
        if self.training_time:
            z2 = np.matmul(self.W, x*self.r1) + self.B1
            a2 = lrelu(z2)*self.r2
            o = np.matmul(self.U.transpose(), a2) + self.B2
        else:
            z2 = np.matmul(self.keep_prob*self.W, x) + self.B1
            a2 = lrelu(z2)
            o = np.matmul(self.keep_prob*self.U.transpose(), a2) + self.B2
            
        if self.outer_relu:
            # s = relu(o)
            s = o
        else:
            raise Exception('Support for Non-Outer_Relu removed')
            s = sigmoid(o)
            
        return (z2, a2, s)
    
    '''
    def Forward_Prop(self, x):
        z2 = np.matmul(self.keep_prob*self.W, x) + self.B1
        a2 = lrelu(z2)
        o = np.matmul(self.keep_prob*self.U.transpose(), a2) + self.B2
        if self.outer_relu:
            # s = relu(o)
            s = o
        else:
            raise Exception('Support for Non-Outer_Relu removed')
            s = sigmoid(o)
        return (z2, a2, s)
    '''
    def Get_Energy(self, x):
        # print("problem arises now") 
        x=x[0:1500]
        # numpy.shape(self.W)
        # numpy.shape(x)
        z2 = np.matmul(self.W, x) + self.B1
        # print(len(x))
        a2 = lrelu(z2)
        o = np.matmul(self.U.transpose(), a2) + self.B2
        if self.outer_relu:
            # s = relu(o)
            s = o
        else:
            raise Exception('Support for Non-Outer_Relu removed')
            s = sigmoid(o)
        return s
    
    # Back_Propagate gradient of Loss, L:  Assuming S is the direct output of the network
    def Back_Prop(self, dLdOut, nodeLen, featVMat, _debug = True):
        N = nodeLen
        dLdU = np.zeros(self.U.shape)
        dLdB2 = np.zeros(self.B2.shape)

        dLdW = np.zeros(self.W.shape)
        dLdB1 = np.zeros(self.B1.shape)

        if not self.outer_relu:
            raise Exception('Support for Non-Outer_Relu removed')
            return
        else:
            etaW = self.etaW
            etaB1 = self.etaB1
            
            etaU = self.etaU
            etaB2 = self.etaB2

            if (etaW is None) or (etaB1 is None) or (etaU is None) or (etaB2 is None):
                raise Exception('Learning Rates Not Set...')
            
            batch_size = 0
            for i in range(N):
                for j in range(N):
                    if dLdOut[i, j] != 0 and (featVMat[i][j] is not None):
                        batch_size += 1
                        x = featVMat[i][j][0:1500]
                        (z2, a2, s) = self.Forward_Prop(x)
                        # print(a2.transpose())
                        # print('o')
                        # print(np.matmul(self.U.transpose(), a2))
                        
                        dLdU += dLdOut[i, j]*a2
                        
                        dLdB2 += dLdOut[i, j]

                        dRelu = d_lrelu(z2)
                        dLdW += (dLdOut[i, j])*np.matmul((self.U*dRelu), (x*self.r1).transpose())

                        dLdB1 += dLdOut[i, j]*np.matmul(self.U.transpose(), dRelu)

            if batch_size > 0:
                delW = etaW*dLdW/(batch_size)
                delU = etaU*dLdU/(batch_size)
                delB1 = etaB1*dLdB1/batch_size
                delB2 = etaB2*dLdB2/batch_size
                if _debug:
                    print('Max(delW): %10.6f\tMax(delU): %10.6f'%(np.max(np.abs(delW)), np.max(np.abs(delU))))
                self.W -= delW
                self.B1 -= delB1

                self.U -= delU
                self.B2 -= delB2

        
class NN_2:
    def __init__(self, input_dimension, hidden_layer_1_size, hidden_layer_2_size = None, outer_relu = True):
        # d: Input feature dimension i.e. the dimension of the edge feature vectors
        # n: Hidden layer size
        
        if hidden_layer_2_size is None:
            hidden_layer_2_size = hidden_layer_1_size
        
        # TODO: Add Bias terms
        self.h1 = hidden_layer_1_size
        self.h2 = hidden_layer_2_size
        self.d = input_dimension

        rand_init_range = 1e-2
        self.W1 = np.random.uniform(-rand_init_range, rand_init_range, (self.h1, self.d))
        self.B1 = np.random.uniform(-rand_init_range, rand_init_range, (self.h1, 1))
        self.W2 = np.random.uniform(-rand_init_range, rand_init_range, (self.h2, self.h1))
        self.B2 = np.random.uniform(-rand_init_range, rand_init_range, (self.h2, 1))

        rand_init_range = 1e-1
        self.U = np.random.uniform(-rand_init_range, rand_init_range, (self.h2, 1))
        self.B3 = np.random.uniform(-rand_init_range, rand_init_range, (1, 1))

        # Apply relu or sigmoid at the output layer
        # If relu is applied it will be assumed that log is applied to the 
        #   feature before passing it to the network
        # Else in case of outer sigmoid 
        #   log is applied after the neural network
        self.outer_relu = outer_relu


        # Learning Rates
        self.etaW1 = None
        self.etaB1 = None
        self.etaW2 = None
        self.etaB2 = None
        
        self.etaU = None
        self.etaB3 = None
        
        self.version = 'h2'

    def Forward_Prop(self, x):
        z2 = np.matmul(self.W1, x) + self.B1
        a2 = lrelu(z2)
        
        z3 = np.matmul(self.W2, a2) + self.B2
        a3 = lrelu(z3)
        
        o = np.matmul(self.U.transpose(), a3) + self.B3
        if self.outer_relu:
            # s = relu(o)
            s = o
        else:
            raise Exception('Support for Non-Outer_Relu removed')
            s = sigmoid(o)
        return (z3, a3, z2, a2, s)
    def Get_Energy(self, x):
        z2 = np.matmul(self.W1, x) + self.B1
        a2 = lrelu(z2)
        
        z3 = np.matmul(self.W2, a2) + self.B2
        a3 = lrelu(z3)
        
        o = np.matmul(self.U.transpose(), a3) + self.B3
        if self.outer_relu:
            # s = relu(o)
            s = o
        else:
            raise Exception('Support for Non-Outer_Relu removed')
            s = sigmoid(o)
        return s
    
    # Back_Propagate gradient of Loss, L:  Assuming S is the direct output of the network
    def Back_Prop(self, dLdOut, nodeLen, featVMat, _debug = True):
        N = nodeLen
        
        dLdU = np.zeros(self.U.shape)
        dLdB3 = np.zeros(self.B3.shape)

        dLdW2 = np.zeros(self.W2.shape)
        dLdB2 = np.zeros(self.B2.shape)

        dLdW1 = np.zeros(self.W1.shape)
        dLdB1 = np.zeros(self.B1.shape)

        
        if not self.outer_relu:
            raise Exception('Support for Non-Outer_Relu removed')
            return
        else:
            etaW1 = self.etaW1
            etaB1 = self.etaB1
            
            etaW2 = self.etaW2
            etaB2 = self.etaB2
            
            etaU = self.etaU
            etaB3 = self.etaB3

            if (etaW1 is None) or (etaB1 is None) or (etaW2 is None) or (etaB2 is None) or (etaU is None) or (etaB3 is None):
                raise Exception('Learning Rates Not Set...')
            
            batch_size = 0
            for i in range(N):
                for j in range(N):
                    if dLdOut[i, j] != 0 and (featVMat[i][j] is not None):
                        batch_size += 1
                        (z3, a3, z2, a2, s) = self.Forward_Prop(featVMat[i][j])
                        # print(a2.transpose())
                        # print('o')
                        # print(np.matmul(self.U.transpose(), a2))
                        
                        dLdU += dLdOut[i, j]*a3
                        
                        dLdB3 += dLdOut[i, j]

                        dRelu_z3 = d_lrelu(z3)
                        
                        dLdW2 += (dLdOut[i, j])*np.matmul((self.U*dRelu_z3), a2.transpose())

                        dLdB2 += dLdOut[i, j]*self.U*dRelu_z3
                        
                        dRelu_z2 = d_lrelu(z2)
                        
                        dLdW1 += (dLdOut[i, j])*np.matmul(np.matmul(self.W2.transpose(), self.U*dRelu_z3)*dRelu_z2, featVMat[i][j].transpose())

                        dLdB1 += (dLdOut[i, j])*np.matmul(self.W2.transpose(), self.U*dRelu_z3)*dRelu_z2
                        
                        
                        # for k in range(self.n):
                        #     if dRelu[k] != 0:
                        #         dLdW[k, :, None] += (dLdOut[i, j])*self.U[k]*dRelu[k]*(featVMat[i][j])
            # print('dlDW:')
            # print(dLdW/(batch_size))
            # print('dlDU:')
            # print(dLdU/(batch_size))
            # print('Batch size: ', batch_size)
            if batch_size > 0:
                delW1 = etaW1*dLdW1/(batch_size)
                delW2 = etaW1*dLdW2/(batch_size)
                delU = etaU*dLdU/(batch_size)
                delB1 = etaB1*dLdB1/batch_size
                delB2 = etaB2*dLdB2/batch_size
                delB3 = etaB2*dLdB3/batch_size
                if _debug:
                    print('Max(delW2): %10.6f\tMax(delW1): %10.6f\tMax(delU): %10.6f'%(np.max(np.abs(delW2)), np.max(np.abs(delW1)), np.max(np.abs(delU))))
                
                # Layer 1
                self.W1 -= delW1
                self.B1 -= delB1
                
                # Layer 2
                self.B2 -= delB2
                self.W2 -= delW2
                
                # Layer 3
                self.U -= delU
                self.B3 -= delB3