How to use the torch.zeros function in torch

To help you get started, we’ve selected a few torch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pytorch / pytorch / test / test_multiprocessing.py View on Github external
def test_cuda_bad_call(self):
        # Initialize CUDA
        t = torch.zeros(5, 5).cuda().cpu()
        inq = mp.Queue()
        outq = mp.Queue()
        p = mp.Process(target=queue_get_exception, args=(inq, outq))
        p.start()
        inq.put(t)
        p.join()
        self.assertIsInstance(outq.get(), RuntimeError)
github pytorch / pytorch / test / test_torch.py View on Github external
# test reuse
        X = torch.randn(4, 4)
        U, S, V = torch.svd(X)
        Xhat = torch.mm(U, torch.mm(S.diag(), V.t()))
        self.assertEqual(X, Xhat, 1e-8, 'USV\' wrong')

        self.assertFalse(U.is_contiguous(), 'U is contiguous')
        torch.svd(X, out=(U, S, V))
        Xhat = torch.mm(U, torch.mm(S.diag(), V.t()))
        self.assertEqual(X, Xhat, 1e-8, 'USV\' wrong')

        # test non-contiguous
        X = torch.randn(5, 5)
        U = torch.zeros(5, 2, 5)[:, 1]
        S = torch.zeros(5, 2)[:, 1]
        V = torch.zeros(5, 2, 5)[:, 1]

        self.assertFalse(U.is_contiguous(), 'U is contiguous')
        self.assertFalse(S.is_contiguous(), 'S is contiguous')
        self.assertFalse(V.is_contiguous(), 'V is contiguous')
        torch.svd(X, out=(U, S, V))
        Xhat = torch.mm(U, torch.mm(S.diag(), V.t()))
        self.assertEqual(X, Xhat, 1e-8, 'USV\' wrong')
github pyro-ppl / pyro / tests / integration_tests / test_tracegraph_elbo.py View on Github external
def _test_vectorized_map_data_in_elbo(self, n_superfluous_top, n_superfluous_bottom, n_steps):
        pyro.clear_param_store()
        self.data_tensor = Variable(torch.zeros(9, 2))
        for _out in range(self.n_outer):
            for _in in range(self.n_inner):
                self.data_tensor[3 * _out + _in, :] = self.data[_out][_in]

        def model():
            mu_latent = pyro.sample(
                    "mu_latent",
                    dist.Normal(self.mu0, torch.pow(self.lam0, -0.5), reparameterized=False))

            def obs_inner(i, _i, _x):
                for k in range(n_superfluous_top):
                    pyro.sample("z_%d_%d" % (i, k),
                                dist.Normal(ng_zeros(4 - i, 1), ng_ones(4 - i, 1), reparameterized=False))
                pyro.observe("obs_%d" % i, dist.normal, _x, mu_latent, torch.pow(self.lam, -0.5))
                for k in range(n_superfluous_top, n_superfluous_top + n_superfluous_bottom):
                    pyro.sample("z_%d_%d" % (i, k),
github tshi04 / LeafNATS / headline2_summary2_app / beam_search.py View on Github external
encoder_hy, hidden_encoder = models['encoder_'+task_key](encoder_hy0)
    hidden_decoder = models['encoder2decoder_'+task_key](hidden_encoder)
    if args.rnn_network == 'lstm':
        (h0_new, c0_new) = hidden_decoder
    else:
        h0_new = hidden_decoder

    beam_size = args.beam_size
    src_seq_len = src_emb.size(1)
    if args.repetition == 'temporal':
        past_attn_new = Variable(torch.ones(
            batch_size*beam_size, src_seq_len)).to(args.device)
    else:
        past_attn_new = Variable(torch.zeros(
            batch_size*beam_size, src_seq_len)).to(args.device)
    h_attn_new = Variable(torch.zeros(
        batch_size*beam_size, args.trg_hidden_dim)).to(args.device)
    past_dehy_new = Variable(torch.zeros(1, 1)).to(args.device)

    if args.task_key[-7:] == 'summary':
        max_len = args.sum_seq_lens
    if args.task_key[-5:] == 'title':
        max_len = args.ttl_seq_lens
    beam_seq = Variable(torch.LongTensor(
        batch_size, beam_size, max_len+1).fill_(batch_data['vocab2id'][''])).to(args.device)
    beam_seq[:, :, 0] = batch_data['vocab2id']['<s>']
    beam_prb = torch.FloatTensor(batch_size, beam_size).fill_(1.0)
    last_wd = Variable(torch.LongTensor(batch_size, beam_size, 1).fill_(
        batch_data['vocab2id']['<s>'])).to(args.device)
    beam_attn_ = Variable(torch.FloatTensor(
        max_len, batch_size, beam_size, src_seq_len).fill_(0.0)).to(args.device)
</s></s>
github JaryHuang / awesome_SSD_FPN_GIoU / utils / box / box_utils.py View on Github external
Returns:
        gious(ndarray): shape (n, k)
    """


    #bboxes1 = torch.FloatTensor(bboxes1)
    #bboxes2 = torch.FloatTensor(bboxes2)
    rows = bboxes1.shape[0]
    cols = bboxes2.shape[0]
    ious = torch.zeros((rows, cols))
    if rows * cols == 0:
        return ious
    exchange = False
    if bboxes1.shape[0] > bboxes2.shape[0]:
        bboxes1, bboxes2 = bboxes2, bboxes1
        ious = torch.zeros((cols, rows))
        exchange = True
    area1 = (bboxes1[:, 2] - bboxes1[:, 0]) * (
        bboxes1[:, 3] - bboxes1[:, 1])
    area2 = (bboxes2[:, 2] - bboxes2[:, 0]) * (
        bboxes2[:, 3] - bboxes2[:, 1])

    inter_max_xy = torch.min(bboxes1[:, 2:],bboxes2[:, 2:])

    inter_min_xy = torch.max(bboxes1[:, :2],bboxes2[:, :2])

    out_max_xy = torch.max(bboxes1[:, 2:],bboxes2[:, 2:])

    out_min_xy = torch.min(bboxes1[:, :2],bboxes2[:, :2])

    inter = torch.clamp((inter_max_xy - inter_min_xy), min=0)
    inter_area = inter[:, 0] * inter[:, 1]
github santi-pdp / pase / pase / models / modules.py View on Github external
def forward(self, inputs):
        # convert inputs [B, F, T] -> [BxT, F]
        inputs = inputs.permute(0, 2, 1).contiguous()
        input_shape = inputs.shape
        flat_input = inputs.view(-1, self.emb_dim)
        device = 'cuda' if inputs.is_cuda else 'cpu'

        # TODO: UNDERSTAND THIS COMPUTATION
        # compute distances
        dist = (torch.sum(flat_input ** 2, dim=1, keepdim=True) + \
                torch.sum(self.emb.weight ** 2, dim=1) - \
                2 * torch.matmul(flat_input, self.emb.weight.t()))

        # Encoding
        enc_indices = torch.argmin(dist, dim=1).unsqueeze(1)
        enc = torch.zeros(enc_indices.shape[0], self.emb_K).to(device)
        enc.scatter_(1, enc_indices, 1)
        
        # Use EMA to update emb vectors
        if self.training:
            self.ema_cluster_size = self.ema_cluster_size * self.gamma + \
                    (1 - self.gamma) * torch.sum(enc, 0)
            n = torch.sum(self.ema_cluster_size.data)
            self.ema_cluster_size = (
                (self.ema_cluster_size + self.eps) / \
                (n + self.emb_K * self.eps) * n
            )
            dw = torch.matmul(enc.t(), flat_input)
            self.ema_w = nn.Parameter(self.ema_w * self.gamma + \
                                      (1 - self.gamma) * dw)
            self.emb.weight = nn.Parameter(self.ema_w / \
                                           self.ema_cluster_size.unsqueeze(1))
github ikostrikov / pytorch-a2c-ppo-acktr-gail / running_stat.py View on Github external
def __init__(self, shape, demean=True, destd=True, clip=10.0):
        super(ObsNorm, self).__init__()
        self.demean = demean
        self.destd = destd
        self.clip = clip

        self.register_buffer('count', torch.zeros(1).double() + 1e-2)
        self.register_buffer('sum', torch.zeros(shape).double())
        self.register_buffer('sum_sqr', torch.zeros(shape).double() + 1e-2)

        self.register_buffer('mean', torch.zeros(shape),)
        self.register_buffer('std', torch.ones(shape))
github Ailln / chatbot / back / model / en_decoder.py View on Github external
def init_hidden(self, input):
        hidden = Variable(
            torch.zeros(self.n_layers * 1, input.size(0), self.hidden_size)).cuda() if USE_CUDA else Variable(
            torch.zeros(self.n_layers, input.size(0), self.hidden_size))
        context = Variable(
            torch.zeros(self.n_layers * 1, input.size(0), self.hidden_size)).cuda() if USE_CUDA else Variable(
            torch.zeros(self.n_layers, input.size(0), self.hidden_size))
        return (hidden, context)
github zuoym15 / craves.ai / pose / datasets / arm.py View on Github external
def _compute_mean(self):
        meanstd_file = '../datasets/arm/mean.pth.tar'
        if isfile(meanstd_file):
            meanstd = torch.load(meanstd_file)
        else:
            mean = torch.zeros(3)
            std = torch.zeros(3)
            for index in self.train:
                ids = [index]
                cams = [self.cam_name]
                images = self.dataset.get_image(cams, ids, 'synthetic')
                img_path = images[0]
                img = load_image(img_path) # CxHxW
                mean += img.view(img.size(0), -1).mean(1)
                std += img.view(img.size(0), -1).std(1)
            mean /= len(self.train)
            std /= len(self.train)
            meanstd = {
                'mean': mean,
                'std': std,
                }
            torch.save(meanstd, meanstd_file)
github SamuelJoutard / Permutohedral_attention_module / PAM / PL_with_grad_torch_HT.py View on Github external
data_vector_real,
                          blured,
                          rank, 
                          barycentric,
                          blur_neighbours1,
                          blur_neighbours2,
                          indices,
                          low_precision=False):
        n_ch_1 = barycentric.size(1)
        n_ch = n_ch_1 - 1
        n_ch_1 = n_ch + 1
        
        B, n_ch_data, n_voxels = data_vector.size()

        # Splatting
        splat = torch.zeros((B, n_ch_data, blur_neighbours1[0].size(0)+1)).cuda()
        for scit in range(n_ch_1):
            data = (data_vector * 
                    barycentric[:, scit:scit+1].repeat(1, data_vector.size(1), 1))
            splat.scatter_add_(2, 
                               indices[scit].unsqueeze(1).repeat(1, data.size(1), 1), 
                               data)

        # Blur with 1D kernels
        for dit in range(n_ch, -1, -1):
            b1 = torch.index_select(splat, 2, blur_neighbours1[dit])
            b3 = torch.index_select(splat, 2, blur_neighbours2[dit])
            splat = torch.cat([
                splat[:, :, :1], splat[:, :, 1:] + 0.5 * (b1 + b3)], 2)
            
        # Slice
        sliced_feat = [None] * n_ch_1