File size: 7,341 Bytes
5e22f01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
from PRNN_utils import tags2sentence, check_conditions

class PRNNSigmoid():

    def __init__(self, seed=15):
        np.random.seed(seed)      # Set the seed
        self.params = np.random.normal(0, 1, size=10)
        self.w = np.random.normal(0, 1, size=1)
    
    def step(self, x, threshold=0.5):
        out = (x>threshold).astype('float')
        return out  

    def sigmoid(self, x, max_val=10):
        
        x = np.clip(x, -max_val, max_val)
        out = 1 / (1 + np.exp(-x))
        return out
    
    def sigmoid_dash(self, x):
        sig_x = self.sigmoid(x)
        out = sig_x*(1-sig_x)
        return out


    def forward(self, x, h):
        '''
        Process x(t) and h(t-1) ie Single pass of RNN
        Parameters:
            x:np.array = [-upper_input_1-hot- -1 -lower_input_1-hot-]
            h:float {0,1}
        Returns:
            out(float): Sigmoid(params_trans x(t) + w h(t-1))
        '''
        c_t = np.dot(self.params, x) + self.w*h     # p_trans x(t) + w h(t-1)
        h_t = self.sigmoid(c_t)

        return c_t, h_t
    
    def process_seq(self, sequence, h_0=0.0):
        """
        Process the whole sequence
        Parameters:
            sequence List[List] 
        Returns:
            list of hidden states
        """
        
        hidden_states = [h_0]
        C_states = [0]  #Assume c(0) is 0
        
        h_tminus1 = h_0
        # Sequentially process the 
        for x_t in sequence:

            c_t, h_t = self.forward(x=x_t, h=h_tminus1)
            hidden_states.append(h_t[0])            # Just extract the numerical value
            C_states.append(c_t[0])
            
            h_tminus1 = h_t
        
        C = np.array(C_states).reshape(-1)
        H = np.array(hidden_states).reshape(-1)
        return C, H
    
    def predict_tags(self, sequence):
        ''''
        Predict Tags {0,1} using step function
        The op is [[y_cap(1), y_cap(2), .... y_cap(T)]]
        Each y_cap(i) is either 0 or 1
        '''

        C, H = self.process_seq(sequence)
        out = self.step(H).reshape(-1)[1:]
        return out
    

    def process_batch(self, batch):
        """
        Processes a batch of sequences throught the model(rnn)
        Parameters:
            batch (dtaframe) : containint the field <pos_tags>
        Oututput
            outputs list[numpy_array] : Output of each sequence through RNN, hidden state

        """

        H_outputs = []
        C_outputs = []
        for _, row in batch.iterrows():

            x = tags2sentence(row.pos_tags)
            C, H = self.process_seq(x)
            H  = H.reshape(-1)
            C  = C.reshape(-1)

            C_outputs.append(C)
            H_outputs.append(H)

        return C_outputs, H_outputs
    


    def view_params(self):
        '''
        prints perceptron parameters along with names
        '''
        print("PERCEPTRON PARAMETERS")

        print(f"Vcap : {self.params[0]}" , end = ' | ')
        print(f"Vnn : {self.params[1]}" , end = ' | ')
        print(f"Vdt : {self.params[2]}" , end = ' | ')
        print(f"Vjj : {self.params[3]}" , end = ' | ')
        print(f"Vot : {self.params[4]}" )
        print(f"T [Theta] : {self.params[5]}" , end = ' | ')
        print(f"Wnn : {self.params[6]}" , end = ' | ')
        print(f"Wdt : {self.params[7]}" , end = ' | ')
        print(f"Wjj : {self.params[8]}" , end = ' | ')
        print(f"Wot : {self.params[9]}")
        
        print(f"W : {self.w[0]}")



    def set_perfect_params(self):
        '''
        Params are of the form
        params = [Vcap, Vnn, Vdt, Vjj, Vot, [T]Theta, Wnn, Wdt, Wjj, Wot]
        '''
        print("RESETTING TO PERFECT PARAMETERS \n")
        self.params = np.array([1.5, .3, .1, .2, 2.5, 1.2, .3, 1.3, .2, 2.0])
        self.w[0] = 0.1
        self.view_params()



    def gradient_descent_step(self, grad_p, grad_w, lr=0.05):
        '''
        Updates the self. parama and self.w according to the fradient descent rule
        Parameters:
            grad_p : numpy array (10,)
            grad_w : sigle float
        '''
        self.params = self.params - lr*grad_p
        self.w = self.w - lr*grad_w



    def batch_CE_loss(self, batch):
        """
        Processes a batch of sequences and calculates the ReLU loss
        Parameters:
            batch (dtaframe) : containint the field <pos_tags> and <chunk_tags>
        Oututput
            Total Loss Relu 
        """

        total_loss = 0
        for _, row in batch.iterrows():

            sent_pos_tags = row.pos_tags
            X = tags2sentence(sent_pos_tags)
            
            _, H = self.process_seq(X)
            H = H[1:]         # Exclude h(0)

            sent_tags = row.chunk_tags  
            Y = np.array(sent_tags)
            
            loss = -(Y*np.log(H) + (1-Y)*np.log(1-H))
            loss = loss.mean()

            total_loss += loss

        total_loss = total_loss/len(batch)

        return total_loss



    def batch_accuracy(self, batch):

        correct = 0 # Predictions that match
        total = 0   # Total predictions

        for _, row in batch.iterrows():

            sent_pos_tags = row.pos_tags
            x = tags2sentence(sent_pos_tags)

            sent_tags = row.chunk_tags  
            y_target = np.array(sent_tags)

            y_pred = self.predict_tags(x) 
            correct += np.sum(y_pred == y_target)
            total += len(y_pred)
            
        acc = (correct/total)*100       # Accuracy in percentage

        return acc
    

    def batch_sentence_accuracy(self, batch):
        
        match = 0
        
        for _, row in batch.iterrows():

            sent_pos_tags = row.pos_tags
            x = tags2sentence(sent_pos_tags)

            sent_tags = row.chunk_tags  
            y_target = np.array(sent_tags)

            y_pred = self.predict_tags(x) 
            if np.array_equal(y_pred, y_target):
                match +=1

        sent_acc = (match/len(batch))*100

        return sent_acc


    def set_parameter(self, Vcap=1.5, Vnn=.3, Vdt=.1, Vjj=.2, Vot=2.5, T=1.2, Wnn=.3, Wdt=1.3, Wjj=.2, Wot=2.0, W=.10):

        self.params[0] = Vcap
        self.params[1] = Vnn
        self.params[2] = Vdt
        self.params[3] = Vjj
        self.params[4] = Vot
        self.params[5] = T
        self.params[6] = Wnn
        self.params[7] = Wdt
        self.params[8] = Wjj
        self.params[9] = Wot

        self.w[0] = W

    def does_RNN_satisfy_conditions(self):
        """
        Checks whether the RNN satisfies the inequality conditions
        """
        check_conditions(Vcap = np.round(self.params[0],4), 
                         Vnn = np.round(self.params[1],4), 
                         Vdt = np.round(self.params[2],4), 
                         Vjj = np.round(self.params[3],4), 
                         Vot = np.round(self.params[4],4),
                         T = np.round(self.params[5],4), 
                         Wnn = np.round(self.params[6],4), 
                         Wdt = np.round(self.params[7],4), 
                         Wjj = np.round(self.params[8],4), 
                         Wot = np.round(self.params[9],4), 
                         W = np.round(self.w[0],4), verbose=True)