Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions scripts/word_embeddings/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ def hybrid_forward(self, F, center, context, center_words):
# center - context pairs
emb_center = self.embedding(center).expand_dims(1)
emb_context = self.embedding_out(context).expand_dims(2)
pred_pos = F.batch_dot(emb_center, emb_context).squeeze(axis=None)
pred_pos = F.batch_dot(emb_center, emb_context).squeeze()
loss_pos = (F.relu(pred_pos) - pred_pos + F.Activation(
-F.abs(pred_pos), act_type='softrelu')) / (mask.sum(axis=1) + 1)

# center - negatives pairs
emb_negatives = self.embedding_out(negatives).reshape(
(-1, self._kwargs['num_negatives'],
self._kwargs['output_dim'])).swapaxes(1, 2)
pred_neg = F.batch_dot(emb_center, emb_negatives).squeeze(axis=None)
pred_neg = F.batch_dot(emb_center, emb_negatives).squeeze()
mask = mask.reshape((-1, self._kwargs['num_negatives']))
loss_neg = (F.relu(pred_neg) + F.Activation(
-F.abs(pred_neg), act_type='softrelu')) * mask
Expand Down Expand Up @@ -180,15 +180,15 @@ def hybrid_forward(self, F, center, context):
# context - center samples
emb_context = self.embedding(context).expand_dims(1)
emb_center = self.embedding_out(center).expand_dims(2)
pred_pos = F.batch_dot(emb_context, emb_center).squeeze(axis=None)
pred_pos = F.batch_dot(emb_context, emb_center).squeeze()
loss_pos = (F.relu(pred_pos) - pred_pos + F.Activation(
-F.abs(pred_pos), act_type='softrelu')) / (mask.sum(axis=1) + 1)

# context - negatives samples
emb_negatives = self.embedding_out(negatives).reshape(
(-1, self._kwargs['num_negatives'],
self._kwargs['output_dim'])).swapaxes(1, 2)
pred_neg = F.batch_dot(emb_context, emb_negatives).squeeze(axis=None)
pred_neg = F.batch_dot(emb_context, emb_negatives).squeeze()
mask = mask.reshape((-1, self._kwargs['num_negatives']))
loss_neg = (F.relu(pred_neg) + F.Activation(
-F.abs(pred_neg), act_type='softrelu')) * mask
Expand Down
12 changes: 6 additions & 6 deletions scripts/word_embeddings/train_glove.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ def hybrid_forward(self, F, row, col, counts):
emb_in = F.Dropout(emb_in, p=self._dropout)
emb_out = F.Dropout(emb_out, p=self._dropout)

bias_in = self.source_bias(row).squeeze(axis=None)
bias_out = self.context_bias(col).squeeze(axis=None)
bias_in = self.source_bias(row).squeeze()
bias_out = self.context_bias(col).squeeze()
dot = F.batch_dot(emb_in.expand_dims(1),
emb_out.expand_dims(2)).squeeze(axis=None)
tmp = dot + bias_in + bias_out - F.log(counts).squeeze(axis=None)
emb_out.expand_dims(2)).squeeze()
tmp = dot + bias_in + bias_out - F.log(counts).squeeze()
weight = F.clip(((counts / self._x_max)**self._alpha), a_min=0,
a_max=1).squeeze(axis=None)
a_max=1).squeeze()
loss = weight * F.square(tmp)
return loss

Expand Down Expand Up @@ -264,7 +264,7 @@ def __getitem__(self, tokens):

if squeeze:
assert len(vecs) == 1
return vecs[0].squeeze(axis=None)
return vecs[0].squeeze()
else:
return vecs

Expand Down