def predict_proba(self, inp, gpu=False, **kwargs): """ Compute the probability assigned by the model to an input sequence. In the future this should use pack_padded_sequence to run prediction on multiple input sentences at once. Parameters: ----------- inp: list of ints representing the input sequence gpu: bool, whether to use the gpu kwargs: other model parameters Returns: -------- float representing the log probability of the input sequence """ if self.training: logging.warn("Generating in training modus!") if isinstance(inp, list): inp = torch.LongTensor(inp) if gpu: inp = inp.cuda() outs, *_ = self(Variable(inp, volatile=True), **kwargs) log_probs = u.select_cols(outs[:-1], inp[1:]) return log_probs.sum().data[0] / len(log_probs)
def predict_proba(self, inp, gpu=False, **kwargs): if self.training: logging.warn("Generating in training modus!") inp_vec = Variable(torch.LongTensor([inp]), volatile=True) if gpu: inp_vec.cuda() outs, hidden, _ = self(inp_vec, **kwargs) outs = u.select_cols(outs, inp).sum() return outs.data[0] / len(inp)
def predict_proba(self, inp, gpu=False, **kwargs): if self.training: logging.warn("Generating in training modus!") inp_vec = Variable(torch.LongTensor([inp]), volatile=True) if gpu: inp_vec.cuda() outs, _, _ = self(inp_vec, **kwargs) log_probs = u.select_cols(outs[:-1], inp[1:]) return log_probs.sum().data[0] / len(log_probs)
def sample(self, temperature=1., seed_texts=None, max_seq_len=25, batch_size=10, bos=False, ignore_eos=False, **kwargs): scores, prev, hidden = self._seed( seed_texts, batch_size, 'sample', bos, temperature=temperature) batch = prev.size(1) hyps = [prev.squeeze().data.tolist()] finished = np.array([False] * batch) for _ in range(max_seq_len): outs, hidden, _ = self.model(prev, hidden=hidden, **kwargs) prev = outs.div(temperature).exp_().multinomial().t() score = u.select_cols(outs.data.cpu(), prev.squeeze().data.cpu()) hyps.append(prev.squeeze().data.tolist()) if self.eos is not None and not ignore_eos: mask = (prev.squeeze().data == self.eos).cpu().numpy() == 1 finished[mask] = True if all(finished == True): # nopep8 break # 0-mask scores for finished batch examples score[torch.ByteTensor(finished.tolist())] = 0 scores += score return scores.tolist(), list(zip(*hyps))
def sample(self, temperature=1., seed_texts=None, max_seq_len=25, batch_size=10, ignore_eos=False, bos=False, eos=False, **kwargs): """ Generate a sequence multinomially sampling from the output distribution at each generation step. The output distribution can be tweaked by the input parameter `temperature`. """ prev, hidden = self._seed(seed_texts, batch_size, bos, eos, temperature=temperature) batch_size = prev.size(1) # not equal to input if seed_texts hyps, scores = [], 0 mask = torch.zeros(batch_size).long() + 1 for _ in range(max_seq_len): outs, hidden, _ = self.model(prev, hidden=hidden, **kwargs) prev = outs.div_(temperature).exp().multinomial(1).t() score = u.select_cols(outs.data.cpu(), prev.squeeze().data.cpu()) hyps.append(prev.squeeze().data.tolist()) if self.eos is not None and not ignore_eos: mask = mask * (prev.squeeze().data.cpu() != self.eos).long() if mask.sum() == 0: break # 0-mask scores for finished batch examples score[mask == 0] = 0 scores += score return scores.tolist(), list(zip(*hyps))
def sample(out): prev = out.div(temperature).exp_().multinomial().t() score = u.select_cols(out.data.cpu(), prev.squeeze().data.cpu()) return prev, score