wordemb-skip-binary.py 文件源码

python
阅读 32 收藏 0 点赞 0 评论 0

项目:nn4nlp-code 作者: neubig 项目源码 文件源码
def calc_sent_loss(sent):
  # Create a computation graph
  dy.renew_cg()

  W_c = dy.parameter(W_c_p)

  # Get embeddings for the sentence
  emb = [W_w_p[x] for x in sent]

  # Step through the sentence and calculate binary prediction losses
  all_losses = [] 
  for i, my_emb in enumerate(emb):
    scores = dy.logistic(W_c * my_emb)
    pos_words = ([sent[x] if x >= 0 else S for x in range(i-N,i)] +
                 [sent[x] if x < len(sent) else S for x in range(i+1,i+N+1)])
    word_repr = [[float(y) for y in np.binary_repr(x).zfill(nbits)] for x in pos_words]
    word_repr = [dy.inputVector(x) for x in word_repr]
    all_losses.extend([dy.binary_log_loss(scores, x) for x in word_repr])
  return dy.esum(all_losses)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号