-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Model] BPHGNN #246
base: main
Are you sure you want to change the base?
[Model] BPHGNN #246
Conversation
|
train_dataset = TextDataset('OpenHGNN/openhgnn/dataset/data/MHGCN_dataset/train.txt') | ||
valid_dataset = TextDataset('OpenHGNN/openhgnn/dataset/data/MHGCN_dataset/valid.txt') | ||
test_dataset = TextDataset('OpenHGNN/openhgnn/dataset/data/MHGCN_dataset/test.txt') | ||
|
||
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True) | ||
valid_loader = DataLoader(valid_dataset, batch_size=32, shuffle=False) | ||
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这部分代码考虑一下是否要放在 __init__
函数中
|
||
output = self.model(features=features, hg=self.hg, encode=features) | ||
|
||
loss = F.cross_entropy(output, labels) # 计算损失 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要出现中文注释
docs/source/api/model.rst
Outdated
@@ -10,7 +10,7 @@ Models | |||
{% for cls in openhgnn.models.classes %} | |||
{{ cls }} | |||
{% endfor %} | |||
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要添加无意义的空行
docs/source/api/trainerflow.rst
Outdated
@@ -10,7 +10,7 @@ Trainerflow | |||
{% for cls in openhgnn.trainerflow.classes %} | |||
{{ cls }} | |||
{% endfor %} | |||
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
main.py
Outdated
@@ -7,7 +7,7 @@ | |||
|
|||
if __name__ == '__main__': | |||
parser = argparse.ArgumentParser() | |||
parser.add_argument('--model', '-m', default='RGCN', type=str, help='name of models') | |||
parser.add_argument('--model', '-m', default='BPHGNN', type=str, help='name of models') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要修改 main.py
中默认的超参数,如果想运行自己的模型的话,可以在命令行中设置,python mian.py --model BPHGNN
@@ -28,7 +28,7 @@ class NodeClassification(BaseTask): | |||
def __init__(self, args): | |||
super(NodeClassification, self).__init__() | |||
self.logger = args.logger | |||
self.dataset = build_dataset(args.dataset, 'node_classification', logger=self.logger) | |||
self.dataset = build_dataset(args.dataset, 'node_classification', logger=self.logger) # 返回node_classificationDataset对象 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
删除中文注释
openhgnn/trainerflow/base_flow.py
Outdated
@@ -137,11 +137,11 @@ def init_feature(self, act): | |||
if isinstance(self.hg.ndata['h'], dict): | |||
# The heterogeneous contains more than one node type. | |||
input_feature = HeteroFeature(self.hg.ndata['h'], get_nodes_dict(self.hg), | |||
self.args.hidden_dim, act=act).to(self.device) | |||
self.args.hidden, act=act).to(self.device) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要修改 base_flow.py
中的默认参数名称,可将 config.ini
文件中对应的超参数名改成 hidden_dim
opt = torch.optim.Adam([{'params': self.model.parameters(), 'lr': 0.01}, {'params': log.parameters()}], lr=0.001, weight_decay=0.0005) | ||
log.to(device) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
现在优化器中的学习率使用的是固定参数,这样做不准确。需要将值设置为 config.ini
中的参数值
except: | ||
pass | ||
labels = labels.astype(np.int16) | ||
device=torch.device('cuda') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在 main.py
中已经通过 --gpu
这个参数来设置tensor的位置了,不需要重复设置
class TextDataset(Dataset): | ||
def __init__(self, filepath): | ||
self.data = [] | ||
with open(filepath, 'r') as f: | ||
for line in f: | ||
|
||
split_line = list(map(int, line.strip().split())) | ||
self.data.append(split_line) | ||
|
||
def __len__(self): | ||
return len(self.data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个类在后续代码中并没有用到,可以删除
openhgnn/dataset/BPHGNN_dataset.py
Outdated
class TextDataset(Dataset): | ||
def __init__(self, filepath): | ||
self.data = [] | ||
with open(filepath, 'r') as f: | ||
for line in f: | ||
|
||
split_line = list(map(int, line.strip().split())) | ||
self.data.append(split_line) | ||
|
||
def __len__(self): | ||
return len(self.data) | ||
|
||
def __getitem__(self, idx): | ||
|
||
data = self.data[idx] | ||
features = torch.tensor(data[:-1], dtype=torch.float32) | ||
labels = torch.tensor(data[-1], dtype=torch.long) | ||
return features, labels | ||
|
||
def load_edges(file_path): | ||
edge_data = { | ||
1: [], | ||
2: [], | ||
3: [] | ||
} | ||
|
||
with open(file_path, 'r') as f: | ||
for line in f: | ||
|
||
split_line = list(map(int, line.strip().split())) | ||
edge_type, src_id, dst_id, _ = split_line | ||
edge_data[edge_type].append((src_id, dst_id)) | ||
|
||
return edge_data | ||
|
||
def build_hetero_graph(): | ||
|
||
edge_data = load_edges('OpenHGNN/openhgnn/dataset/data/test/BPHGNN_dataset/alibaba_small/train.txt') | ||
|
||
data_dict = { | ||
('node', 'relation1', 'node'): edge_data[1], | ||
('node', 'relation2', 'node'): edge_data[2], | ||
('node', 'relation3', 'node'): edge_data[3], | ||
} | ||
|
||
hg = dgl.heterograph(data_dict) | ||
return hg | ||
|
||
|
||
train_dataset = TextDataset('OpenHGNN/openhgnn/dataset/data/BPHGNN_dataset/train.txt') | ||
valid_dataset = TextDataset('OpenHGNN/openhgnn/dataset/data/BPHGNN_dataset/valid.txt') | ||
test_dataset = TextDataset('OpenHGNN/openhgnn/dataset/data/BPHGNN_dataset/test.txt') | ||
|
||
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True) | ||
valid_loader = DataLoader(valid_dataset, batch_size=32, shuffle=False) | ||
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dataset
文件的内容可以参考其他dataset文件的编写内容,这样写不对
@@ -500,6 +502,7 @@ def __init__(self, dataset_name, *args, **kwargs): | |||
self.train_idx, self.valid_idx, self.test_idx = split_idx["train"][self.category], split_idx["valid"][ | |||
self.category], split_idx["test"][self.category] | |||
self.g, self.label_dict = dataset[0] | |||
self.BPHGNN_g = self.mag4mhgcn(dataset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bphgnn模型为什么要调用self.mag4mhgcn(dataset)
No description provided.