Exemplo n.º 1
0
    def forward(self, x, to_dense=False):

        d = super().forward_as_dict(x)

        f8_3 = F.elu(self.f8_3(d['conv4']))
        f8_4 = F.elu(self.f8_4(d['conv5']))
        f8_5 = F.elu(self.f8_5(d['conv6']))
        x = F.elu(self.f9(torch.cat([f8_3, f8_4, f8_5], dim=1)))

        if x.size(2) == self.predefined_featuresize and x.size(3) == self.predefined_featuresize:
            ind_from = self.ind_from
            ind_to = self.ind_to
        else:
            ind_from, ind_to = pyutils.get_indices_of_pairs(5, (x.size(2), x.size(3)))
            ind_from = torch.from_numpy(ind_from)
            ind_to = torch.from_numpy(ind_to)

        x = x.view(x.size(0), x.size(1), -1)

        if is_cuda_available:
            ind_from = ind_from.cuda(non_blocking=True)
            ind_to = ind_to.cuda(non_blocking=True)
        ff = torch.index_select(x, dim=2, index=ind_from)
        ft = torch.index_select(x, dim=2, index=ind_to)

        ff = torch.unsqueeze(ff, dim=2)
        ft = ft.view(ft.size(0), ft.size(1), -1, ff.size(3))

        aff = torch.exp(-torch.mean(torch.abs(ft - ff), dim=1))

        if to_dense:
            aff = aff.view(-1).cpu()

            ind_from_exp = torch.unsqueeze(ind_from, dim=0).expand(ft.size(2), -1).contiguous().view(-1)
            indices = torch.stack([ind_from_exp, ind_to])
            indices_tp = torch.stack([ind_to, ind_from_exp])

            area = x.size(2)

            indices_id = torch.stack([torch.arange(0, area).long(), torch.arange(0, area).long()])

            if is_cuda_available:
                indices_id = indices_id.cuda()
                aff = aff.cuda()
                torch_ones_cuda = torch.ones([area]).cuda()
                aff_mat = sparse.FloatTensor(torch.cat([indices, indices_id, indices_tp], dim=1),
                                             torch.cat([aff, torch_ones_cuda, aff])).to_dense().cuda()

            else:
                aff_mat = sparse.FloatTensor(torch.cat([indices, indices_id, indices_tp], dim=1),
                                             torch.cat([aff, torch.ones([area]), aff])).to_dense()

            return aff_mat

        else:
            return aff
Exemplo n.º 2
0
 def __init__(self, data, shape=None):
     if type(data) is tuple:
         indices, values = data
         self._data = tsparse.FloatTensor(indices, values, shape=shape)
     elif isinstance(data, DTensor):
         self._data = data._data.to_sparse()
     elif isinstance(data, STensor):
         self._data = data._data
     else:
         self._data = tsparse.FloatTensor(data, shape=shape)
     self._data = self._data.cuda().coalesce()
Exemplo n.º 3
0
    def from_sparse_array(cls, x: sparse.COO):
        """Construct a sparse tensor from a `sparse.COO`.

        Parameters
        ----------
        x : sparse.COO
            Input COO sparse array.

        Returns
        -------
        STensor
            Sparse tensor constructed from given sparse array.

        Raises
        ------
        TypeError
            Raise if the parameter is not `sparse.COO`.
        """
        if not isinstance(x, sparse.COO):
            raise TypeError(
                f"Argument type should be `sparse.SparseArray`, got {type(x)}")
        indices = torch.from_numpy(x.coords).type(torch.long)
        values = torch.from_numpy(x.data)
        shape = torch.Size(x.shape, dtype=torch.long)
        t = cls.__new__(cls)
        t._data = tsparse.FloatTensor(indices, values, shape).cuda()
        t._data = t._data.coalesce()
        return t
Exemplo n.º 4
0
def sparse_select(dims, indices, t):
    """
    Select sparse tensor t on on dimensions dims and indices chosen by indices.
    Equivalent to t[i_0, i_1, ...] where i_d = : if d is not in dims and
    i_(dims[j]) = indices[j] for all j
    """
    if type(dims) is not list:
        dims = [dims]
    if type(indices) is not list:
        indices = [indices]

    t_indices = t._indices()
    t_values = t._values()
    selector = torch.ones(t_indices.shape[-1]).byte()
    for dim, index in zip(dims, indices):
        selector = selector & (t._indices()[dim, :] == index)
    remaining_dimensions = list(
        filter(lambda x: x not in dims, range(t_indices.shape[0])))

    indices_selected = t_indices[:, selector][remaining_dimensions, :]
    values_selected = t_values[selector]
    new_shape = torch.Size(t.shape[d] for d in remaining_dimensions)

    out = sp.FloatTensor(indices_selected, values_selected, new_shape)
    return out
Exemplo n.º 5
0
    def add_coordinates(self, layer_index, coordinates, lr, avg=1):
        param_coords = []
        gradients = []
        s = []

        # extract coordinate-gradient pairs and combine gradients at the same coordinate
        for coord in coordinates:
            cd = coord[0][1:]
            coord[1] /= avg

            if cd in param_coords:
                parameters[param_coords.index(cd)] += (coord[1])
            else:
                param_coords.append(cd)
                gradients.append(coord[1])

        # get corresponding parameters
        params = [p for p in self.parameters()]

        # create coordinate/index tensor i, and value tensor v
        try:
            grads = FloatTensor(gradients)
            shape = list(params[layer_index].size())

            if len(shape) > 1:
                # update parameters with gradients at particular coordinates
                grads = sparse.FloatTensor(
                    LongTensor(param_coords).t(), grads,
                    Size(shape)).to_dense()

            params[layer_index].data.add_(-lr * grads)
        except Exception as e:
            self.log.exception("Unexpected exception! %s", e)
Exemplo n.º 6
0
def scipy_to_pytorch_sparse(coo):
    values = coo.data
    indices = np.stack((coo.row, coo.col))
    i = torch.LongTensor(indices)
    v = torch.FloatTensor(values)
    shape = coo.shape
    return ts.FloatTensor(i, v, torch.Size(shape))
Exemplo n.º 7
0
    def from_scipy_sparse(cls, x: scipy.sparse.spmatrix):
        """Construct a `STensor` from a `scipy.sparse.spmatrix`.

        Parameters
        ----------
        x : scipy.sparse.spmatrix
            Input sparse matrix.

        Returns
        -------
        STensor
            Sparse tensor constructed from given sparse matrix.

        Raises
        ------
        TypeError
            Raise if the parameter is not `scipy.sparse.spmatrix`.
        """
        if not isinstance(x, scipy.sparse.spmatrix):
            raise TypeError(
                f"Argument type should be `scipy.sparse.spmatrix`, \
                got {type(x)}")
        x = x.tocoo()
        indices = torch.from_numpy(np.vstack([x.row, x.col])).type(torch.long)
        values = torch.from_numpy(x.data)
        shape = torch.Size(x.shape, dtype=torch.long)
        t = cls.__new__(cls)
        t._data = tsparse.FloatTensor(indices, values, shape).cuda()
        t._data = t._data.coalesce()
        return t
Exemplo n.º 8
0
 def _make_sparse(self, indices):
     i = torch.LongTensor(2, indices.numel())
     v = torch.ones(indices.numel())
     i[1].copy_(torch.range(0, indices.numel() - 1))
     i[0].copy_(indices)
     return sparse.FloatTensor(
         i, v, torch.Size([self._weight_size[0],
                           indices.numel()])).contiguous()
Exemplo n.º 9
0
    def _get_W(x: torch.Tensor, n_Neigbr=15, n_sig=8, target=None):
        N, d = x.shape  # N: number of samples, d: the dimension of the sample data
        if N < 2:
            raise ValueError('The number of sample must more than 1!!!')

        n_Neigb = min(N, n_Neigbr)
        n_sig = min(n_sig, n_Neigbr)

        # Calculate the distance
        X = x.detach().cpu().numpy()
        nbrs = NearestNeighbors(n_neighbors=n_Neigbr,
                                algorithm='ball_tree').fit(X)

        dis, idx = nbrs.kneighbors(X)
        dis2 = np.exp(-dis**2 / dis[:, n_sig - 1:n_sig] / 2)  # TV
        # dis2 = np.exp(- dis**2 / dis[:, n_sig-1:n_sig] ) # Aniso-TV

        if target is None:
            M = (n_Neigbr - 1) * N
            idx_i = torch.cat((
                torch.LongTensor(range(M)),
                torch.LongTensor(range(M)),
            ))
            idx_j = torch.cat((
                torch.LongTensor(idx[:, 0].repeat(n_Neigbr - 1)),
                torch.LongTensor(idx[:, 1:].repeat(1)),
            ))

            v = torch.cat((
                torch.FloatTensor(dis2[:, 1:]).view(-1),
                -torch.FloatTensor(dis2[:, 1:]).view(-1),
            ))
            # print(idx_i.shape, idx_j.shape, v.shape)
        else:
            idx_i = []
            idx_j = []
            v = []

            tot = 0
            for i in range(N):
                for k in range(1, n_Neigbr):
                    j = idx[i, k]

                    if target[i] == target[j]:
                        idx_i += [tot, tot]
                        idx_j += [i, j]
                        v += [dis2[i, k], -dis2[i, k]]

                        tot += 1

            idx_i = torch.LongTensor(idx_i)
            idx_j = torch.LongTensor(idx_j)
            v = torch.FloatTensor(v)
            M = tot

        return sparse.FloatTensor(
            torch.cat((idx_i.view(1, -1), idx_j.view(1, -1))), v,
            torch.Size([M, N]))
Exemplo n.º 10
0
def get_sparse_tensor(idxs, vals, dim=74000):
    rows = torch.from_numpy(np.zeros(len(idxs), dtype='int64')).view(1, -1)
    cols = torch.from_numpy(np.array(idxs, dtype='int64')).view(1, -1)
    i = torch.cat((rows, cols), dim=0)
    vals = np.ones(len(idxs), dtype='int64') #TODO use value?
    v = torch.from_numpy(np.array(vals, dtype='float32'))
    sparse_matrix = sparse.FloatTensor(i, v, torch.Size([1, dim]))
    # print(sparse_matrix)
    return sparse_matrix
Exemplo n.º 11
0
    def forward(self, x_input, to_dense=False):
        self.eval()
        d = self.forward_as_dict(x_input)

        f8_3 = F.elu(self.f8_3(d['conv4']))
        f8_4 = F.elu(self.f8_4(d['conv5']))
        f8_5 = F.elu(self.f8_5(d['conv6']))
        x = F.elu(self.f9(torch.cat([f8_3, f8_4, f8_5], dim=1)))

        if x.size(2) == self.predefined_featuresize and x.size(3) == self.predefined_featuresize:
            ind_from = self.ind_from
            ind_to = self.ind_to
        else:
            ind_from, ind_to = get_indices_of_pairs(5, (x.size(2), x.size(3)))
            ind_from = torch.from_numpy(ind_from); ind_to = torch.from_numpy(ind_to)

        x = x.view(x.size(0), x.size(1), -1)

        ff = torch.index_select(x, dim=2, index=ind_from.cuda(non_blocking=True))
        ft = torch.index_select(x, dim=2, index=ind_to.cuda(non_blocking=True))

        ff = torch.unsqueeze(ff, dim=2)
        ft = ft.view(ft.size(0), ft.size(1), -1, ff.size(3))

        aff = torch.exp(-torch.mean(torch.abs(ft-ff), dim=1))

        if to_dense:

            aff = aff.view(-1).cpu()

            ind_from_exp = torch.unsqueeze(ind_from, dim=0).expand(ft.size(2), -1).contiguous().view(-1)
            indices = torch.stack([ind_from_exp, ind_to])
            indices_tp = torch.stack([ind_to, ind_from_exp])

            area = x.size(2)
            indices_id = torch.stack([torch.arange(0, area).long(), torch.arange(0, area).long()])

            aff_mat = sparse.FloatTensor(torch.cat([indices, indices_id, indices_tp], dim=1),
                                      torch.cat([aff, torch.ones([area]), aff])).to_dense().cuda()

            return aff_mat

        else:
            aff_mat = torch.zeros((x.shape[-1],x.shape[-1])).cuda()
            aff = aff.view(-1)

            ind_from_exp = torch.unsqueeze(ind_from, dim=0).expand(ft.size(2), -1).contiguous().view(-1)
            indices = torch.stack([ind_from_exp, ind_to])
            indices_tp = torch.stack([ind_to, ind_from_exp])

            area = x.size(2)
            indices_id = torch.stack([torch.arange(0, area).long(), torch.arange(0, area).long()])
            rows_cols = torch.cat([indices.cuda(), indices_id.cuda(), indices_tp.cuda()], dim=1)
            values = torch.cat([aff, torch.ones([area]).cuda(), aff])
            aff_mat[rows_cols[0], rows_cols[1]] = values
            return aff_mat
Exemplo n.º 12
0
def sparse_scipy2torch(w: sp.coo_matrix):
    """
    build pytorch sparse tensor from scipy sparse matrix
    reference: https://stackoverflow.com/questions/50665141
    :return:
    """
    shape = w.shape
    i = torch.tensor(np.vstack((w.row, w.col)).astype(int)).long()
    v = torch.tensor(w.data).float()
    return sparse.FloatTensor(i, v, torch.Size(shape))
Exemplo n.º 13
0
def to_sparse_tensor(mat):
    """
    Converts a SciPy sparse matrix into a torch sparse tensor.
    """
    if isinstance(mat, ssp.csr_matrix) or isinstance(mat, ssp.csc_matrix):
        mat = mat.tocoo()
    data = mat.data
    indices = np.concatenate((mat.row.reshape(1, -1), mat.col.reshape(1, -1)),
                             axis=0)
    sparse_mat = sp.FloatTensor(torch.LongTensor(indices),
                                torch.FloatTensor(data), torch.Size(mat.shape))
    return sparse_mat
Exemplo n.º 14
0
def sparsify_tensor(tensor):
    """Convert a torch.Tensor into a torch.sparse.FloatTensor

    Args:
        tensor (torch.Tensor)

    returns:
        sparse (torch.sparse.Tensor)
    """
    ij = tensor.nonzero()

    if len(ij) > 0:
        v = tensor[ij[:, 0], ij[:, 1]]
        return sp.FloatTensor(ij.t(), v, tensor.size())
    else:
        return 0
Exemplo n.º 15
0
def get_kd_sparse_tensor(idxsls, dim=74000):
    rowsls = []
    for k in range(len(idxsls)):
        for j in range(len(idxsls[k])):
            rowsls.append(k)
    rows = torch.from_numpy(np.array(rowsls, dtype='int64')).view(1, -1)
    idxseries = []
    for k in idxsls:
        idxseries.extend(k)
    cols = torch.from_numpy(np.array(idxseries, dtype='int64')).view(1, -1)
    i = torch.cat((rows, cols), dim=0)
    vals = np.ones(len(idxseries), dtype='int64') #TODO use value?
    v = torch.from_numpy(np.array(vals, dtype='float32'))
    sparse_matrix = sparse.FloatTensor(i, v, torch.Size([len(idxsls), dim]))
    # print(sparse_matrix)D
    return sparse_matrix
Exemplo n.º 16
0
def normalize_adj(adj):
    """Symmetrically normalize adjacency matrix."""
    adj = adj.coalesce()
    sp_adj = sp.coo_matrix((adj.values(), adj.indices()),
                           shape=list(adj.size()))

    rowsum = np.array(sp_adj.sum(axis=1))
    d_inv_sqrt = np.power(rowsum, -0.5).flatten()
    d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
    d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
    norm_sp_adj = sp_adj.dot(d_mat_inv_sqrt).transpose().dot(
        d_mat_inv_sqrt).tocoo()

    entries = norm_sp_adj.data
    row = norm_sp_adj.row
    col = norm_sp_adj.col
    indices = torch.LongTensor([row, col])
    entries = torch.FloatTensor(entries)
    return sparse.FloatTensor(indices, entries, adj.size())
def collate(batch):
    dim = 74000
    propensity = torch.cat([x['p'] for x in batch]).view(len(batch), 1)
    label = torch.cat([x['label'] for x in batch]).view(len(batch), 1)
    # propensity label batch * 1
    rows = []
    cols = []
    for i in range(len(batch)):
        cols.extend(batch[i]['idx'])
        for k in range(len(batch[i]['idx'])):
            rows.append(i)
    Rows = torch.from_numpy(np.array(rows, dtype='int64')).view(1, -1)
    Cols = torch.from_numpy(np.array(cols, dtype='int64')).view(1, -1)
    i = torch.cat((Rows, Cols), dim=0)
    vals = np.ones(len(cols), dtype='float32')
    v = torch.from_numpy(np.array(vals, dtype='float32'))
    batch_sparse_matrix = sparse.FloatTensor(i, v,
                                             torch.Size([len(batch), dim]))
    return {'p': propensity, 'feature': batch_sparse_matrix, 'label': label}
Exemplo n.º 18
0
    def forward(self, x, adj):
        if x.is_sparse:
            wh = sparse.mm(x, self.weight).view(-1, self.nheads,
                                                self.out_features)
        else:
            x = F.dropout(x, p=self.dropout, training=self.training)
            wh = torch.mm(x, self.weight).view(-1, self.nheads,
                                               self.out_features)

        awh_i = (wh * self.linear_i).sum(dim=2)
        awh_j = (wh * self.linear_j).sum(dim=2)

        idx_i, idx_j = adj._indices()

        e_values = F.leaky_relu(awh_i[idx_i] + awh_j[idx_j],
                                negative_slope=self.alpha)

        e = sparse.FloatTensor(adj._indices(), e_values)
        a = sparse.softmax(e.cpu(), dim=1).to(e.device)

        # Choose memory / speed tradeoff
        # keep_sparse = True  : Loop through sparse tensor (Low memory usage / Slow)
        # keep_sparse = False : Convert sparse tensor to dense tensor (High memory usage / Fast)
        # Both methods return almost identical results
        keep_sparse = False

        if keep_sparse:
            x = torch.cat([(a[i]._values().unsqueeze(dim=2) *
                            wh[a[i]._indices()[0]]).sum(dim=0, keepdim=True)
                           for i in range(x.shape[0])],
                          dim=0)
        else:
            a = a.to_dense().unsqueeze(dim=3)
            wh = wh.unsqueeze(dim=0)
            x = (a * wh).sum(dim=1)

        if self.concat:
            return x.flatten(start_dim=1)
        else:
            return x.mean(dim=1)
Exemplo n.º 19
0
    def __init__(self, original_conv):
        super(M2MSparseConv, self).__init__()

        # Math attributes
        self.IN_CH = original_conv.in_channels
        self.OUT_CH = original_conv.out_channels
        if isinstance(original_conv.kernel_size, int):
            self.K_W, self.K_H = original_conv.kernel_size, original_conv.kernel_size
        else:
            self.K_W, self.K_H = original_conv.kernel_size

        if isinstance(original_conv.padding, int):
            self.P_W, self.P_H = original_conv.padding, original_conv.padding
        else:
            self.P_W, self.P_H = original_conv.padding

        if isinstance(original_conv.stride, int):
            self.S_W, self.S_H = original_conv.stride, original_conv.stride
        else:
            self.S_W, self.S_H = original_conv.stride

        # Parameters
        if type(original_conv) is SoftMaskedConv2d:
            params = original_conv.weight * original_conv.mask
        else:
            params = original_conv.weight
        W = params.view(self.OUT_CH, -1)
        shape = W.shape
        indexes = torch.where(W != 0)
        indexes = torch.stack([indexes[0], indexes[1]])
        values = W.flatten()[W.flatten() != .0]
        self.W = sparse.FloatTensor(indexes, values, shape)
        self.B = None
        if hasattr(original_conv, 'bias'):
            if original_conv.bias is not None:
                self.B = \
                    original_conv.bias.data.unsqueeze(1).unsqueeze(2).unsqueeze(0)
Exemplo n.º 20
0
    def __call__(self, data):
        assert data.face is not None
        assert data.face_curvature is not None
        assert data.face_weight is not None

        # Prepare the initial local coordinate system
        device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
        # device = torch.device('cpu')
        norms = data.norm.to(device)
        positions = data.pos.to(device)
        faces = data.face.to(device)

        face_id = torch.tensor(list(range(len(faces.t())))).to(
            device)  # checked
        face0 = faces[0]  # checked
        face1 = faces[1]  # checked
        face2 = faces[2]  # checked

        weights = data.face_weight  # checked
        f_curv = data.face_curvature

        face0 = torch.stack((face_id, face0), dim=0).t()  # checked
        face1 = torch.stack((face_id, face1), dim=0).t()  # checked
        face2 = torch.stack((face_id, face2), dim=0).t()  # checked
        weights = data.face_weight * torch.ones(len(face0),
                                                dtype=torch.long).to(device)
        sparse_size = torch.Size((faces.shape[1], len(positions)))  # checked

        sparse_face0 = tsp.FloatTensor(
            torch.LongTensor(face0).t(), weights,
            sparse_size).to(device)  # .to_dense()  # checked
        sparse_face1 = tsp.FloatTensor(
            torch.LongTensor(face1).t(), weights,
            sparse_size).to(device)  # .to_dense()  # checked
        sparse_face2 = tsp.FloatTensor(
            torch.LongTensor(face2).t(), weights,
            sparse_size).to(device)  # .to_dense()  # checked

        weighted_faces = sparse_face0 + sparse_face1 + sparse_face2  # checked
        weighted_faces = weighted_faces.coalesce()

        # checked On older pytorch have to cast to float
        weighted_faces = weighted_faces.t()
        node_curv = tsp.mm(weighted_faces, f_curv)
        sum_weights_per_node = tsp.sum(weighted_faces,
                                       dim=1).to_dense()  # checked
        node_curv = node_curv.t() / sum_weights_per_node  # checked
        node_curv = node_curv.t()
        eigs = []
        for i in node_curv:  # checked
            eig = torch.eig(i.reshape(2, 2))
            principal_curvatures = eig.eigenvalues[:, 0].sort(
                descending=True).values
            eigs.append(principal_curvatures)
        eigs = torch.stack(eigs, dim=0)
        s_s = eigs[:, 0] + eigs[:, 1]
        s_p = eigs[:, 0] - eigs[:, 1]
        s = s_s.div(s_p)
        pi = math.pi * torch.ones(len(positions)).to(device)
        s = (2 / pi) * torch.atan(s)

        data.shape_index = s
        if self.remove:
            data.face_curvature = None
            data.face_weights = None
            data.face_normals = None
        return data
Exemplo n.º 21
0
    def get_probabilities(self, train_iter):
        TEXT = self.TEXT
        V = self.V
        unigram = sp.FloatTensor(V)
        bigram = sp.FloatTensor(V, V)
        trigram = sp.FloatTensor(V, V, V)

        for batch in tqdm(train_iter):

            i = batch.text.values.flatten().unsqueeze(0)
            unigram_counts = sp.FloatTensor(i, torch.ones(i.shape[1]),
                                            torch.Size([V]))

            unigram += unigram_counts

            ii = torch.stack(
                [batch.text.values[:-1, :],
                 batch.text.values[1:, :]]).view(2, -1)
            bigram_counts = sp.FloatTensor(ii, torch.ones(ii.shape[-1]),
                                           torch.Size([V, V]))
            bigram += bigram_counts

            iii = torch.stack([
                batch.text.values[:-2, :], batch.text.values[1:-1, :],
                batch.text.values[2:, :]
            ]).view(3, -1)
            trigram_counts = sp.FloatTensor(iii, torch.ones(iii.shape[-1]),
                                            torch.Size([V, V, V]))
            trigram += trigram_counts

        unigram = unigram.coalesce()
        unigram = unigram / sp.sum(unigram)

        bigram = bigram.coalesce()
        trigram = trigram.coalesce()

        bigram_df = pd.DataFrame(np.hstack([
            bigram.indices().numpy().T,
            bigram.values().numpy()[:, np.newaxis]
        ]),
                                 dtype=int,
                                 columns=['word1', 'word2', 'counts'])

        trigram_df = pd.DataFrame(
            np.hstack([
                trigram.indices().numpy().T,
                trigram.values().numpy()[:, np.newaxis]
            ]),
            dtype=int,
            columns=['word1', 'word2', 'word3', 'counts'])

        bigram_df['prob'] = (
            (bigram_df['counts'] /
             bigram_df.groupby(['word1']).transform('sum')['counts']))

        bigram_ind = torch.from_numpy(bigram_df[['word1', 'word2']].values.T)
        bigram_val = torch.from_numpy(bigram_df['prob'].values)
        bigram = torch.sparse.FloatTensor(bigram_ind, bigram_val, bigram.shape)

        trigram_df['prob'] = (
            trigram_df['counts'] /
            trigram_df.groupby(['word1', 'word2']).transform('sum')['counts'])

        trigram_ind = torch.from_numpy(trigram_df[['word1', 'word2',
                                                   'word3']].values.T)
        trigram_val = torch.from_numpy(trigram_df['prob'].values)
        trigram = torch.sparse.FloatTensor(trigram_ind, trigram_val,
                                           trigram.shape)

        self.unigram = unigram.float()
        self.bigram = bigram.float()
        self.trigram = trigram.float()
Exemplo n.º 22
0
def scsp2tsp(mat):
    return tsp.FloatTensor(
        torch.from_numpy(np.stack([mat.row, mat.col])).long(),
        torch.from_numpy(mat.data).float(), mat.shape)
def top_k_contact_metrics(features,
                          logits,
                          dist_matrices,
                          seq_lens,
                          k=5,
                          contact_range='long',
                          sequence_position_index1=0,
                          sequence_position_index2=22,
                          ang8_bin=8,
                          **kwargs):
    """Calculates metrics for the top L/k predicted contacts
    :param logits: The logits to generate probabilities from. Should have shape
                   (batch, logits, n, n).
    :type logits: torch.Tensor
    :param true_dist_mat:
    :param k:
    :param kwargs: kwargs to pas to top_k_contacts
    :return:
    """
    from src.viz import heatmap2d
    features = torch.einsum('bcij -> bijc', features)

    # Get mask from the non-positive distances of the distance matrix
    # (ignores 0 as well because those are not counted in the top-k metric according to CASP)
    mask = torch.zeros(dist_matrices.shape, dtype=torch.uint8)
    mask[dist_matrices > 0] = 1

    residue_distances = features[:, :, :,
                                 sequence_position_index1] - features[:, :, :,
                                                                      sequence_position_index2]
    residue_distances[~mask] = 0
    # Use only the bottom diagonal side of the distance matrix,
    residue_distances[residue_distances <= 0] = 0

    lower_bound, upper_bound = seperation_ranges[contact_range]
    mask[(residue_distances < lower_bound).__or__(
        residue_distances > upper_bound)] = 0

    # Turn sparse representation into dense, byte representation
    predicted_contacts = top_k_predictions_(logits,
                                            seq_lens,
                                            k=k,
                                            mask=mask,
                                            ang8_bin=ang8_bin)
    print(predicted_contacts)

    indices = torch.stack([predicted_contacts[:, 0],
                           predicted_contacts[:, 1]]).long()
    values = torch.ones(predicted_contacts.shape[0])
    predicted_contact_mat = sparse.FloatTensor(indices, values,
                                               dist_mat.size())
    predicted_contact_mat = predicted_contact_mat.to_dense().byte()

    dist_matrices[~mask] = -1
    true_contact_matrices = (dist_matrices < ang8_bin).__and__(
        dist_matrices > 0)

    # True positive, False positive, False negative calculations
    tp = len(
        predicted_contact_mat[predicted_contact_mat.__and__(true_contact_mat)])
    fp = int(predicted_contacts.shape[0]) - tp
    fn = total_contacts - tp

    # Calculate metrics
    if tp + fp == 0:
        precision = float('nan')
    else:
        precision = tp / float(tp + fp)
    if tp + fn == 0:
        recall = float('nan')
    else:
        recall = tp / float(tp + fn)
    if precision + recall == 0:
        f1 = float('nan')
    else:
        f1 = (2 * precision * recall) / (precision + recall)

    return torch.Tensor([precision, recall, f1])
Exemplo n.º 24
0
 def forward(self, adj, size):
     val = t.ones(size)
     x = sp.FloatTensor(adj, val, size=(size, size))
     x = sp.mm(x, self.weight)
Exemplo n.º 25
0
    def get_affinity(self, image, dense=False):
        output = self.features(image)

        l1 = self.intermediate_outputs[0]
        l1 = self.pool1(l1)
        l1 = self.dimreduce_1(l1)

        l2 = self.intermediate_outputs[1]
        l2 = self.dimreduce_2(l2)

        l3 = output
        l3 = self.dimreduce_3(l3)

        comb = torch.cat([l1, l2, l3], dim=1)
        comb = self.dimreduce_final(comb)
        x = comb

        self.intermediate_outputs.clear()

        if x.size(2) == self.predefined_featuresize and x.size(
                3) == self.predefined_featuresize:
            ind_from = self.ind_from
            ind_to = self.ind_to
        else:
            ind_from, ind_to = get_indices_of_pairs(5, (x.size(2), x.size(3)))
            ind_from = torch.from_numpy(ind_from)
            ind_to = torch.from_numpy(ind_to)

        x = x.view(x.size(0), x.size(1), -1)

        ff = torch.index_select(x,
                                dim=2,
                                index=ind_from.cuda(non_blocking=True))
        ft = torch.index_select(x, dim=2, index=ind_to.cuda(non_blocking=True))

        ff = torch.unsqueeze(ff, dim=2)
        ft = ft.view(ft.size(0), ft.size(1), -1, ff.size(3))

        aff = torch.abs(ft - ff)
        aff = torch.mean(aff, dim=1)
        aff = torch.exp(-aff)

        if dense:
            aff = aff.view(-1).cpu()

            ind_from_exp = torch.unsqueeze(ind_from, dim=0).expand(
                ft.size(2), -1).contiguous().view(-1)
            indices = torch.stack([ind_from_exp, ind_to])
            indices_tp = torch.stack([ind_to, ind_from_exp])

            area = x.size(2)
            indices_id = torch.stack(
                [torch.arange(0, area).long(),
                 torch.arange(0, area).long()])

            aff_mat = sparse.FloatTensor(
                torch.cat([indices, indices_id, indices_tp], dim=1),
                torch.cat([aff, torch.ones([area]), aff])).to_dense().cuda()
            return aff_mat
        else:
            return aff
Exemplo n.º 26
0
    def forward(self, x, to_dense=False, name=None, save_feature=False):

        d = super().forward_as_dict(x)

        f8_3 = F.elu(self.f8_3(d['conv4']))
        f8_4 = F.elu(self.f8_4(d['conv5']))
        f8_5 = F.elu(self.f8_5(d['conv6']))

        x = F.elu(self.f9(torch.cat([f8_3, f8_4, f8_5],
                                    dim=1)))  # [1,448,46,63]
        """ ============ save pixel feature ============ """
        if save_feature:
            if not op.exists("AFF_FEATURE_res38"):
                os.mkdir("AFF_FEATURE_res38")
            np.save(op.join("AFF_FEATURE_res38", name),
                    x.clone().cpu().numpy())

        if x.size(2) == self.predefined_featuresize and x.size(
                3) == self.predefined_featuresize:
            ind_from = self.ind_from
            ind_to = self.ind_to
        else:
            ind_from, ind_to = pyutils.get_indices_of_pairs(
                5, (x.size(2), x.size(3)))
            ind_from = torch.from_numpy(ind_from)
            ind_to = torch.from_numpy(ind_to)

        # """ ============ save aff_feature ============ """
        # print("x.shape ", x.shape)
        # np.save(file=op.join("AFF_FEATURE", name),
        #         arr=x.clone().cpu().numpy(),
        #         allow_pickle=True)
        x = x.view(x.size(0), x.size(1), -1)  # [1,448,46*63=2898]

        ff = torch.index_select(
            x, dim=2, index=ind_from.cuda(non_blocking=True))  # [1,448,46*63]
        ft = torch.index_select(
            x, dim=2, index=ind_to.cuda(non_blocking=True))  # [1,448,46*63]
        ff = torch.unsqueeze(ff, dim=2)
        ft = ft.view(ft.size(0), ft.size(1), -1, ff.size(3))  # [1,448,34,2310]
        aff = torch.exp(-torch.mean(torch.abs(ft - ff), dim=1))  # [1,34,2310]

        if to_dense:  # True
            aff = aff.view(-1).cpu()

            ind_from_exp = torch.unsqueeze(ind_from, dim=0).expand(
                ft.size(2), -1).contiguous().view(-1)  # [78540]
            indices = torch.stack([ind_from_exp, ind_to])  # [2,78540]
            indices_tp = torch.stack([ind_to, ind_from_exp])  # [2,78540]

            area = x.size(2)  # 2898

            # [2,2898]
            indices_id = torch.stack(
                [torch.arange(0, area).long(),
                 torch.arange(0, area).long()])

            # === sparse.floatTensor(coordinate,value)
            # e.g. 0 0 3
            #      3 0 5
            # coordinate =[[0 1 1],
            #    [2 0 2]]
            # value =[3,4,5]
            aff_mat = sparse.FloatTensor(
                torch.cat([indices, indices_id, indices_tp],
                          dim=1),  # coordinate
                torch.cat([aff, torch.ones([area]),
                           aff])).to_dense().cuda()  # scalar
            return aff_mat  # [2898,2898] tensor

        else:
            return aff
Exemplo n.º 27
0
    #     print(para.size())

    for t in rand_pos:
        input[t] = float(1)

    t_rand_pos = torch.from_numpy(rand_pos).long()
    row_indices = torch.zeros(edge_node).long()
    indices = torch.cat((row_indices.view(1, -1), t_rand_pos.view(1, -1)),
                        dim=0)
    v = torch.ones(indices.size()[-1])
    size = torch.Size([1, node_num])

    print(indices)
    print(v)
    print(size)
    x = ts.FloatTensor(indices, v, size)

    print(model)
    if torch.cuda.is_available():
        # sparse tensor do not support .cuda()
        print(edge_node)
        indices.cuda()
        v.cuda()
        input = x.cuda()
        print(input.dtype)
        print(type(x.cuda()))
        model.cuda()
        print(model(input))

    #     x.cuda(device=3)
    #     model.cuda(device=3)
Exemplo n.º 28
0
def load_sparse_tensor(filepath):
    x_dict = torch.load(filepath)
    return sparse.FloatTensor(x_dict['indices'], x_dict['values'],
                              x_dict['size'])
Exemplo n.º 29
0
def build_adj_matrix(corpus,
                     vocabulary,
                     num_documents,
                     doc_offset=0,
                     window_size=20):
    """
    Builds the adjacency matrix A for the text-document graph
    
    A_ij = max(0, PMI(i,j))     if i, j are words (0 if PMI <= 0)
         = TF-IDF(i,j)          if i is a document and j is a word (or opposite)
         = 1                    if i = j
         = 0                    otherwise
    
    """
    num_words = len(vocabulary)
    num_vertices = num_words + num_documents

    word_to_index = {w: i for i, w in enumerate(vocabulary)}

    # nonzero entries in the adjacency matrix
    entries = []
    # indices for the entries
    row = []
    col = []

    print('Building word frequencies per doc')
    word_freqs_per_doc = {}
    for i, doc in enumerate(tqdm(corpus)):
        doc_idx = num_words + doc_offset + i

        word_freq = {}
        text = doc.text
        for word in text:
            if word not in vocabulary:
                word = '<unk>'

            if word in word_freq:
                word_freq[word] += 1
            else:
                word_freq[word] = 1

        word_freqs_per_doc[doc_idx] = word_freq

    ### PMI calculations
    print('Building word frequencies per window')
    num_windows = 0
    word_window_occurrences = defaultdict(int)
    word_pair_window_occurrences = defaultdict(
        int)  # keys are tuples (x,y) where x < y are strings
    for doc in tqdm(corpus):
        text = doc.text
        for window_start_idx in range(max(1, len(text) - window_size + 1)):
            num_windows += 1
            window = text[window_start_idx:window_start_idx + window_size]
            distinct_words = list(set(window))
            for word in distinct_words:
                word_window_occurrences[word] += 1
            for i in range(len(distinct_words)):
                for j in range(i + 1, len(distinct_words)):
                    word1, word2 = distinct_words[i], distinct_words[j]
                    if word2 < word1:
                        word1, word2 = word2, word1
                    key = (word1, word2)
                    word_pair_window_occurrences[key] += 1

    # get rid of defaultdict behavior
    word_window_occurrences = dict(word_window_occurrences)
    word_pair_window_occurrences = dict(word_pair_window_occurrences)

    print('Calculating PMIs')
    for pair, pair_freq in tqdm(word_pair_window_occurrences.items()):
        word1, word2 = pair
        w1_idx, w2_idx = word_to_index[word1], word_to_index[word2]
        freq1 = word_window_occurrences[word1]
        freq2 = word_window_occurrences[word2]
        # PMI = P(i and j) / (P(i) * P(j)) where P(i and j) = #windows with i and j / #windows
        # and P(i) = #windows with i / #windows
        pmi = log((pair_freq * num_windows) / (freq1 * freq2))

        if pmi <= 0: continue

        entries.append(pmi)
        row.append(w1_idx)
        col.append(w2_idx)

        entries.append(pmi)
        row.append(w2_idx)
        col.append(w1_idx)

    ### TF-IDF calculations
    print('Calculating TF-IDF')
    for w_idx, word in enumerate(tqdm(vocabulary)):
        doc_occurrences = 0
        for doc_idx, freqs in word_freqs_per_doc.items():
            if word in freqs:
                doc_occurrences += 1

        if doc_occurrences == 0: continue

        idf = log(num_documents / doc_occurrences)
        for doc_idx, freqs in word_freqs_per_doc.items():
            if word in freqs and freqs[word] > 0:
                entries.append(freqs[word] * idf)
                row.append(w_idx)
                col.append(doc_idx)

                entries.append(freqs[word] * idf)
                row.append(doc_idx)
                col.append(w_idx)

    ### 1s for identities
    print('Identities')
    for i in trange(num_vertices):
        entries.append(1)
        row.append(i)
        col.append(i)

    indices = torch.LongTensor([row, col])
    entries = torch.FloatTensor(entries)
    return sparse.FloatTensor(indices, entries,
                              torch.Size([num_vertices, num_vertices]))
Exemplo n.º 30
0
idx_val = th.arange(len(x_train), len(x_train) + len(x_val)).to(device)
idx_test = th.arange(len(x_train) + len(x_val), len(x)).to(device)

k = 2
n = len(x)
p = [th.sum(y == 0), th.sum(y == 1)]  # TODO
c_in = args.c_in
c_out = args.c_out
q = np.ones([k, k]) * c_out / n
q[range(k), range(k)] *= c_in / c_out

A, _ = sbm.generate(n, p, q)
op = sps.coo_matrix(getattr(operators, args.op)(A))
idx = th.from_numpy(np.vstack([op.row, op.col])).long()
dat = th.from_numpy(op.data).float()
a = ths.FloatTensor(idx, dat, [n, n]).to(device)

network_args = getattr(args, args.network + '_args')
if hasattr(network_args, 'n_feats'):
    if network_args.n_feats is None:
        network_args.n_feats = [x.shape[1], k]
    else:
        network_args.n_feats = [x.shape[1]] + network_args.n_feats + [k]
else:
    network_args.in_feats = x.shape[1]
    network_args.out_feats = k
network = __import__(args.network).Network(**vars(network_args)).to(device)
optim_args = getattr(args, args.optim.lower() + '_args')
optimizer = getattr(optim, args.optim)(network.parameters(),
                                       **vars(optim_args))