我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用torch.LongTensor()。
def read(self, inpt, lang_h, ctx_h, prefix_token="THEM:"): """Reads a given utterance.""" # inpt contains the pronounced utterance # add a "THEM:" token to the start of the message prefix = Variable(torch.LongTensor(1)) prefix.data.fill_(self.word_dict.get_idx(prefix_token)) inpt = torch.cat([self.to_device(prefix), inpt]) # embed words inpt_emb = self.word_encoder(inpt) # append the context embedding to every input word embedding ctx_h_rep = ctx_h.expand(inpt_emb.size(0), ctx_h.size(1), ctx_h.size(2)) inpt_emb = torch.cat([inpt_emb, ctx_h_rep], 2) # finally read in the words out, lang_h = self.reader(inpt_emb, lang_h) return out, lang_h
def __call__(self, x): """ Args: x (FloatTensor/LongTensor or ndarray) Returns: x_mu (LongTensor or ndarray) """ mu = self.qc - 1. if isinstance(x, np.ndarray): x_mu = np.sign(x) * np.log1p(mu * np.abs(x)) / np.log1p(mu) x_mu = ((x_mu + 1) / 2 * mu + 0.5).astype(int) elif isinstance(x, (torch.Tensor, torch.LongTensor)): if isinstance(x, torch.LongTensor): x = x.float() mu = torch.FloatTensor([mu]) x_mu = torch.sign(x) * torch.log1p(mu * torch.abs(x)) / torch.log1p(mu) x_mu = ((x_mu + 1) / 2 * mu + 0.5).long() return x_mu
def forward(self, x, lengths, hidden): # Basket Encoding ub_seqs = [] # users' basket sequence for user in x: # x shape (batch of user, time_step, indice of product) nested lists embed_baskets = [] for basket in user: basket = torch.LongTensor(basket).resize_(1, len(basket)) basket = basket.cuda() if self.config.cuda else basket # use cuda for acceleration basket = self.encode(torch.autograd.Variable(basket)) # shape: 1, len(basket), embedding_dim embed_baskets.append(self.pool(basket, dim = 1)) # concat current user's all baskets and append it to users' basket sequence ub_seqs.append(torch.cat(embed_baskets, 1)) # shape: 1, num_basket, embedding_dim # Input for rnn ub_seqs = torch.cat(ub_seqs, 0).cuda() if self.config.cuda else torch.cat(ub_seqs, 0) # shape: batch_size, max_len, embedding_dim packed_ub_seqs = torch.nn.utils.rnn.pack_padded_sequence(ub_seqs, lengths, batch_first=True) # packed sequence as required by pytorch # RNN output, h_u = self.rnn(packed_ub_seqs, hidden) dynamic_user, _ = torch.nn.utils.rnn.pad_packed_sequence(output, batch_first=True) # shape: batch_size, max_len, embedding_dim return dynamic_user, h_u
def reorder_bpr_loss(re_x, his_x, dynamic_user, item_embedding, config): ''' loss function for reorder prediction re_x padded reorder baskets his_x padded history bought items ''' nll = 0 ub_seqs = [] for u, h, du in zip(re_x, his_x, dynamic_user): du_p_product = torch.mm(du, item_embedding.t()) # shape: max_len, num_item nll_u = [] # nll for user for t, basket_t in enumerate(u): if basket_t[0] != 0: pos_idx = torch.cuda.LongTensor(basket_t) if config.cuda else torch.LongTensor(basket_t) # Sample negative products neg = [random.choice(h[t]) for _ in range(len(basket_t))] # replacement # neg = random.sample(range(1, config.num_product), len(basket_t)) # without replacement neg_idx = torch.cuda.LongTensor(neg) if config.cuda else torch.LongTensor(neg) # Score p(u, t, v > v') score = du_p_product[t - 1][pos_idx] - du_p_product[t - 1][neg_idx] # Average Negative log likelihood for basket_t nll_u.append(- torch.mean(torch.nn.LogSigmoid()(score))) nll += torch.mean(torch.cat(nll_u)) return nll
def package(data, volatile=False): """Package data for training / evaluation.""" data = map(lambda x: json.loads(x), data) dat = map(lambda x: map(lambda y: dictionary.word2idx[y], x['text']), data) maxlen = 0 for item in dat: maxlen = max(maxlen, len(item)) targets = map(lambda x: x['label'], data) maxlen = min(maxlen, 500) for i in range(len(data)): if maxlen < len(dat[i]): dat[i] = dat[i][:maxlen] else: for j in range(maxlen - len(dat[i])): dat[i].append(dictionary.word2idx['<pad>']) dat = Variable(torch.LongTensor(dat), volatile=volatile) targets = Variable(torch.LongTensor(targets), volatile=volatile) return dat.t(), targets
def tokenize(self, path): """Tokenizes a text file.""" assert os.path.exists(path) # Add words to the dictionary with open(path, 'r') as f: tokens = 0 for line in f: words = line.split() + ['<eos>'] tokens += len(words) for word in words: self.dictionary.add_word(word) # Tokenize file content with open(path, 'r') as f: ids = torch.LongTensor(tokens) token = 0 for line in f: words = line.split() + ['<eos>'] for word in words: ids[token] = self.dictionary.word2idx[word] token += 1 return ids
def Saliency_map(image,model,preprocess,ground_truth,use_gpu=False,method=util.GradType.GUIDED): vis_param_dict['method'] = method img_tensor = preprocess(image) img_tensor.unsqueeze_(0) if use_gpu: img_tensor=img_tensor.cuda() input = Variable(img_tensor,requires_grad=True) if input.grad is not None: input.grad.data.zero_() model.zero_grad() output = model(input) ind=torch.LongTensor(1) if(isinstance(ground_truth,np.int64)): ground_truth=np.asscalar(ground_truth) ind[0]=ground_truth ind=Variable(ind) energy=output[0,ground_truth] energy.backward() grad=input.grad if use_gpu: return np.abs(grad.data.cpu().numpy()[0]).max(axis=0) return np.abs(grad.data.numpy()[0]).max(axis=0)
def default_collate(batch): "Puts each data field into a tensor with outer dimension batch size" if torch.is_tensor(batch[0]): return torch.cat([t.view(1, *t.size()) for t in batch], 0) elif isinstance(batch[0], int): return torch.LongTensor(batch) elif isinstance(batch[0], float): return torch.DoubleTensor(batch) elif isinstance(batch[0], str): return batch elif isinstance(batch[0], collections.Iterable): # if each batch element is not a tensor, then it should be a tuple # of tensors; in that case we collate each element in the tuple transposed = zip(*batch) return [default_collate(samples) for samples in transposed] raise TypeError(("batch must contain tensors, numbers, or lists; found {}" .format(type(batch[0]))))
def __init__(self, nIndex, nOutput, paddingValue=-1, maxNorm=None, normType=None): super(LookupTable, self).__init__() self.weight = torch.Tensor(nIndex, nOutput) self.gradWeight = torch.Tensor(nIndex, nOutput).zero_() self.paddingValue = paddingValue self.maxNorm = maxNorm self.normType = normType self.shouldScaleGradByFreq = False self._gradOutput = None self._sorted = None self._indices = None self._count = torch.IntTensor() self._input = torch.LongTensor() self.reset()
def type(self, type=None, tensorCache=None): if not type: return self._type super(LookupTable, self).type(type, tensorCache) if type == 'torch.cuda.FloatTensor': # CUDA uses _sorted and _indices temporary tensors self._sorted = torch.cuda.LongTensor() self._indices = torch.cuda.LongTensor() self._count = torch.cuda.LongTensor() self._input = torch.cuda.LongTensor() else: # self._count and self._input should only be converted if using Cuda self._count = torch.IntTensor() self._input = torch.LongTensor() return self
def test_Index(self): net = nn.Index(0) # test 1D input = [torch.Tensor((10, 20, 30)), torch.LongTensor((0, 1, 1, 2))] output = net.forward(input) self.assertEqual(output, torch.Tensor((10, 20, 20, 30))) gradOutput = torch.Tensor((1, 1, 1, 3)) gradInput = net.backward(input, gradOutput) self.assertEqual(gradInput[0], torch.Tensor((1, 2, 3))) # test 2D input = [torch.Tensor(((10, 20), (30, 40))), torch.LongTensor((0, 0))] output = net.forward(input) self.assertEqual(output, torch.Tensor(((10, 20), (10, 20)))) gradOutput = torch.Tensor(((1, 2), (1, 2))) gradInput = net.backward(input, gradOutput) self.assertEqual(gradInput[0], torch.Tensor(((2, 4), (0, 0)))) # Check that these don't raise errors net.__repr__() str(net)
def test_scatter(self): m, n, o = random.randint(10, 20), random.randint(10, 20), random.randint(10, 20) elems_per_row = random.randint(1, 10) dim = random.randrange(3) idx_size = [m, n, o] idx_size[dim] = elems_per_row idx = torch.LongTensor().resize_(*idx_size) self._fill_indices(idx, dim, ([m, n, o])[dim], elems_per_row, m, n, o) src = torch.Tensor().resize_(*idx_size).normal_() actual = torch.zeros(m, n, o).scatter_(dim, idx, src) expected = torch.zeros(m, n, o) for i in range(idx_size[0]): for j in range(idx_size[1]): for k in range(idx_size[2]): ii = [i, j, k] ii[dim] = idx[i,j,k] expected[tuple(ii)] = src[i,j,k] self.assertEqual(actual, expected, 0) idx[0][0][0] = 34 self.assertRaises(RuntimeError, lambda: torch.zeros(m, n, o).scatter_(dim, idx, src))
def test_scatterFill(self): m, n, o = random.randint(10, 20), random.randint(10, 20), random.randint(10, 20) elems_per_row = random.randint(1, 10) dim = random.randrange(3) val = random.random() idx_size = [m, n, o] idx_size[dim] = elems_per_row idx = torch.LongTensor().resize_(*idx_size) self._fill_indices(idx, dim, ([m, n, o])[dim], elems_per_row, m, n, o) actual = torch.zeros(m, n, o).scatter_(dim, idx, val) expected = torch.zeros(m, n, o) for i in range(idx_size[0]): for j in range(idx_size[1]): for k in range(idx_size[2]): ii = [i, j, k] ii[dim] = idx[i,j,k] expected[tuple(ii)] = val self.assertEqual(actual, expected, 0) idx[0][0][0] = 28 self.assertRaises(RuntimeError, lambda: torch.zeros(m, n, o).scatter_(dim, idx, val))
def __getitem__(self, index): AB_path = self.AB_paths[index] AB = Image.open(AB_path).convert('RGB') AB = AB.resize((self.opt.loadSizeX * 2, self.opt.loadSizeY), Image.BICUBIC) AB = self.transform(AB) w_total = AB.size(2) w = int(w_total / 2) h = AB.size(1) w_offset = random.randint(0, max(0, w - self.opt.fineSize - 1)) h_offset = random.randint(0, max(0, h - self.opt.fineSize - 1)) A = AB[:, h_offset:h_offset + self.opt.fineSize, w_offset:w_offset + self.opt.fineSize] B = AB[:, h_offset:h_offset + self.opt.fineSize, w + w_offset:w + w_offset + self.opt.fineSize] if (not self.opt.no_flip) and random.random() < 0.5: idx = [i for i in range(A.size(2) - 1, -1, -1)] idx = torch.LongTensor(idx) A = A.index_select(2, idx) B = B.index_select(2, idx) return {'A': A, 'B': B, 'A_paths': AB_path, 'B_paths': AB_path}
def prepare_split(self, X, y, validation_data=None, validation_split=None): # Preparing validation data assert validation_split or validation_data if validation_data is not None: trainX, trainy = X, y devX, devy = validation_data else: permutation = np.random.permutation(len(X)) trainidx = permutation[int(validation_split*len(X)):] devidx = permutation[0:int(validation_split*len(X))] trainX, trainy = X[trainidx], y[trainidx] devX, devy = X[devidx], y[devidx] if not self.cudaEfficient: trainX = torch.FloatTensor(trainX).cuda() trainy = torch.LongTensor(trainy).cuda() devX = torch.FloatTensor(devX).cuda() devy = torch.LongTensor(devy).cuda() else: trainX = torch.FloatTensor(trainX) trainy = torch.LongTensor(trainy) devX = torch.FloatTensor(devX) devy = torch.LongTensor(devy) return trainX, trainy, devX, devy
def trainepoch(self, X, y, epoch_size=1): self.model.train() for _ in range(self.nepoch, self.nepoch + epoch_size): permutation = np.random.permutation(len(X)) all_costs = [] for i in range(0, len(X), self.batch_size): # forward idx = torch.LongTensor(permutation[i:i + self.batch_size]) if isinstance(X, torch.cuda.FloatTensor): idx = idx.cuda() Xbatch = Variable(X.index_select(0, idx)) ybatch = Variable(y.index_select(0, idx)) if self.cudaEfficient: Xbatch = Xbatch.cuda() ybatch = ybatch.cuda() output = self.model(Xbatch) # loss loss = self.loss_fn(output, ybatch) all_costs.append(loss.data[0]) # backward self.optimizer.zero_grad() loss.backward() # Update parameters self.optimizer.step() self.nepoch += epoch_size
def score(self, devX, devy): self.model.eval() correct = 0 if not isinstance(devX, torch.cuda.FloatTensor) or self.cudaEfficient: devX = torch.FloatTensor(devX).cuda() devy = torch.LongTensor(devy).cuda() for i in range(0, len(devX), self.batch_size): Xbatch = Variable(devX[i:i + self.batch_size], volatile=True) ybatch = Variable(devy[i:i + self.batch_size], volatile=True) if self.cudaEfficient: Xbatch = Xbatch.cuda() ybatch = ybatch.cuda() output = self.model(Xbatch) pred = output.data.max(1)[1] correct += pred.long().eq(ybatch.data.long()).sum() accuracy = 1.0*correct / len(devX) return accuracy
def test_process(self): raw_field = data.RawField() field = data.Field(sequential=True, use_vocab=False, batch_first=True) # Test tensor-like batch data which is accepted by both RawField and Field batch = [[1, 2, 3], [2, 3, 4]] batch_tensor = torch.LongTensor(batch) raw_field_processed = raw_field.process(batch) field_processed = field.process(batch, device=-1, train=False) assert raw_field_processed == batch assert field_processed.data.equal(batch_tensor) # Test non-tensor data which is only accepted by RawField any_obj = [object() for _ in range(5)] raw_field_processed = raw_field.process(any_obj) assert any_obj == raw_field_processed with pytest.raises(TypeError): field.process(any_obj)
def __init__( self, sequential=True, use_vocab=True, init_token=None, eos_token=None, fix_length=None, tensor_type=torch.LongTensor, preprocessing=None, postprocessing=None, lower=False, tokenize=(lambda s: s.split()), include_lengths=False, batch_first=False, pad_token="<pad>", unk_token="<unk>", pad_first=False): self.sequential = sequential self.use_vocab = use_vocab self.init_token = init_token self.eos_token = eos_token self.unk_token = unk_token self.fix_length = fix_length self.tensor_type = tensor_type self.preprocessing = preprocessing self.postprocessing = postprocessing self.lower = lower self.tokenize = get_tokenizer(tokenize) self.include_lengths = include_lengths self.batch_first = batch_first self.pad_token = pad_token if self.sequential else None self.pad_first = pad_first
def resample(self, seed=None): """Resample the dataset. Args: seed (int, optional): Seed for resampling. By default no seed is used. """ if seed is not None: gen = torch.manual_seed(seed) else: gen = torch.default_generator if self.replacement: self.perm = torch.LongTensor(len(self)).random_( len(self.dataset), generator=gen) else: self.perm = torch.randperm( len(self.dataset), generator=gen).narrow(0, 0, len(self))
def testListDataset(self): def identity(x): return x h = [0, 1, 2] d = dataset.ListDataset(elem_list=h, load=identity) self.assertEqual(len(d), 3) self.assertEqual(d[0], 0) t = torch.LongTensor([0, 1, 2]) d = dataset.ListDataset(elem_list=t, load=identity) self.assertEqual(len(d), 3) self.assertEqual(d[0], 0) a = np.asarray([0, 1, 2]) d = dataset.ListDataset(elem_list=a, load=identity) self.assertEqual(len(d), 3) self.assertEqual(d[0], 0)
def test_batched_index_select(self): indices = numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Each element is a vector of it's index. targets = torch.ones([2, 10, 3]).cumsum(1) - 1 # Make the second batch double it's index so they're different. targets[1, :, :] *= 2 indices = Variable(torch.LongTensor(indices)) targets = Variable(targets) selected = util.batched_index_select(targets, indices) assert list(selected.size()) == [2, 2, 2, 3] ones = numpy.ones([3]) numpy.testing.assert_array_equal(selected[0, 0, 0, :].data.numpy(), ones) numpy.testing.assert_array_equal(selected[0, 0, 1, :].data.numpy(), ones * 2) numpy.testing.assert_array_equal(selected[0, 1, 0, :].data.numpy(), ones * 3) numpy.testing.assert_array_equal(selected[0, 1, 1, :].data.numpy(), ones * 4) numpy.testing.assert_array_equal(selected[1, 0, 0, :].data.numpy(), ones * 10) numpy.testing.assert_array_equal(selected[1, 0, 1, :].data.numpy(), ones * 12) numpy.testing.assert_array_equal(selected[1, 1, 0, :].data.numpy(), ones * 14) numpy.testing.assert_array_equal(selected[1, 1, 1, :].data.numpy(), ones * 16)
def test_add_sentence_boundary_token_ids_handles_3D_input(self): tensor = Variable(torch.from_numpy( numpy.array([[[1, 2, 3, 4], [5, 5, 5, 5], [6, 8, 1, 2]], [[4, 3, 2, 1], [8, 7, 6, 5], [0, 0, 0, 0]]]))) mask = ((tensor > 0).sum(dim=-1) > 0).type(torch.LongTensor) bos = Variable(torch.from_numpy(numpy.array([9, 9, 9, 9]))) eos = Variable(torch.from_numpy(numpy.array([10, 10, 10, 10]))) new_tensor, new_mask = util.add_sentence_boundary_token_ids(tensor, mask, bos, eos) expected_new_tensor = numpy.array([[[9, 9, 9, 9], [1, 2, 3, 4], [5, 5, 5, 5], [6, 8, 1, 2], [10, 10, 10, 10]], [[9, 9, 9, 9], [4, 3, 2, 1], [8, 7, 6, 5], [10, 10, 10, 10], [0, 0, 0, 0]]]) assert (new_tensor.data.numpy() == expected_new_tensor).all() assert (new_mask.data.numpy() == ((expected_new_tensor > 0).sum(axis=-1) > 0)).all()
def as_tensor(self, padding_lengths: Dict[str, int], cuda_device: int = -1, for_training: bool = True) -> Dict[str, torch.Tensor]: tensors = {} desired_num_tokens = padding_lengths['num_tokens'] for indexer_name, indexer in self._token_indexers.items(): padded_array = indexer.pad_token_sequence(self._indexed_tokens[indexer_name], desired_num_tokens, padding_lengths) # We use the key of the indexer to recognise what the tensor corresponds to within the # field (i.e. the result of word indexing, or the result of character indexing, for # example). # TODO(mattg): we might someday have a TokenIndexer that needs to use something other # than a LongTensor here, and it's not clear how to signal that. Maybe we'll need to # add a class method to TokenIndexer to tell us the type? But we can worry about that # when there's a compelling use case for it. tensor = Variable(torch.LongTensor(padded_array), volatile=not for_training) tensors[indexer_name] = tensor if cuda_device == -1 else tensor.cuda(cuda_device) return tensors
def get_lengths_from_binary_sequence_mask(mask: torch.Tensor): """ Compute sequence lengths for each batch element in a tensor using a binary mask. Parameters ---------- mask : torch.Tensor, required. A 2D binary mask of shape (batch_size, sequence_length) to calculate the per-batch sequence lengths from. Returns ------- A torch.LongTensor of shape (batch_size,) representing the lengths of the sequences in the batch. """ return mask.long().sum(-1)
def accuracy(self, predicted, ground_truth): """ Utility function for calculating the accuracy of the model. Params ------ - predicted: (torch.FloatTensor) - ground_truth: (torch.LongTensor) Returns ------- - acc: (float) % accuracy. """ predicted = torch.max(predicted, 1)[1] total = len(ground_truth) correct = (predicted == ground_truth).sum() acc = 100 * (correct / total) return acc
def sent2tenosr(self, sentence): max_len = self.args.max_word_len - 2 sentence = normalizeString(sentence) words = [w for w in sentence.strip().split()] if len(words) > max_len: words = words[:max_len] words = [WORD[BOS]] + words + [WORD[EOS]] idx = [self.src_dict[w] if w in self.src_dict else UNK for w in words] idx_data = torch.LongTensor(idx) idx_position = torch.LongTensor([pos_i+1 if w_i != PAD else 0 for pos_i, w_i in enumerate(idx)]) idx_data_tensor = Variable(idx_data.unsqueeze(0), volatile=True) idx_position_tensor = Variable(idx_position.unsqueeze(0), volatile=True) if self.cuda: idx_data_tensor = idx_data_tensor.cuda() idx_position_tensor = idx_position_tensor.cuda() return idx_data_tensor, idx_position_tensor
def __call__(self, x_mu): """ Args: x_mu (FloatTensor/LongTensor or ndarray) Returns: x (FloatTensor or ndarray) """ mu = self.qc - 1. if isinstance(x_mu, np.ndarray): x = ((x_mu) / mu) * 2 - 1. x = np.sign(x) * (np.exp(np.abs(x) * np.log1p(mu)) - 1.) / mu elif isinstance(x_mu, (torch.Tensor, torch.LongTensor)): if isinstance(x_mu, torch.LongTensor): x_mu = x_mu.float() mu = torch.FloatTensor([mu]) x = ((x_mu) / mu) * 2 - 1. x = torch.sign(x) * (torch.exp(torch.abs(x) * torch.log1p(mu)) - 1.) / mu return x
def test_torch(self): try: import torch except ImportError: # pass by default if no torch available return st = SharedTable({'a': torch.FloatTensor([1]), 'b': torch.LongTensor(2)}) assert st['a'][0] == 1.0 assert len(st) == 2 assert 'b' in st del st['b'] assert 'b' not in st assert len(st) == 1 if torch.cuda.is_available(): st = SharedTable({'a': torch.cuda.FloatTensor([1]), 'b': torch.cuda.LongTensor(2)}) assert st['a'][0] == 1.0 assert len(st) == 2 assert 'b' in st del st['b'] assert 'b' not in st assert len(st) == 1
def collate(samples, pad_idx, eos_idx): def merge(key, left_pad, move_eos_to_beginning=False): return LanguagePairDataset.collate_tokens( [s[key] for s in samples], pad_idx, eos_idx, left_pad, move_eos_to_beginning) return { 'id': torch.LongTensor([s['id'].item() for s in samples]), 'src_tokens': merge('source', left_pad=LanguagePairDataset.LEFT_PAD_SOURCE), # we create a shifted version of targets for feeding the previous # output token(s) into the next decoder step 'input_tokens': merge('target', left_pad=LanguagePairDataset.LEFT_PAD_TARGET, move_eos_to_beginning=True), 'target': merge('target', left_pad=LanguagePairDataset.LEFT_PAD_TARGET), 'ntokens': sum(len(s['target']) for s in samples), }
def sample(self): """ Returns a sample which has the same shape as `ps` (or `vs`), except that if ``one_hot=True`` (and no `vs` is specified), the last dimension will have the same size as the number of events. The type of the sample is `numpy.ndarray` if `vs` is a list or a numpy array, else a tensor is returned. :return: sample from the Categorical distribution :rtype: numpy.ndarray or torch.LongTensor """ sample = torch_multinomial(self.ps.data, 1, replacement=True).expand(*self.shape()) sample_one_hot = torch_zeros_like(self.ps.data).scatter_(-1, sample, 1) if self.vs is not None: if isinstance(self.vs, np.ndarray): sample_bool_index = sample_one_hot.cpu().numpy().astype(bool) return self.vs[sample_bool_index].reshape(*self.shape()) else: return self.vs.masked_select(sample_one_hot.byte()) if self.one_hot: return Variable(sample_one_hot) return Variable(sample)
def forward(self, unique_word_chars, unique_word_lengths, sequences_as_uniqs): long_tensor = torch.cuda.LongTensor if torch.cuda.device_count() > 0 else torch.LongTensor embedded_chars = self._embeddings(unique_word_chars.type(long_tensor)) # [N, S, L] conv_out = self._conv(embedded_chars.transpose(1, 2)) # [N, L] conv_mask = misc.mask_for_lengths(unique_word_lengths) conv_out = conv_out + conv_mask.unsqueeze(1) embedded_words = conv_out.max(2)[0] if not isinstance(sequences_as_uniqs, list): sequences_as_uniqs = [sequences_as_uniqs] all_embedded = [] for word_idxs in sequences_as_uniqs: all_embedded.append(functional.embedding( word_idxs.type(long_tensor), embedded_words)) return all_embedded
def generate(self): if self.shuffle: idx = np.random.permutation(np.arange(self.vocab_size)) idx = torch.LongTensor(idx) src = self.src[idx] target = self.target[idx] else: src = self.src target = self.target for i in range(0, self.vocab_size, self.batch_size): b_idx = i e_idx = i + self.batch_size batch_x = src[b_idx:e_idx].contiguous() batch_y = target[b_idx:e_idx].contiguous() batch_x = Variable(batch_x) batch_y = Variable(batch_y) yield batch_x, batch_y
def get_data(self, path, batch_size=20): # Add words to the dictionary with open(path, 'r') as f: tokens = 0 for line in f: words = line.split() + ['<eos>'] tokens += len(words) for word in words: self.dictionary.add_word(word) # Tokenize the file content ids = torch.LongTensor(tokens) token = 0 with open(path, 'r') as f: for line in f: words = line.split() + ['<eos>'] for word in words: ids[token] = self.dictionary.word2idx[word] token += 1 num_batches = ids.size(0) // batch_size ids = ids[:num_batches*batch_size] return ids.view(batch_size, -1)
def feed_forward_generator(self, advantages, num_mini_batch): num_steps, num_processes = self.rewards.size()[0:2] batch_size = num_processes * num_steps mini_batch_size = batch_size // num_mini_batch sampler = BatchSampler(SubsetRandomSampler(range(batch_size)), mini_batch_size, drop_last=False) for indices in sampler: indices = torch.LongTensor(indices) if advantages.is_cuda: indices = indices.cuda() observations_batch = self.observations[:-1].view(-1, *self.observations.size()[2:])[indices] states_batch = self.states[:-1].view(-1, self.states.size(-1))[indices] actions_batch = self.actions.view(-1, self.actions.size(-1))[indices] return_batch = self.returns[:-1].view(-1, 1)[indices] masks_batch = self.masks[:-1].view(-1, 1)[indices] old_action_log_probs_batch = self.action_log_probs.view(-1, 1)[indices] adv_targ = advantages.view(-1, 1)[indices] yield observations_batch, states_batch, actions_batch, \ return_batch, masks_batch, old_action_log_probs_batch, adv_targ
def test(self, dataset): self.model.eval() self.embedding_model.eval() loss = 0 accuracies = torch.zeros(len(dataset)) output_trees = [] outputs = [] for idx in tqdm(range(len(dataset)), desc='Testing epoch '+str(self.epoch)+''): tree, sent, label = dataset[idx] input = Var(sent, volatile=True) target = Var(torch.LongTensor([int(label)]), volatile=True) if self.args.cuda: input = input.cuda() target = target.cuda() emb = F.torch.unsqueeze(self.embedding_model(input),1) output, _, acc, tree = self.model(tree, emb) err = self.criterion(output, target) loss += err.data[0] accuracies[idx] = acc output_trees.append(tree) outputs.append(tree.output_softmax.data.numpy()) # predictions[idx] = torch.dot(indices,torch.exp(output.data.cpu())) return loss/len(dataset), accuracies, outputs, output_trees
def forward(self, input_, length=None, hx=None): if self.batch_first: input_ = input_.transpose(0, 1) max_time, batch_size, _ = input_.size() if length is None: length = Variable(torch.LongTensor([max_time] * batch_size)) if input_.is_cuda: length = length.cuda() if hx is None: hx = Variable(input_.data.new(batch_size, self.hidden_size).zero_()) hx = (hx, hx) h_n = [] c_n = [] layer_output = None for layer in range(self.num_layers): layer_output, (layer_h_n, layer_c_n) = LSTM._forward_rnn( cell=self.cells[layer], input_=input_, length=length, hx=hx) input_ = self.dropout_layer(layer_output) h_n.append(layer_h_n) c_n.append(layer_c_n) output = layer_output h_n = torch.stack(h_n, 0) c_n = torch.stack(c_n, 0) return output, (h_n, c_n)
def forward(self, pos_u, pos_v, neg_u, neg_v): losses = [] emb_u = [] for i in range(len(pos_u)): emb_ui = self.u_embeddings(Variable(torch.LongTensor(pos_u[i]))) emb_u.append(np.sum(emb_ui.data.numpy(), axis=0).tolist()) emb_u = Variable(torch.FloatTensor(emb_u)) emb_v = self.v_embeddings(Variable(torch.LongTensor(pos_v))) score = torch.mul(emb_u, emb_v) score = torch.sum(score, dim=1) score = F.logsigmoid(score) losses.append(sum(score)) neg_emb_u = [] for i in range(len(neg_u)): neg_emb_ui = self.u_embeddings(Variable(torch.LongTensor(neg_u[i]))) neg_emb_u.append(np.sum(neg_emb_ui.data.numpy(), axis=0).tolist()) neg_emb_u = Variable(torch.FloatTensor(neg_emb_u)) neg_emb_v = self.v_embeddings(Variable(torch.LongTensor(neg_v))) neg_score = torch.mul(neg_emb_u, neg_emb_v) neg_score = torch.sum(neg_score, dim=1) neg_score = F.logsigmoid(-1 * neg_score) losses.append(sum(neg_score)) return -1 * sum(losses)
def padding(seqs, pad, batch_first=False): """ :param seqs: tuple of seq_length x dim :return: seq_length x Batch x dim """ lengths = [len(s) for s in seqs] seqs = [torch.Tensor(s) for s in seqs] batch_length = max(lengths) seq_tensor = torch.LongTensor(batch_length, len(seqs)).fill_(pad) for i, s in enumerate(seqs): end_seq = lengths[i] seq_tensor[:end_seq, i].copy_(s[:end_seq]) if batch_first: seq_tensor = seq_tensor.t() return (seq_tensor, lengths)
def _create_collate_fn(self, batch_first=True): def collate(examples, this): if this.split == "train": items, question_ids, questions, questions_char, passages, passages_char, answers_positions, answer_texts, passage_tokenized = zip( *examples) else: items, question_ids, questions, questions_char, passages, passages_char, passage_tokenized = zip(*examples) questions_tensor, question_lengths = padding(questions, this.PAD, batch_first=batch_first) passages_tensor, passage_lengths = padding(passages, this.PAD, batch_first=batch_first) # TODO: implement char level embedding question_document = Documents(questions_tensor, question_lengths) passages_document = Documents(passages_tensor, passage_lengths) if this.split == "train": return question_ids, question_document, passages_document, torch.LongTensor(answers_positions), answer_texts else: return question_ids, question_document, passages_document, passage_tokenized return partial(collate, this=self)
def vectorize_stories(stories, vocab : Vocabulary): X = [] Q = [] C = [] A = [] for s, q, a, c in stories: x = vocab.convert2idx(s) xq = vocab.convert2idx(q) xc = vocab.convert2idx(c) X.append(x) Q.append(xq) C.append(xc) A.append(vocab.getIdx(a)) X = X Q = Q C = C A = torch.LongTensor(A) return X, Q, A, C
def prepare_message(self, target_features, source_features, select_mat, gate_module): feature_data = [] transfer_list = np.where(select_mat > 0) source_indices = Variable(torch.from_numpy(transfer_list[1]).type(torch.LongTensor)).cuda() target_indices = Variable(torch.from_numpy(transfer_list[0]).type(torch.LongTensor)).cuda() source_f = torch.index_select(source_features, 0, source_indices) target_f = torch.index_select(target_features, 0, target_indices) transferred_features = gate_module(target_f, source_f) for f_id in range(target_features.size()[0]): if len(np.where(select_mat[f_id, :] > 0)[0]) > 0: feature_indices = np.where(transfer_list[0] == f_id)[0] indices = Variable(torch.from_numpy(feature_indices).type(torch.LongTensor)).cuda() features = torch.index_select(transferred_features, 0, indices).mean(0).view(-1) feature_data.append(features) else: temp = Variable(torch.zeros(target_features.size()[1:]), requires_grad=True).type(torch.FloatTensor).cuda() feature_data.append(temp) return torch.stack(feature_data, 0)
def eval_batch(dr_model, ub, up, batch_size, is_reordered = False): ''' Using dr_model to predict (u,p) score in batch Parameters: - ub: users' baskets - up: users' history purchases - batch_size ''' # turn on evaluation mode dr_model.eval() is_cuda = dr_model.config.cuda item_embedding = dr_model.encode.weight dr_hidden = dr_model.init_hidden(batch_size) id_u, item_u, score_u, dynam_u = [], [], [], [] start_time = time() num_batchs = ceil(len(ub) / batch_size) for i, x in enumerate(batchify(ub, batch_size, is_reordered)): if is_reordered is True: baskets, lens, uids, r_baskets, h_baskets = x else: baskets, lens, uids = x dynamic_user, _ = dr_model(baskets, lens, dr_hidden) for uid, l, du in zip(uids, lens, dynamic_user): du_latest = du[l - 1].unsqueeze(0) # calculating <u,p> score for all history <u,p> pair history_item = [int(i) for i in up[up.user_id == uid]['product_id'].values[0]] history_item = torch.cuda.LongTensor(history_item) if is_cuda else torch.LongTensor(history_item) score_up = torch.mm(du_latest, item_embedding[history_item].t()).cpu().data.numpy()[0] id_u.append(uid), dynam_u.append(du_latest.cpu().data.numpy()[0]), item_u.append(history_item.cpu().numpy()),score_u.append(score_up) # Logging elapsed = time() - start_time; start_time = time() print('[Predicting]| Batch {:5d} / {:5d} | seconds/batch {:02.02f}'.format(i, num_batchs, elapsed)) return id_u, item_u, score_u, dynam_u
def get_item_embedding(pid, dr_model): ''' get item's embedding pid can be a integer or a torch.cuda.LongTensor ''' if isinstance(pid, torch.cuda.LongTensor) or isinstance(pid, torch.LongTensor): return dr_model.encode.weight[pid] elif isinstance(pid, int): return dr_model.encode.weight[pid].unsqueeze(0) else: print('Unsupported Index Type %s'%type(pid)) return None
def bpr_loss(x, dynamic_user, item_embedding, config): ''' bayesian personalized ranking loss for implicit feedback parameters: - x: batch of users' baskets - dynamic_user: batch of users' dynamic representations - item_embedding: item_embedding matrix - config: model configuration ''' nll = 0 ub_seqs = [] for u,du in zip(x, dynamic_user): du_p_product = torch.mm(du, item_embedding.t()) # shape: max_len, num_item nll_u = [] # nll for user for t, basket_t in enumerate(u): if basket_t[0] != 0 and t != 0: pos_idx = torch.cuda.LongTensor(basket_t) if config.cuda else torch.LongTensor(basket_t) # Sample negative products neg = [random.choice(range(1, config.num_product)) for _ in range(len(basket_t))] # replacement # neg = random.sample(range(1, config.num_product), len(basket_t)) # without replacement neg_idx = torch.cuda.LongTensor(neg) if config.cuda else torch.LongTensor(neg) # Score p(u, t, v > v') score = du_p_product[t - 1][pos_idx] - du_p_product[t - 1][neg_idx] #Average Negative log likelihood for basket_t nll_u.append(- torch.mean(torch.nn.LogSigmoid()(score))) nll += torch.mean(torch.cat(nll_u)) return nll
def checkOneHot(self): v = torch.LongTensor([1, 2, 1, 2, 0]) v_length = torch.LongTensor([2, 3]) v_onehot = utils.oneHot(v, v_length, 4) target = torch.FloatTensor([[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]], [[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]]]) assert target.equal(v_onehot)
def __iter__(self): n_batch = len(self) // self.batch_size tail = len(self) % self.batch_size index = torch.LongTensor(len(self)).fill_(0) for i in range(n_batch): random_start = random.randint(0, len(self) - self.batch_size) batch_index = random_start + torch.range(0, self.batch_size - 1) index[i * self.batch_size:(i + 1) * self.batch_size] = batch_index # deal with tail if tail: random_start = random.randint(0, len(self) - self.batch_size) tail_index = random_start + torch.range(0, tail - 1) index[(i + 1) * self.batch_size:] = tail_index return iter(index)