def solve(self, x_seq, pos, neg, train=True, variablize=False, onebyone=True):
if variablize:# If arguments are just arrays (not variables), make them variables
x_seq = [chainer.Variable(x, volatile=not train) for x in x_seq]
x_seq = [F.dropout(x, ratio=self.dropout_ratio, train=train) for x in x_seq]
pos = self.act1(self.W_candidate(
F.dropout(chainer.Variable(pos, volatile=not train),
ratio=self.dropout_ratio, train=train)))
neg = self.act1(self.W_candidate(
F.dropout(chainer.Variable(neg, volatile=not train),
ratio=self.dropout_ratio, train=train)))
if onebyone and train:
target_x_seq = [self.act1(self.W_candidate(x)) for x in x_seq[:4]]# 1,2,3,4,5-th targets
onebyone_loss = 0.
self.LSTM.reset_state()
for i, x in enumerate(x_seq):
h = self.LSTM( F.dropout(x, ratio=self.dropout_ratio, train=train) )
if onebyone and train and target_x_seq[i+1:]:
pos_score, neg_score = self.calculate_score(h, target_x_seq[i+1:], neg,
multipos=True)
onebyone_loss += F.relu( self.margin - pos_score + neg_score )
pos_score, neg_score = self.calculate_score(h, pos, neg)
accum_loss = F.relu( self.margin - pos_score + neg_score )
TorFs = sum(accum_loss.data < self.margin)
if onebyone and train:
return F.sum(accum_loss) + F.sum(onebyone_loss), TorFs
else:
return F.sum(accum_loss), TorFs
评论列表
文章目录