python类unsqueeze()的实例源码

datasets.py 文件源码 项目:RetinaNet 作者: c0nn3r 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def __getitem__(self, index):
        """
        Args:
            index (int): Index
        Returns:
            tuple: Tuple (image, target). target is the object returned by ``coco.loadAnns``.
        """
        coco = self.coco
        img_id = self.ids[index]
        ann_ids = coco.getAnnIds(imgIds=img_id)
        target = coco.loadAnns(ann_ids)

        target = torch.unsqueeze(torch.Tensor(target[0]['bbox']), -1)

        path = coco.loadImgs(img_id)[0]['file_name']

        img = Image.open(os.path.join(self.root, path)).convert('RGB')

        if self.transform is not None:
            img = self.transform(img)

        if self.target_transform is not None:
            target = self.target_transform(target)

        return img, target
image_pool.py 文件源码 项目:DistanceGAN 作者: sagiebenaim 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def query(self, images):
        if self.pool_size == 0:
            return images
        return_images = []
        for image in images.data:
            image = torch.unsqueeze(image, 0)
            if self.num_imgs < self.pool_size:
                self.num_imgs = self.num_imgs + 1
                self.images.append(image)
                return_images.append(image)
            else:
                p = random.uniform(0, 1)
                if p > 0.5:
                    random_id = random.randint(0, self.pool_size-1)
                    tmp = self.images[random_id].clone()
                    self.images[random_id] = image
                    return_images.append(tmp)
                else:
                    return_images.append(image)
        return_images = Variable(torch.cat(return_images, 0))
        return return_images
image_pool.py 文件源码 项目:DeblurGAN 作者: KupynOrest 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def query(self, images):
        if self.pool_size == 0:
            return images
        return_images = []
        for image in images.data:
            image = torch.unsqueeze(image, 0)
            if self.num_imgs < self.pool_size:
                self.num_imgs = self.num_imgs + 1
                self.images.append(image)
                return_images.append(image)
            else:
                p = random.uniform(0, 1)
                if p > 0.5:
                    random_id = random.randint(0, self.pool_size-1)
                    tmp = self.images[random_id].clone()
                    self.images[random_id] = image
                    return_images.append(tmp)
                else:
                    return_images.append(image)
        return_images = Variable(torch.cat(return_images, 0))
        return return_images
train.py 文件源码 项目:lr-gan.pytorch 作者: jwyang 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def clampT(self, Tin):
        x_s = Tin.select(1, 0)
        x_r = Tin.select(1, 1)
        x_t = Tin.select(1, 2)

        y_r = Tin.select(1, 3)
        y_s = Tin.select(1, 4)
        y_t = Tin.select(1, 5)

        x_s_clamp = torch.unsqueeze(x_s.clamp(opt.maxobjscale, 2 * opt.maxobjscale), 1)
        x_r_clmap = torch.unsqueeze(x_r.clamp(-rot, rot), 1)
        x_t_clmap = torch.unsqueeze(x_t.clamp(-1.0, 1.0), 1)

        y_r_clamp = torch.unsqueeze(y_r.clamp(-rot, rot), 1)
        y_s_clamp = torch.unsqueeze(y_s.clamp(opt.maxobjscale, 2 * opt.maxobjscale), 1)
        y_t_clamp = torch.unsqueeze(y_t.clamp(-1.0, 1.0), 1)

        Tout = torch.cat([x_s_clamp, x_r_clmap, x_t_clmap, y_r_clamp, y_s_clamp, y_t_clamp], 1)
        return Tout
CycleGAN.py 文件源码 项目:Deep-learning-with-cats 作者: AlexiaJM 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def query(self, images):
        if self.pool_size == 0:
            return images
        return_images = []
        for image in images.data:
            image = torch.unsqueeze(image, 0)
            if self.num_imgs < self.pool_size:
                self.num_imgs = self.num_imgs + 1
                self.images.append(image)
                return_images.append(image)
            else:
                p = random.uniform(0, 1)
                if p > 0.5:
                    random_id = random.randint(0, self.pool_size-1)
                    tmp = self.images[random_id].clone()
                    self.images[random_id] = image
                    return_images.append(tmp)
                else:
                    return_images.append(image)
        return_images = Variable(torch.cat(return_images, 0))
        return return_images

# Initialize fake image pools
image_pool.py 文件源码 项目:CycleGANwithPerceptionLoss 作者: EliasVansteenkiste 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def query(self, images):
        if self.pool_size == 0:
            return images
        return_images = []
        for image in images.data:
            image = torch.unsqueeze(image, 0)
            if self.num_imgs < self.pool_size:
                self.num_imgs = self.num_imgs + 1
                self.images.append(image)
                return_images.append(image)
            else:
                p = random.uniform(0, 1)
                if p > 0.5:
                    random_id = random.randint(0, self.pool_size-1)
                    tmp = self.images[random_id].clone()
                    self.images[random_id] = image
                    return_images.append(tmp)
                else:
                    return_images.append(image)
        return_images = Variable(torch.cat(return_images, 0))
        return return_images
image_pool.py 文件源码 项目:pytorch_cycle_gan 作者: jinfagang 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def query(self, images):
        if self.pool_size == 0:
            return images
        return_images = []
        for image in images.data:
            image = torch.unsqueeze(image, 0)
            if self.num_imgs < self.pool_size:
                self.num_imgs = self.num_imgs + 1
                self.images.append(image)
                return_images.append(image)
            else:
                p = random.uniform(0, 1)
                if p > 0.5:
                    random_id = random.randint(0, self.pool_size-1)
                    tmp = self.images[random_id].clone()
                    self.images[random_id] = image
                    return_images.append(tmp)
                else:
                    return_images.append(image)
        return_images = Variable(torch.cat(return_images, 0))
        return return_images
image_pool.py 文件源码 项目:pytorch-CycleGAN-and-pix2pix 作者: junyanz 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def query(self, images):
        if self.pool_size == 0:
            return Variable(images)
        return_images = []
        for image in images:
            image = torch.unsqueeze(image, 0)
            if self.num_imgs < self.pool_size:
                self.num_imgs = self.num_imgs + 1
                self.images.append(image)
                return_images.append(image)
            else:
                p = random.uniform(0, 1)
                if p > 0.5:
                    random_id = random.randint(0, self.pool_size-1)
                    tmp = self.images[random_id].clone()
                    self.images[random_id] = image
                    return_images.append(tmp)
                else:
                    return_images.append(image)
        return_images = Variable(torch.cat(return_images, 0))
        return return_images
image_pool.py 文件源码 项目:wasserstein-cyclegan 作者: abhiskk 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def query(self, images):
        if self.pool_size == 0:
            return images
        return_images = []
        for image in images.data:
            image = torch.unsqueeze(image, 0)
            if self.num_imgs < self.pool_size:
                self.num_imgs = self.num_imgs + 1
                self.images.append(image)
                return_images.append(image)
            else:
                p = random.uniform(0, 1)
                if p > 0.5:
                    random_id = random.randint(0, self.pool_size-1)
                    tmp = self.images[random_id].clone()
                    self.images[random_id] = image
                    return_images.append(tmp)
                else:
                    return_images.append(image)
        return_images = Variable(torch.cat(return_images, 0))
        return return_images
utils.py 文件源码 项目:rarepepes 作者: kendricktan 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def query(self, images):
        if self.pool_size == 0:
            return images
        return_images = []
        for image in images.data:
            image = torch.unsqueeze(image, 0)
            if self.num_imgs < self.pool_size:
                self.num_imgs = self.num_imgs + 1
                self.images.append(image)
                return_images.append(image)
            else:
                p = random.uniform(0, 1)
                if p > 0.5:
                    random_id = random.randint(0, self.pool_size - 1)
                    tmp = self.images[random_id].clone()
                    self.images[random_id] = image
                    return_images.append(tmp)
                else:
                    return_images.append(image)
        return_images = Variable(torch.cat(return_images, 0))
        return return_images
MessageFunction.py 文件源码 项目:nmp_qc 作者: priba 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def m_ggnn(self, h_v, h_w, e_vw, opt={}):

        m = Variable(torch.zeros(h_w.size(0), h_w.size(1), self.args['out']).type_as(h_w.data))

        for w in range(h_w.size(1)):
            if torch.nonzero(e_vw[:, w, :].data).size():
                for i, el in enumerate(self.args['e_label']):
                    ind = (el == e_vw[:,w,:]).type_as(self.learn_args[0][i])

                    parameter_mat = self.learn_args[0][i][None, ...].expand(h_w.size(0), self.learn_args[0][i].size(0),
                                                                            self.learn_args[0][i].size(1))

                    m_w = torch.transpose(torch.bmm(torch.transpose(parameter_mat, 1, 2),
                                                                        torch.transpose(torch.unsqueeze(h_w[:, w, :], 1),
                                                                                        1, 2)), 1, 2)
                    m_w = torch.squeeze(m_w)
                    m[:,w,:] = ind.expand_as(m_w)*m_w
        return m
matrix.py 文件源码 项目:paysage 作者: drckf 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def pdist(x: T.FloatTensor, y: T.FloatTensor) -> T.FloatTensor:
    """
    Compute the pairwise distance matrix between the rows of x and y.

    Args:
        x (tensor (num_samples_1, num_units))
        y (tensor (num_samples_2, num_units))

    Returns:
        tensor (num_samples_1, num_samples_2)

    """
    inner = dot(x, transpose(y))
    x_mag = norm(x, axis=1) ** 2
    y_mag = norm(y, axis=1) ** 2
    squared = add(unsqueeze(y_mag, axis=0), add(unsqueeze(x_mag, axis=1), -2*inner))
    return torch.sqrt(clip(squared, a_min=0))
visualize.py 文件源码 项目:pytorch-classification 作者: bearpaw 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def colorize(x):
    ''' Converts a one-channel grayscale image to a color heatmap image '''
    if x.dim() == 2:
        torch.unsqueeze(x, 0, out=x)
    if x.dim() == 3:
        cl = torch.zeros([3, x.size(1), x.size(2)])
        cl[0] = gauss(x,.5,.6,.2) + gauss(x,1,.8,.3)
        cl[1] = gauss(x,1,.5,.3)
        cl[2] = gauss(x,1,.2,.3)
        cl[cl.gt(1)] = 1
    elif x.dim() == 4:
        cl = torch.zeros([x.size(0), 3, x.size(2), x.size(3)])
        cl[:,0,:,:] = gauss(x,.5,.6,.2) + gauss(x,1,.8,.3)
        cl[:,1,:,:] = gauss(x,1,.5,.3)
        cl[:,2,:,:] = gauss(x,1,.2,.3)
    return cl
image_pool.py 文件源码 项目:VIGAN 作者: chaoshangcs 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def query(self, images):
        if self.pool_size == 0:
            return images
        return_images = []
        for image in images.data:
            image = torch.unsqueeze(image, 0)
            if self.num_imgs < self.pool_size:
                self.num_imgs = self.num_imgs + 1
                self.images.append(image)
                return_images.append(image)
            else:
                p = random.uniform(0, 1)
                if p > 0.5:
                    random_id = random.randint(0, self.pool_size-1)
                    tmp = self.images[random_id].clone()
                    self.images[random_id] = image
                    return_images.append(tmp)
                else:
                    return_images.append(image)
        return_images = Variable(torch.cat(return_images, 0))
        return return_images
loss.py 文件源码 项目:pytorch-semseg 作者: meetshah1995 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def bootstrapped_cross_entropy2d(input, target, K, weight=None, size_average=True):

    batch_size = input.size()[0]

    def _bootstrap_xentropy_single(input, target, K, weight=None, size_average=True):
        n, c, h, w = input.size()
        log_p = F.log_softmax(input, dim=1)
        log_p = log_p.transpose(1, 2).transpose(2, 3).contiguous().view(-1, c)
        log_p = log_p[target.view(n * h * w, 1).repeat(1, c) >= 0]
        log_p = log_p.view(-1, c)

        mask = target >= 0
        target = target[mask]
        loss = F.nll_loss(log_p, target, weight=weight, reduce=False, size_average=False)
        topk_loss, _ = loss.topk(K)
        reduced_topk_loss = topk_loss.sum() / K

        return reduced_topk_loss

    loss = 0.0
    # Bootstrap from each image not entire batch
    for i in range(batch_size):
        loss += _bootstrap_xentropy_single(input=torch.unsqueeze(input[i], 0),
                                           target=torch.unsqueeze(target[i], 0),
                                           K=K,
                                           weight=weight,
                                           size_average=size_average)
    return loss / float(batch_size)
pool.py 文件源码 项目:python-utils 作者: zhijian-liu 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def query(self, elements):
        if self.capacity == 0:
            return elements
        choices = []
        for element in elements.data:
            element = torch.unsqueeze(element, 0)
            if self.size < self.capacity:
                self.size += 1
                self.elements.append(element)
                choices.append(element)
            else:
                if random.uniform(0, 1) > 0.5:
                    index = random.randint(0, self.capacity - 1)
                    candidate = self.elements[index].clone()
                    self.elements[index] = element
                    choices.append(candidate)
                else:
                    choices.append(element)
        choices = Variable(torch.cat(choices, 0))
        return choices
train_text_embedding.py 文件源码 项目:dong_iccv_2017 作者: woozzu 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def pairwise_ranking_loss(margin, x, v):
    zero = torch.zeros(1)
    diag_margin = margin * torch.eye(x.size(0))
    if not args.no_cuda:
        zero, diag_margin = zero.cuda(), diag_margin.cuda()
    zero, diag_margin = Variable(zero), Variable(diag_margin)

    x = x / torch.norm(x, 2, 1, keepdim=True)
    v = v / torch.norm(v, 2, 1, keepdim=True)
    prod = torch.matmul(x, v.transpose(0, 1))
    diag = torch.diag(prod)
    for_x = torch.max(zero, margin - torch.unsqueeze(diag, 1) + prod) - diag_margin
    for_v = torch.max(zero, margin - torch.unsqueeze(diag, 0) + prod) - diag_margin
    return (torch.sum(for_x) + torch.sum(for_v)) / x.size(0)
try.py 文件源码 项目:StackGAN_pytorch 作者: qizhex 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def forward(self, x):
        h_relu = self.linear1(x).clamp(min=0)
        h_relu = torch.unsqueeze(torch.unsqueeze(h_relu, 2), 3) # -> N x H x 1 x 1
        h_expand = h_relu.expand(64, H, h, w).contiguous().view(64, -1) # -> N x H x h x w
        y_pred = self.linear2(h_expand) # -> N x D_out
        return y_pred

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
rasor_model.py 文件源码 项目:squad_rasor_nn 作者: hsgodhia 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def _span_sums(self, p_lens, stt, end, max_p_len, batch_size, dim, max_ans_len):
        # stt       (max_p_len, batch_size, dim)
        # end       (max_p_len, batch_size, dim)
        # p_lens    (batch_size,)

        max_ans_len_range = torch.from_numpy(np.arange(max_ans_len))
        max_ans_len_range = max_ans_len_range.unsqueeze(0)  # (1, max_ans_len) is a vector like [0,1,2,3,4....,max_ans_len-1]
        offsets = torch.from_numpy(np.arange(max_p_len))
        offsets = offsets.unsqueeze(0)  # (1, max_p_len) is a vector like (0,1,2,3,4....max_p_len-1)
        offsets = offsets.transpose(0, 1)  # (max_p_len, 1) is row vector now like [0/1/2/3...max_p_len-1]

        end_idxs = max_ans_len_range.expand(offsets.size(0), max_ans_len_range.size(1)) + offsets.expand(offsets.size(0), max_ans_len_range.size(1))
        #pdb.set_trace()
        end_idxs_flat = end_idxs.view(-1, 1).squeeze(1)  # (max_p_len*max_ans_len, )
        # note: this is not modeled as tensor of size (SZ, 1) but vector of SZ size
        zero_t = torch.zeros(max_ans_len - 1, batch_size, dim)
        if torch.cuda.is_available():
            zero_t = zero_t.cuda(0)
            end_idxs_flat = end_idxs_flat.cuda(0)

        end_padded = torch.cat((end, Variable(zero_t)), 0)
        end_structed = end_padded[end_idxs_flat]  # (max_p_len*max_ans_len, batch_size, dim)
        end_structed = end_structed.view(max_p_len, max_ans_len, batch_size, dim)
        stt_shuffled = stt.unsqueeze(1)  # stt (max_p_len, 1, batch_size, dim)

        # since the FFNN(h_a) * W we expand h_a as [p_start, p_end]*[w_1 w_2] so this reduces to p_start*w_1 + p_end*w_2
        # now we can reuse the operations, we compute only once
        span_sums = stt_shuffled.expand(max_p_len, max_ans_len, batch_size, dim) + end_structed # (max_p_len, max_ans_len, batch_size, dim)

        span_sums_reshapped = span_sums.permute(2, 0, 1, 3).contiguous().view(batch_size, max_ans_len * max_p_len, dim)

        p_lens_shuffled = p_lens.unsqueeze(1)
        end_idxs_flat_shuffled = end_idxs_flat.unsqueeze(0)

        span_masks_reshaped = Variable(end_idxs_flat_shuffled.expand(p_lens_shuffled.size(0), end_idxs_flat_shuffled.size(1))) < p_lens_shuffled.expand(p_lens_shuffled.size(0), end_idxs_flat_shuffled.size(1))
        span_masks_reshaped = span_masks_reshaped.float()

        return span_sums_reshapped, span_masks_reshaped

    #q_align_weights = self.softmax(q_align_mask_scores)  # (batch_size, max_p_len, max_q_len)
rasor_model_AoA.py 文件源码 项目:squad_rasor_nn 作者: hsgodhia 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def _span_sums(self, p_lens, stt, end, max_p_len, batch_size, dim, max_ans_len):
        # stt       (max_p_len, batch_size, dim)
        # end       (max_p_len, batch_size, dim)
        # p_lens    (batch_size,)

        max_ans_len_range = torch.from_numpy(np.arange(max_ans_len))
        max_ans_len_range = max_ans_len_range.unsqueeze(0)  # (1, max_ans_len) is a vector like [0,1,2,3,4....,max_ans_len-1]
        offsets = torch.from_numpy(np.arange(max_p_len))
        offsets = offsets.unsqueeze(0)  # (1, max_p_len) is a vector like (0,1,2,3,4....max_p_len-1)
        offsets = offsets.transpose(0, 1)  # (max_p_len, 1) is row vector now like [0/1/2/3...max_p_len-1]

        end_idxs = max_ans_len_range.expand(offsets.size(0), max_ans_len_range.size(1)) + offsets.expand(offsets.size(0), max_ans_len_range.size(1))
        #pdb.set_trace()
        end_idxs_flat = end_idxs.view(-1, 1).squeeze(1)  # (max_p_len*max_ans_len, )
        # note: this is not modeled as tensor of size (SZ, 1) but vector of SZ size
        zero_t = torch.zeros(max_ans_len - 1, batch_size, dim)
        if torch.cuda.is_available():
            zero_t = zero_t.cuda(0)
            end_idxs_flat = end_idxs_flat.cuda(0)

        end_padded = torch.cat((end, Variable(zero_t)), 0)
        end_structed = end_padded[end_idxs_flat]  # (max_p_len*max_ans_len, batch_size, dim)
        end_structed = end_structed.view(max_p_len, max_ans_len, batch_size, dim)
        stt_shuffled = stt.unsqueeze(1)  # stt (max_p_len, 1, batch_size, dim)

        # since the FFNN(h_a) * W we expand h_a as [p_start, p_end]*[w_1 w_2] so this reduces to p_start*w_1 + p_end*w_2
        # now we can reuse the operations, we compute only once
        span_sums = stt_shuffled.expand(max_p_len, max_ans_len, batch_size, dim) + end_structed # (max_p_len, max_ans_len, batch_size, dim)

        span_sums_reshapped = span_sums.permute(2, 0, 1, 3).contiguous().view(batch_size, max_ans_len * max_p_len, dim)

        p_lens_shuffled = p_lens.unsqueeze(1)
        end_idxs_flat_shuffled = end_idxs_flat.unsqueeze(0)

        span_masks_reshaped = Variable(end_idxs_flat_shuffled.expand(p_lens_shuffled.size(0), end_idxs_flat_shuffled.size(1))) < p_lens_shuffled.expand(p_lens_shuffled.size(0), end_idxs_flat_shuffled.size(1))
        span_masks_reshaped = span_masks_reshaped.float()

        return span_sums_reshapped, span_masks_reshaped

    #q_align_weights = self.softmax(q_align_mask_scores)  # (batch_size, max_p_len, max_q_len)
nn_cuda.py 文件源码 项目:EarlyWarning 作者: wjlei1990 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def predict_on_test(net, test_x):
    print("Predict...")
    pred_y = []
    for idx in range(len(test_x)):
        x = test_x[idx, :]
        x = Variable(torch.unsqueeze(torch.Tensor(x), dim=0)).cuda()
        y_p = net(x)
        _y = float(y_p.cpu().data.numpy()[0])
        # print("pred %d: %f | true y: %f" % (idx, _y, test_y[idx]))
        pred_y.append(_y)

    return pred_y
eew_rnn.py 文件源码 项目:EarlyWarning 作者: wjlei1990 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def main():
    waveforms, magnitudes = load_data()
    loader = make_dataloader(waveforms, magnitudes)

    rnn = RNN(input_size, hidden_size, num_layers)
    print(rnn)

    optimizer = torch.optim.Adam(rnn.parameters(), lr=LR)
    loss_func = nn.MSELoss()

    for epoch in range(3):
        loss_epoch = []
        for step, (batch_x, batch_y) in enumerate(loader):
            x = torch.unsqueeze(batch_x[0, :, :].t(), dim=1)
            print('Epoch: ', epoch, '| Step: ', step, '| x: ',
                  x.size(), '| y: ', batch_y.numpy())
            x = Variable(x)
            y = Variable(torch.Tensor([batch_y.numpy(), ]))
            prediction = rnn(x)
            loss = loss_func(prediction, y)
            optimizer.zero_grad()  # clear gradients for this training step
            loss.backward()  # backpropagation, compute gradients
            optimizer.step()
            loss_epoch.append(loss.data[0])
            print("Current loss: %e --- loss mean: %f"
                  % (loss.data[0], np.mean(loss_epoch)))
eew_rnn_cuda.py 文件源码 项目:EarlyWarning 作者: wjlei1990 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def predict_on_test(rnn, test_x):
    print("Predict...")
    pred_y = []
    for idx in range(len(test_x)):
        x = test_x[idx, :, :]
        x = Variable(torch.unsqueeze(torch.Tensor(x).t(), dim=1)).cuda()
        y_p = rnn(x)
        _y = float(y_p.cpu().data.numpy()[0])
        # print("pred %d: %f | true y: %f" % (idx, _y, test_y[idx]))
        pred_y.append(_y)

    return pred_y
layers.py 文件源码 项目:DBQA 作者: nanfeng1101 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def forward(self, q_input, a_input):
        qw = torch.mm(q_input, self.W.view(self.input_size, -1)).view(-1, self.dim, self.input_size)
        qwa = torch.bmm(qw, torch.unsqueeze(a_input, 2))
        qa_vec = qwa.view(-1, self.dim)
        return qa_vec
qa_cnn.py 文件源码 项目:DBQA 作者: nanfeng1101 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def forward(self, q_input, a_input, drop_rate):
        """
        input -> embedding_layer -> multi_cnn_layer -> interact_layer -> batchnorm_layer -> mlp_layer
        :param q_input: question sentence vec
        :param a_input: answer sentence vec
        :param: drop_rate: dropout rate
        :return:
        """
        q_input_emb = torch.unsqueeze(self.embedding(q_input), dim=1)
        a_input_emb = torch.unsqueeze(self.embedding(a_input), dim=1)
        q_vec, a_vec = self.inception_module_layers(q_input_emb, a_input_emb)
        qa_vec = self.interact_layer(q_vec, a_vec)
        bn_vec = self.bn_layer(qa_vec)
        prop, cate = self.mlp(bn_vec, drop_rate)
        return prop, cate
urnn.py 文件源码 项目:URNN-PyTorch 作者: jingli9111 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def _modReLU(self, h, bias):
        """
        sign(z)*relu(z)
        """
        batch_size = h.size(0)
        sign = torch.sign(h)
        bias_batch = (bias.unsqueeze(0)
                      .expand(batch_size, *bias.size()))
        return sign * functional.relu(torch.abs(h) + bias_batch)
urnn.py 文件源码 项目:URNN-PyTorch 作者: jingli9111 项目源码 文件源码 阅读 76 收藏 0 点赞 0 评论 0
def _forward_rnn(cell, input_, length, hx):
        max_time = input_.size(0)
        output = []
        for time in range(max_time):
            h_next = cell(input_=input_[time], hx=hx)
            # mask = (time < length).float().unsqueeze(1).expand_as(h_next)
            # h_next = h_next*mask + hx*(1 - mask)
            output.append(h_next)
        output = torch.stack(output, 0)
        return output, h_next
goru.py 文件源码 项目:URNN-PyTorch 作者: jingli9111 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def _modReLU(self, h, bias):
        """
        sign(z)*relu(z)
        """
        batch_size = h.size(0)
        sign = torch.sign(h)
        bias_batch = (bias.unsqueeze(0)
                      .expand(batch_size, *bias.size()))
        return sign * functional.relu(torch.abs(h) + bias_batch)
goru.py 文件源码 项目:URNN-PyTorch 作者: jingli9111 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def _forward_rnn(cell, input_, length, hx):
        max_time = input_.size(0)
        output = []
        for time in range(max_time):
            h_next = cell(input_=input_[time], hx=hx)
            # mask = (time < length).float().unsqueeze(1).expand_as(h_next)
            # h_next = h_next*mask + hx*(1 - mask)
            output.append(h_next)
        output = torch.stack(output, 0)
        return output, h_next
UpdateFunction.py 文件源码 项目:nmp_qc 作者: priba 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def u_ggnn(self, h_v, m_v, opt={}):
        h_v.contiguous()
        m_v.contiguous()
        h_new = self.learn_modules[0](torch.transpose(m_v, 0, 1), torch.unsqueeze(h_v, 0))[0]  # 0 or 1???
        return torch.transpose(h_new, 0, 1)


问题


面经


文章

微信
公众号

扫码关注公众号