forked from lipiji/hierarchical-encoder-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsent_encoder.py
More file actions
33 lines (28 loc) · 1.2 KB
/
sent_encoder.py
File metadata and controls
33 lines (28 loc) · 1.2 KB
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
#pylint: skip-file
import numpy as np
import theano
import theano.tensor as T
from utils_pg import *
from gru import *
from lstm import *
class SentEncoderLayer(object):
def __init__(self, cell, rng, layer_id, shape, X, mask, is_train = 1, batch_size = 1, p = 0.5):
prefix = "SentEncoder_"
self.in_size, self.out_size = shape
'''
def code(j):
i = mask[:, j].sum() - 1
i = T.cast(i, 'int32')
sent_x = X[i, j * self.in_size : (j + 1) * self.in_size]
return sent_x
sent_X, updates = theano.scan(lambda i: code(i), sequences=[T.arange(mask.shape[1])])
'''
sent_X = T.reshape(X[X.shape[0] - 1, :], (batch_size, self.in_size))
mask = T.reshape(T.ones_like(sent_X)[:,0], (batch_size, 1))
if cell == "gru":
self.encoder = GRULayer(rng, prefix + layer_id, shape, sent_X, mask, is_train, 1, p)
elif cell == "lstm":
self.encoder = LSTMLayer(rng, prefix + layer_id, shape, sent_X, mask, is_train, 1, p)
self.activation = self.encoder.activation[self.encoder.activation.shape[0] - 1,:]
self.sent_encs = sent_X
self.params = self.encoder.params