forked from xiaohan2012/twitter-sent-dnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_embedding.py
120 lines (91 loc) · 2.54 KB
/
test_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
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
import sys
import math
import numpy as np
import theano
import theano.tensor as T
from cnn4nlp import (WordEmbeddingLayer,
ConvFoldingPoolLayer)
from logreg import LogisticRegression
rng = np.random.RandomState(1234)
##### Test Part One ###############
# WordEmbeddingLayer
#################################
EMB_DIM = 6
x = T.imatrix('x') # the vector of word indices
l1 = WordEmbeddingLayer(rng,
x,
10, EMB_DIM)
print "######## All Embeddings ########"
print l1.embeddings.get_value()
print l1.embeddings.get_value().shape
get_embedding = theano.function(
inputs = [x],
outputs = l1.output,
# mode = "DebugMode"
)
print "######## Selected Embeddings ########"
selected_embedding = get_embedding(
np.array([
[1,3,5],
[2,0,7]
],
dtype = np.int32)
)
print selected_embedding
print selected_embedding.shape
assert selected_embedding.shape == (2, 1, EMB_DIM, 3)
##### Test Part Two ###############
# ConvFoldingPoolLayer
#################################
print "############# ConvFoldingPoolLayer ##############"
k = 2
feat_map_n = 2
l2 = ConvFoldingPoolLayer(rng,
input = l1.output,
filter_shape = (feat_map_n, 1, 1, 2), # two feature map, height: 1, width: 2,
k = k
)
l2_output = theano.function(
inputs = [x],
outputs = l2.output,
)
# TODO:
# check the dimension
# input: 1 x 1 x 6 x 4
out = l2_output(
np.array([[1, 3, 4, 5]], dtype = np.int32)
)
print out
print out.shape
expected = (1, feat_map_n, EMB_DIM / 2, k)
assert out.shape == expected, "%r != %r" %(out.shape, expected)
##### Test Part Three ###############
# LogisticRegressionLayer
#################################
print "############# LogisticRegressionLayer ##############"
l3 = LogisticRegression(
rng,
input = l2.output.flatten(2),
n_in = feat_map_n * k * EMB_DIM / 2, # we fold once, so divide by 2
n_out = 5 # five sentiment level
)
print "n_in = %d" %(2 * 2 * math.ceil(EMB_DIM / 2.))
y = T.ivector('y') # the sentence sentiment label
p_y_given_x = theano.function(
inputs = [x],
outputs = l3.p_y_given_x,
mode = "DebugMode"
)
print "p_y_given_x = "
print p_y_given_x(
np.array([[1, 3, 4, 5], [0, 1, 4 ,7]], dtype = np.int32)
)
cost = theano.function(
inputs = [x, y],
outputs = l3.nnl(y),
mode = "DebugMode"
)
print "cost:\n", cost(
np.array([[1, 3, 4, 5], [0, 1, 4 ,7]], dtype = np.int32),
np.array([1, 2], dtype = np.int32)
)