Giter Club home page Giter Club logo

deephypergraph's People

Contributors

beating-1224 avatar hputx813 avatar jasonyanjl avatar jiishy avatar starrah avatar weiyx15 avatar yifanfeng97 avatar zhangxwww avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

deephypergraph's Issues

您好在使用dhg.models.HGNN出现torch.sparse_coo_tensor的索引超出我的张量尺寸范围,具体的bug如下

Traceback (most recent call last):
File "train.py", line 355, in
main()
File "train.py", line 195, in main
train(opt, train_loader, model, epoch, val_loader)
File "train.py", line 244, in train
model.train_emb(images, captions, lengths,image_lengths=img_lengths)
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/torch/amp/autocast_mode.py", line 14, in decorate_autocast
return func(*args, **kwargs)
File "/work/shared_pool_data3/Code/CHAN-main/lib/model.py", line 262, in train_emb
img_emb, cap_emb = self.forward_emb(images, captions,lengths, image_lengths=image_lengths)
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/torch/amp/autocast_mode.py", line 14, in decorate_autocast
return func(*args, **kwargs)
File "/work/shared_pool_data3/Code/CHAN-main/lib/model.py", line 199, in forward_emb
img_emb = self.img_hy(img_embhy,G)
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl
return forward_call(*input, **kwargs)
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/dhg/models/hypergraphs/hgnn.py", line 44, in forward
X = layer(X, hg)
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1190, in _call_impl
return forward_call(*input, **kwargs)
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/dhg/nn/convs/hypergraphs/hgnn_conv.py", line 56, in forward
X = hg.smoothing_with_HGNN(X).to(device)
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/dhg/structure/hypergraphs/hypergraph.py", line 1221, in smoothing_with_HGNN
L_HGNN = self.L_HGNN
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/dhg/structure/hypergraphs/hypergraph.py", line 1180, in L_HGNN
_tmp = self.D_v_neg_1_2.mm(self.H).mm(self.W_e).mm(self.D_e_neg_1).mm(self.H_T,).mm(self.D_v_neg_1_2)
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/dhg/structure/hypergraphs/hypergraph.py", line 929, in D_v_neg_1_2
_mat = self.D_v.clone()
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/dhg/structure/hypergraphs/hypergraph.py", line 863, in D_v
_tmp = [self.D_v_of_group(name)._values().clone() for name in self.group_names]
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/dhg/structure/hypergraphs/hypergraph.py", line 863, in
_tmp = [self.D_v_of_group(name)._values().clone() for name in self.group_names]
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/dhg/structure/hypergraphs/hypergraph.py", line 881, in D_v_of_group
H = self.H_of_group(group_name).clone()
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/dhg/structure/hypergraphs/hypergraph.py", line 787, in H_of_group
self.group_cache[group_name]["H"] = self.H_v2e_of_group(group_name)
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/dhg/structure/base.py", line 856, in H_v2e_of_group
self.group_cache[group_name]["H_v2e"] = self._fetch_H_of_group("v2e", group_name)
File "/home/iot/shared_pool_data3/shared_pool_data3/anaconda3/envs/CHANPY38/lib/python3.8/site-packages/dhg/structure/base.py", line 560, in _fetch_H_of_group
H = torch.sparse_coo_tensor(
RuntimeError: size is inconsistent with indices: for dim 0, size is 144 but found index 144

对源码的修改:HGNNconv forward改变了X的格式为float()以及更改了smoothingwithHGNN的设备,如下:
class HGNNConv(nn.Module):
r"""The HGNN convolution layer proposed in Hypergraph Neural Networks <https://arxiv.org/pdf/1809.09401>_ paper (AAAI 2019).
Matrix Format:

.. math::
    \mathbf{X}^{\prime} = \sigma \left( \mathbf{D}_v^{-\frac{1}{2}} \mathbf{H} \mathbf{W}_e \mathbf{D}_e^{-1} 
    \mathbf{H}^\top \mathbf{D}_v^{-\frac{1}{2}} \mathbf{X} \mathbf{\Theta} \right).

where :math:`\mathbf{X}` is the input vertex feature matrix, :math:`\mathbf{H}` is the hypergraph incidence matrix, 
:math:`\mathbf{W}_e` is a diagonal hyperedge weight matrix, :math:`\mathbf{D}_v` is a diagonal vertex degree matrix, 
:math:`\mathbf{D}_e` is a diagonal hyperedge degree matrix, :math:`\mathbf{\Theta}` is the learnable parameters.

Args:
    ``in_channels`` (``int``): :math:`C_{in}` is the number of input channels.
    ``out_channels`` (int): :math:`C_{out}` is the number of output channels.
    ``bias`` (``bool``): If set to ``False``, the layer will not learn the bias parameter. Defaults to ``True``.
    ``use_bn`` (``bool``): If set to ``True``, the layer will use batch normalization. Defaults to ``False``.
    ``drop_rate`` (``float``): If set to a positive number, the layer will use dropout. Defaults to ``0.5``.
    ``is_last`` (``bool``): If set to ``True``, the layer will not apply the final activation and dropout functions. Defaults to ``False``.
"""

def __init__(
    self,
    in_channels: int,
    out_channels: int,
    bias: bool = True,
    use_bn: bool = False,
    drop_rate: float = 0.5,
    # drop_rate: float = 0,
    is_last: bool = False,
):
    super().__init__()
    self.is_last = is_last
    self.bn = nn.BatchNorm1d(out_channels) if use_bn else None
    self.act = nn.ReLU(inplace=True)
    self.drop = nn.Dropout(drop_rate)
    self.theta = nn.Linear(in_channels, out_channels, bias=bias)

def forward(self, X: torch.Tensor, hg: Hypergraph) -> torch.Tensor:
    r"""The forward function.

    Args:
        X (``torch.Tensor``): Input vertex feature matrix. Size :math:`(N, C_{in})`.
        hg (``dhg.Hypergraph``): The hypergraph structure that contains :math:`N` vertices.
    """
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    X = self.theta(X)
    X=X.float()
    X = hg.smoothing_with_HGNN(X).to(device)
    if not self.is_last:
        X = self.act(X)
        if self.bn is not None:
            X = self.bn(X)
            
        X = self.drop(X)
    return X

请问大佬有没有办法解决这个问题

可视化部分draw_in_poincare_ball(), Norm 运算产生0,导致除法运算报错

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
Screen Shot 2022-09-26 at 11 03 10

Screen Shot 2022-09-26 at 11 02 04

Environment (please complete the following information):

  • OS: [e.g. Ubuntu 18.04]
  • Python: [e.g. 3.8.13]
  • PyTorch [e.g. 1.11.0]
  • DHG [e.g. 0.9.1]

Additional context
Add any other context about the problem here.

Dataset: Coauthor network from DBLP

Where is the dataset from? Please describe.
It is crawled according to DBLP API with some conditions.

Describe the dataset
The dataset is a part of the dataset crawled according to DBLP API, and we have selected each item based on some conditions, such as the venue and publication year (from 2018 to 2022). It contains 6498 authors and 2603 papers.

Download link
Where to access the dataset. Ex. a cloud drive link or any other website.

Additional context
Add any other context or dataset request here.

如何利用DHG实现整图分类任务

你好,你们的工作让我受益良多,我目前有两类数据(每个样本都具有该两类数据),计算了它们之间的相关性从而得到了网络结构,想使用你们提供的DHG来实现一个整图分类的工作,但是目前好像只提供了节点分类的功能,请问有什么便捷的方法能让我快速实现该想法?非常感谢。

如何获取训练后节点的向量?

import torch
import torch.nn as nn
import torch.optim as optim

from dhg import Hypergraph
from dhg.data import Cooking200
from dhg.models import HGNNP
from dhg.random import set_seed
from dhg.experiments import HypergraphVertexClassificationTask as Task
from dhg.metrics import HypergraphVertexClassificationEvaluator as Evaluator
def structure_builder(trial):
global hg_base, g
cur_hg: Hypergraph = hg_base.clone()
return cur_hg

def model_builder(trial):
return HGNNP(dim_features, trial.suggest_int("hidden_dim", 10, 20), num_classes, use_bn=True)

def train_builder(trial, model):
optimizer = optim.Adam(
model.parameters(),
lr=trial.suggest_loguniform("lr", 1e-4, 1e-2),
weight_decay=trial.suggest_loguniform("weight_decay", 1e-4, 1e-2),
)
criterion = nn.CrossEntropyLoss()
return {
"optimizer": optimizer,
"criterion": criterion,
}
if name == "main":
work_root = "hypergraph/tmp"
set_seed(2022)
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
data = Cooking200()
dim_features = data["num_vertices"]
num_classes = data["num_classes"]
hg_base = Hypergraph(data["num_vertices"], data["edge_list"])
print(data["labels"][0])
print(data["val_mask"].len())
input_data = {
"features": torch.eye(data["num_vertices"]),
"labels": data["labels"],
"train_mask": data["train_mask"],
"val_mask": data["val_mask"],
"test_mask": data["test_mask"],
}
evaluator = Evaluator(["accuracy", "f1_score", {"f1_score": {"average": "micro"}}])
task = Task(
work_root, input_data, model_builder, train_builder, evaluator, device, structure_builder=structure_builder,
)
task.run(200, 50, "maximize")

超图的顶点度矩阵D_v

如果生成一个5个顶点以及3条边的超图,超边的权重为2,3,4
hg = dhg.Hypergraph(5, [(0, 1, 2), (2, 3), (0, 4)], [2.0, 3.0, 4.0])
dhg中计算得到的D_v为 :
tensor([[2., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 2., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
但是按照实际的定义以及论文“Hypergraph Neural Networks”所展示的:
For a vertex v ∈ V, its degree is defined as d(v) =Σ ω(e)h(v, e)

超图如何采集负样本?

您好,有个问题想向您请教下。

对于超图的监督学习,当采用二值化交叉熵损失函数,负样本空间中的组合情况特别多,这时负的超边该如何采样呢?

谢谢!

mini-batches training for node classification on hypergraph

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
Hi, I am curious if it is possible to train hypergraph for node classification in mini-batches setting. Because now the graph data is growing bigger and bigger, training it in full-batch with hypergraph might not fit the GPU memory anymore. One direct way in my head is to chunk the incidence matrix but it may not work well if the message scheme is "node->hyperedge->node" like HGNN+? Hence, I want to ask if you have any suggestions on this problem.

Describe the solution you'd like
Some smarter ways to chunk the incidence matrix or some other methods?

Thanks in advance!

Node classification on multiple hypergraphs

Is your feature request related to a problem? Please describe.
I am trying to perform node classification on multiple hypergraph , let's say I have multiple hypergraphs 80% for training and I want to test on the remaining 20% hypergraphs. Can I do that?

Describe the solution you'd like
Maybe a dataloader that can arrange the hypergraph in one big hypergraph?

关于UniGCN在计算超边的度的平均值,这是否是一个bug

图片
这里计算超边e的度的平均值时用的_De.scatter_reduce函数,在指定mean求平均时,假如有两个值被分配到某个索引,由于_De本身的值也被计算了,因此结果是三个数的平均(_De的初始值也被平均了,即便是0),下面用例子说明

import torch

# 源张量
src = torch.tensor([1.0, 2.0, 3.0, 4.0])

# 索引张量
index = torch.tensor([0, 1, 2, 2])
De = torch.zeros(3)

# 使用 scatter_reduce 函数
result = De.scatter_reduce(0, index=index, src=src, reduce="sum")
print(result)
# 输出 tensor([1., 2., 7.])
# index    src
#  0       1.0
#  1       2.0
#  2       3.0
#  2       4.0
# [1,  2,  3+4]  求和


result = De.scatter_reduce(0, index=index, src=src, reduce="mean")
print(result)
# 输出 tensor([0.5000, 1.0000, 2.3333])
# index    src
#  0       1.0
#  1       2.0
#  2       3.0
#  2       4.0
# 期望值 [1/1,  2/1,  (3+4)/2]  
# 输出值 [(0+1)/2,  (0+2)/2,  (0+3+4)/3]

大型图的处理

当处理图级的超图分类问题,如果每个超图的节点和超边数量较多,batch加速处理对邻接矩阵进行类似dgl.batch的拼接方式时,似乎并不支持很大的batch_size,如何优化?或者在计算速度和显存之间取舍?

关于稠密图数据用于训练

您好,我在使用库中gcn的模型进行稠密图的训练的时候发现效率非常低,在用github内另一个引用同一个文献的gcn实现的代码时算法效率并没有这么低。是优化还是什么别的原因吗?

adaptive fusion和coequal fusion的文档问题

你好,你们的工作让我受益匪浅,最近也在使用你们的工具进行一些超图的实验,但针对于论文《HGNN+》期刊中的adaptive fusion策略,我并没有找到相应的文档信息,代码中也并没有找到相应的例子,可能是我寻找方式不对,但俺确实是找不到,求大佬指点迷津

Didn't concat node's vector with hyperedge's vector before computing the attention of UniGATConv

Describe the bug
In the paper 'UniGNN: a Unified Framework for Graph and Hypergraph Neural Networks,' the input to the attention module of UniGAT is the vector obtained by concatenating the node vector and the hyperedge vector. However, during the implementation of the approach, in the forward function of UniGATConv, the input for atten_e is the hyperedge vector alone, rather than the vector formed by concatenating the node vector and the hyperedge vector.

I appreciate your contributions to such a valuable project, which has been immensely helpful to me in studying hypergraph techniques. Thank you for your attention.

Optuna version 1.10 is not exist

Describe the bug
During installation, I got an error related to optuna package: optuna<2 and >=1.10 is required, but optuna has not version number equal 1.10, the next version after 1.5, is 2.x.

Environment (please complete the following information):

  • Python: [3.8.1]
  • PyTorch [1.12.1]
  • DHG [0.9.0]

Additional context
By modifying pyproject.toml and changed optuna requirements to
optuna = "^1.5"
the problem is solved

About Directed Hypergraph

The feature is related to the feature of Directed Hypergraph. I noticed that you have scheduled the release of the structure of directed hypergraph already. I'm wondering if you have already finished / almost finished the implimentation? I'm currently looking into a partial implimentation limited to bring HyperGCN into directed hypergraph, but if you guys already finished the part, I would really like to take your implimentation as the solution.

Thank you for your contribution to Hypergraph Learning.

超图实现具有不同超边权重的超图卷积模型

您好,我想请问下。在dhg文档中,有份示例代码,是用于构造具有不同超边权重的超图卷积模型(代码附在下面)。我想问的是,这个超边特征Y是不是得先进将顶点信息传递到边后,即先调用 hg.v2e才可以拿到这个超边特征。
class HGATConv(nn.Module):
def init(
self,
in_channels: int,
out_channels: int,
bias: bool = True,
drop_rate: float = 0.5,
atten_neg_slope: float = 0.2,
):
super().init()
self.atten_dropout = nn.Dropout(drop_rate)
self.atten_act = nn.LeakyReLU(atten_neg_slope)
self.act = nn.ELU(inplace=True)
self.theta_vertex = nn.Linear(in_channels, out_channels, bias=bias)
self.theta_hyperedge = nn.Linear(in_channels, out_channels, bias=bias)
self.atten_vertex = nn.Linear(out_channels, 1, bias=False)
self.atten_hyperedge = nn.Linear(out_channels, 1, bias=False)

def forward(self, X: torch.Tensor, Y: torch.Tensor, hg: dhg.Hypergraph) -> torch.Tensor:
    X = self.theta_vertex(X)
    Y = self.theta_hyperedge(Y)
    x_for_vertex = self.atten_vertex(X)
    y_for_hyperedge = self.atten_hyperedge(Y)
    v2e_atten_score = x_for_vertex[hg.v2e_src] + y_for_hyperedge[hg.v2e_dst]
    e2v_atten_score = y_for_hyperedge[hg.e2v_src] + x_for_vertex[hg.e2v_dst]
    v2e_atten_score = self.atten_dropout(self.atten_act(v2e_atten_score).squeeze())
    e2v_atten_score = self.atten_dropout(self.atten_act(e2v_atten_score).squeeze())
    Y_ = hg.v2e(X, aggr="softmax_then_sum", v2e_weight=v2e_atten_score)
    X_ = hg.e2v(Y_, aggr="softmax_then_sum", e2v_weight=e2v_atten_score)
    X_ = self.act(X_)
    Y_ = self.act(Y_)
    return X_

The issue in segmentation

Your work is amazing! I think it is helpful for me! Thank you very much!
There is a segmentation network for heart mri in your example work. However, I wonder why the lable is two-dimensional as the heart mri is three_dimensional? How do you process the lable?

CoauthorshipCora dataset label error

Sorry, my fault, I not noticed the target has been processed, please delete this issue.

#--------------------------------
Here is my code:

data = dhg.data.CoauthorshipCora()
print(data['labels'][data['train_mask']])

Print output as follow:

tensor([7, 4, 5, 1, 7, 5, 6, 4, 4, 4, 4, 2, 4, 2, 1, 3, 3, 5, 6, 6, 7, 1, 5, 4,
1, 7, 7, 7, 1, 7, 3, 1, 6, 6, 5, 7, 7, 1, 4, 6, 6, 1, 5, 6, 6, 4, 7, 3,
1, 3, 4, 5, 1, 1, 3, 3, 1, 3, 6, 3, 1, 5, 1, 5, 2, 7, 1, 1, 3, 3, 5, 5,
4, 7, 4, 1, 1, 1, 2, 1, 2, 1, 5, 6, 7, 4, 5, 4, 6, 3, 1, 6, 0, 7, 1, 4,
4, 1, 1, 6, 7, 7, 6, 5, 6, 1, 1, 4, 7, 1, 3, 7, 1, 1, 3, 1, 2, 4, 4, 0,
5, 6, 4, 6, 1, 5, 1, 7, 5, 1, 7, 2, 4, 1, 6, 4, 1, 3, 5, 6])

The labels has "7", it contains 0 to 7, but the data['num_classes'] is 7, the labels should be 0 to 6.
Please let me know if anything wrong.

kindly need help to for several reproduction results

Discussed in #23

Originally posted by ShuaiWang97 December 5, 2022
To the community,

Hope you had a great weekend.
Thank you so much for building this package! I am quite interested in hypergraph and learned a lot from the tutorial and source code. I tried to use the method and dataset from the package to reproduce several results. The performance on co-authorship dataset seems good but the performance on cocitation datasets seems a bit low. I checked the implements several times but did not find any problem. Can anyone please help me a bit?

The accuracy score on node classification of several cocitation datasets (CocitationCora,CocitationCiteseer,CocitationPubmed) by HGNN, HyperGCN, HGNN+ are followed and the code is attached. The way I change datasets and methods are just to change data and net variable. Any ideas are incredibly welcome. Thanks in advance.

image

import time
from copy import deepcopy

import torch
import torch.optim as optim
import torch.nn.functional as F

from dhg import Hypergraph,Graph
from dhg.data import Cooking200, CoauthorshipCora,CocitationCora,CocitationCiteseer,CoauthorshipDBLP, CocitationPubmed,\
                     Citeseer,Cora,Pubmed
from dhg.models import HGNN, HyperGCN, HGNNP
from dhg.random import set_seed
from dhg.metrics import HypergraphVertexClassificationEvaluator as Evaluator

from data import data
#from config import config



def train(net, X, A, lbls, train_idx, optimizer, epoch):
    net.train()

    st = time.time()
    optimizer.zero_grad()

    # import the data["features"] X and Graph structure G 
    outs = net(X, A)
    outs, lbls = outs[train_idx], lbls[train_idx]
    loss = F.cross_entropy(outs, lbls)
    #loss = F.nll_loss(outs, lbls) # decrease performance a lot
    loss.backward()
    optimizer.step()
    print(f"Epoch: {epoch}, Time: {time.time()-st:.5f}s, Loss: {loss.item():.5f}")
    return loss.item()


@torch.no_grad()
def infer(net, X, A, lbls, idx, test=False):
    net.eval()
    outs = net(X, A)
    outs, lbls = outs[idx], lbls[idx]
    if not test:
        res = evaluator.validate(lbls, outs)
    else:
        res = evaluator.test(lbls, outs)
    return res



if __name__ == "__main__":
    set_seed(2021)
    #args = config.parse()
    device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
    evaluator = Evaluator(["accuracy", "f1_score", {"f1_score": {"average": "micro"}}])
    
    # Load dataset of CocitationCiteseer, CocitationCora, CocitationPubmed
    #data = CocitationCora()
    data = CocitationCiteseer()

    # Build the hypergraph dataloader
    X, lbl = data["features"], data["labels"]
    HG = Hypergraph(data["num_vertices"], data["edge_list"])

    #net = HGNNP(data["dim_features"], 16, data["num_classes"], use_bn=False)
    net = HGNN(data["dim_features"], 16, data["num_classes"], use_bn=False)

    print("net is: ", net)
    optimizer = optim.Adam(net.parameters(), lr=0.01, weight_decay=0.0005)

    train_mask = data["train_mask"]
    val_mask = data["val_mask"]
    test_mask = data["test_mask"]
    print(f"length of train is : {sum(train_mask)}, length of val is: {sum(val_mask)},length of test is: {sum(test_mask)}")

    X, lbl = X.to(device), lbl.to(device)
    HG = HG.to(device)
    net = net.to(device)

    best_state = None
    best_epoch, best_val = 0, 0
    for epoch in range(200):
        # train
        train(net, X, HG, lbl, train_mask, optimizer, epoch)
        # validation
        if epoch % 10 == 0:
            with torch.no_grad():
                val_res = infer(net, X, HG, lbl, val_mask)
                print("val acc is: ",infer(net, X, HG, lbl, val_mask,test=True)["accuracy"])

                print("val_res is: ",val_res)
            if val_res > best_val:
                print(f"update best: {val_res:.5f}")
                best_epoch = epoch
                best_val = val_res
                best_state = deepcopy(net.state_dict())
    print("\ntrain finished!")
    print(f"best val: {best_val:.5f}")
    # test
    print("test...")
    net.load_state_dict(best_state)
    res = infer(net, X, HG, lbl, test_mask, test=True)
    print(f"final result: epoch: {best_epoch}")
    print(res)

Best,
Shuai

我针对你的超图写了个邻接矩阵的接口,想请老哥帮忙看看写的对不对?我自己也做了个些测试,输出的挺对,你介意合并不?

超图的邻接矩阵

我检查了好几遍,好像没发现超图的邻接矩阵实现,自己写了下,感谢大佬指点

# 这是希望在你hypergraph源代码希望增加的部分
@property
    def A(self) -> torch.Tensor:
        r"""return the hyper-graph adjacency matrix :math:`mathbf{A}` with ``torch.sparse_coo_tensor`` format
        """
        if self.cache.get("A", None) is None:
            if self.num_e == 0:
                self.cache["A"] = torch.sparse_coo_tensor(
                    size=(self.num_v, self.num_v), device=self.device)
            else:
                self.cache["A"] = torch.Tensor.mm(self.H, self.H_T) - self.D_v
        return self.cache["A"]

# **我自己写的邻接矩阵测试代码**

import matplotlib.pyplot as plt
import torch
from dhg.structure import Hypergraph
from typing import Optional, Union, List


class SingleLayerHyperNetwork(Hypergraph):
    def __init__(
            self,
            num_v: int,
            e_list: Optional[Union[List[int], List[List[int]]]] = None,
            e_weight: Optional[Union[float, List[float]]] = None,
            device: torch.device = torch.device("cpu")
    ):
        super(
            SingleLayerHyperNetwork,
            self).__init__(
            num_v,
            e_list,
            e_weight,
            device=device)

    def __repr__(self):
        return f"HyperG(num_v={self.num_v}, num_e={self.num_e})"

    @property
    def A(self) -> torch.Tensor:
        r"""return the hyper-graph adjacency matrix :math:`mathbf{A}` with ``torch.sparse_coo_tensor`` format
        """
        if self.cache.get("A", None) is None:
            if self.num_e == 0:
                self.cache["A"] = torch.sparse_coo_tensor(
                    size=(self.num_v, self.num_v), device=self.device)
            else:
                self.cache["A"] = torch.Tensor.mm(self.H, self.H_T) - self.D_v
        return self.cache["A"]

if __name__ == '__main__':
    hg = SingleLayerHyperNetwork(5, [(0, 1, 2), (2, 3), (0, 1, 4)])
    print(hg.A.to_dense())
output:
      tensor([[0., 2., 1., 0., 1.],
              [2., 0., 1., 0., 1.],
              [1., 1., 0., 1., 0.],
              [0., 0., 1., 0., 0.],
              [1., 1., 0., 0., 0.]])

pip install dhg时出现error:'The 'sklearn' PyPI package is deprecated, use 'scikit-learn' rather than 'sklearn' for pip commands.'

在虚拟环境中安装dhg包时,在Collecting sklearn (from dhg)过程中出现error:subprocess-exited-with-error,详细情况如下:


Collecting sklearn (from dhg)
Using cached sklearn-0.0.post12.tar.gz (2.6 kB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [15 lines of output]
The 'sklearn' PyPI package is deprecated, use 'scikit-learn'
rather than 'sklearn' for pip commands.

  Here is how to fix this error in the main use cases:
  - use 'pip install scikit-learn' rather than 'pip install sklearn'
  - replace 'sklearn' by 'scikit-learn' in your pip requirements files
    (requirements.txt, setup.py, setup.cfg, Pipfile, etc ...)
  - if the 'sklearn' package is used by one of your dependencies,
    it would be great if you take some time to track which package uses
    'sklearn' instead of 'scikit-learn' and report it to their issue tracker
  - as a last resort, set the environment variable
    SKLEARN_ALLOW_DEPRECATED_SKLEARN_PACKAGE_INSTALL=True to avoid this error

  More information is available at
  https://github.com/scikit-learn/sklearn-pypi-package
  [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

本虚拟环境中python版本为3.11,pytorch版本为2.2.1。

How to use HGNN+ for RL

Hello,

Is there a way to use HGNN+ for Reinforcement Learning using batch training? The only examples I see are doing classification tasks with labels and do not show how to use your library for RL.

Thanks

How to get a directed hypergraph

It is a really great project!
However, I met some issues in the process. Is this package able to deal with a directed hypergraph or not? Because in the hypergraph, I still want to remain the directed graph structure.

关于超图子图的接口实现

根据超图节点集进行超图子图的抽取
是否可以实现这样一个关于子图的函数,用户输入这个超图有关的节点集,就能返回包含这个节点集的超图子图。
关于超边的选择方面,一种策略是可以保留全空超边,另一种策略是只保留至少包含一个节点的超边。

torch.topk 函数设置 largest=False 是为了找到最小的距离,即最近的邻居。这种方法可能比转换张量为 numpy 数组并使用 scipy.spatial.cKDTree 更有效,因为它避免了 CPU 和 GPU 之间的数据传输。

@staticmethod
def _e_list_from_feature_kNN(features: torch.Tensor, k: int):
r"""Construct hyperedges from the feature matrix. Each hyperedge in the hypergraph is constructed by the central vertex and its :math:k-1 neighbor vertices.

Args:
    ``features`` (``torch.Tensor``): The feature matrix.
    ``k`` (``int``): The number of nearest neighbors.
"""
assert features.ndim == 2, "The feature matrix should be 2-D."
assert (
    k <= features.shape[0]
), "The number of nearest neighbors should be less than or equal to the number of vertices."

dist_matrix = torch.cdist(features, features, p=2)
_, nbr_indices = torch.topk(dist_matrix, k, largest=False)

return nbr_indices.tolist()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.