/
SSWE.py
232 lines (172 loc) · 6.78 KB
/
SSWE.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
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
"""
SSWE(Sentiment-Specific Word Embedding) model.
Tang, Duyu, et al.
"Learning Sentiment-Specific Word Embedding for Twitter Sentiment Classification."
ACL (1). 2014.
by chainer v 2.x
"""
import argparse
import collections
import numpy as np
import pickle
from tqdm import tqdm
import chainer
from chainer import cuda, optimizers, initializers
import chainer.links as L
import chainer.functions as F
from chainer.utils import walker_alias
# parser setting
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', '-g', default=-1, type=int,
help='GPU ID(negative value indicates CPU)')
parser.add_argument('--embed', '-em', default=50, type=int,
help='number of embeded size')
parser.add_argument('--unit', '-u', default=20, type=int,
help='number of hidden units')
parser.add_argument('--window', '-w', default=3, type=int,
help='window size')
parser.add_argument('--batchsize', '-b', type=int, default=1000,
help='learning minibatch size')
parser.add_argument('--negative-size', '-ns', default=100, type=int,
help='number of negative samples')
parser.add_argument('--loss-weight', '-lw', default=0.6, type=float,
help="weight of liner conbination for two model's loss")
parser.add_argument('--epoch', '-e', default=5, type=int,
help='number of epochs to learn')
args = parser.parse_args()
# GPU setting
print('====================')
if args.gpu >= 0:
chainer.cuda.get_device_from_id(args.gpu).use()
cuda.check_cuda_available()
xp = cuda.cupy
print(' Use GPU : {}'.format(args.gpu))
else:
xp = np
print(' Use CPU')
# print parameter
print('====================')
print(' Embeded Size : {}'.format(args.embed))
print(' Hidden Unit : {}'.format(args.unit))
print(' Window : {}'.format(args.window))
print(' Epoch : {}'.format(args.epoch))
print(' Minibatch size : {}'.format(args.batchsize))
print(' Sampling Size : {}'.format(args.negative_size))
print(' Conbi loss weight : {}'.format(args.loss_weight))
#=====================
# SSWE model
#=====================
class SSWE(chainer.Chain):
def __init__(self, n_vocab, n_embed, n_units):
super(CandW, self).__init__()
with self.init_scope():
self.embed = L.EmbedID(n_vocab, n_embed, initialW=initializers.Uniform(1. / n_embed))
self.l1 = L.Linear(n_embed*(args.window*2+1), n_units) # n_embed * (window*2 + 1)-> n_units
self.lc = L.Linear(n_units, 1) # n_units -> 1
self.ls = L.Linear(n_units, 2) # n_units -> 1
def __call__(self, context, sentiment, context_pre, context_fol, neg_context):
bs = context.shape[1]
# ----- context ----------------
# ---------- positive ----------
e = self.embed(context)
e = F.concat(e,axis=1)
h = self.l1(e)
th = F.tanh(h)
pout = self.lc(th)
pout = F.tile(pout,(args.negative_size,1))
# ---------- negative ----------
# embedding
pe = self.embed.W.data[context_pre.T]
fe = self.embed.W.data[context_fol.T]
shape = pe.shape
pe = pe.reshape(shape[0],shape[1]*shape[2])
fe = fe.reshape(shape[0],shape[1]*shape[2])
pe = xp.tile(pe,(args.negative_size,1))
fe = xp.tile(fe,(args.negative_size,1))
ne = xp.array(self.embed.W.data[neg_context])
# concatenate
tmp = xp.concatenate((pe,ne),axis=1)
ne_in = xp.concatenate((tmp,fe),axis=1)
# forward
nout = xp.tanh(ne_in.dot(self.l1.W.data.T))
nout = nout.dot(self.lc.W.data.T)
# ---------- hinge loss calculate ----------
loss_c = F.hinge(pout + nout, xp.zeros((args.negative_size*bs,),dtype=np.int32))
loss_c = loss_c * args.negative_size * bs
# ----- sentiment -----
sout = self.ls(th) # 0-> nega, 1-> posi
sentiment[sentiment==1] = -1
sentiment[sentiment==0] = 1
sout = sout[:,0]*sentiment + sout[:,1]*sentiment
loss_s = F.hinge(F.reshape(sout,(bs,1)),xp.zeros((bs,),dtype=np.int32))
loss_s = loss_s + bs
# ----- loss calculate -----
#print(loss_c)
#print(loss_s)
alpha = args.loss_weight
return (1-alpha) * loss_c + alpha * loss_s
#===============
# main process
#===============
# load learning datasets
text_data = np.load('./data/sentence_data.npy')
counts = collections.Counter(text_data)
cs = [counts[w] for w in range(len(counts))]
senti_data = np.load('./data/sentiment_data.npy')
# load vocabulary dict
with open('./data/vocab.dict','rb') as fr:
vocab = pickle.load(fr) # word2id
n_vocab = len(vocab)
print('====================')
print(' vocab size : {}'.format(n_vocab))
print(' train data : {}\n'.format(len(text_data)))
model = SSWE(n_vocab, args.embed, args.unit)
if args.gpu >= 0:
cuda.get_device_from_id(args.gpu).use()
model.to_gpu()
#optimizer = optimizers.Adam()
optimizer = optimizers.AdaGrad()
#optimizer = optimizers.SGD()
optimizer.setup(model)
#optimizer.add_hook(chainer.optimizer.WeightDecay(0.0001))
#====================
# model learning
#====================
sampler = walker_alias.WalkerAlias(np.power(cs,0.75))
ng_size = args.negative_size
n_data = len(text_data)
n_win = args.window
bs = args.batchsize
for epoch in tqdm(range(args.epoch)):
indexes = np.arange(n_win, n_data-n_win)
np.random.shuffle(indexes)
for n in range(0, len(indexes), bs):
index = indexes[n:n+bs]
context = []
sentiment = []
for offset in range(-n_win, n_win + 1):
if offset == 0:
sentiment = senti_data[index + offset]
context.append(text_data[index + offset])
context = np.array(context,dtype=np.int32)
sentiment = np.array(sentiment,dtype=np.int32)
context_pre = context[:n_win]
context_fol = context[-n_win:]
neg_context = np.array(sampler.sample(ng_size * len(index)),dtype=np.int32)
# convert
if args.gpu >= 0:
context = cuda.to_gpu(context)
sentiment = cuda.to_gpu(sentiment)
context_pre = cuda.to_gpu(context_pre)
context_fol = cuda.to_gpu(context_fol)
neg_context = cuda.to_gpu(neg_context)
model.zerograds()
loss = model(context, sentiment, context_pre, context_fol, neg_context)
loss.backward()
optimizer.update()
if args.gpu >= 0:
w = cuda.to_cpu(model.embed.W.data)
else:
w = model.embed.W.data
with open('SSWE.model','wb') as fw:
pickle.dump(w,fw)