forked from xiaohan2012/twitter-sent-dnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_numpy_embedding.py
37 lines (26 loc) · 1.16 KB
/
test_numpy_embedding.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
import theano
import numpy as np
from dcnn import WordEmbeddingLayer
from dcnn_train import WordEmbeddingLayer as TheanoWordEmbeddingLayer
from test_util import assert_matrix_eq
########### NUMPY ###########
vocab_size, embed_dm = 10, 5
embeddings = np.random.rand(vocab_size, embed_dm)
sents = np.asarray(np.random.randint(10, size = (3, 6)),
dtype = np.int32)
np_l = WordEmbeddingLayer(embeddings)
actual = np_l.output(sents)
########### THEANO ###########
x_symbol = theano.tensor.imatrix('x') # the word indices matrix
th_l = TheanoWordEmbeddingLayer(rng = np.random.RandomState(1234),
input = x_symbol,
vocab_size = vocab_size,
embed_dm = embed_dm,
embeddings = theano.shared(value = embeddings,
name = "embeddings"
)
)
f = theano.function(inputs = [x_symbol],
outputs = th_l.output)
expected = f(sents)
assert_matrix_eq(actual, expected, "Embedding")