Skip to content

[Feature] Add RGCN for Question Generation #589

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/pytorch/math_word_problem/mawps/src/evaluation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from graph4nlp.pytorch.modules.evaluation.base import EvaluationMetricBase

import sympy
from sympy.parsing.sympy_parser import parse_expr

from graph4nlp.pytorch.modules.evaluation.base import EvaluationMetricBase


class SolutionMatch(EvaluationMetricBase):
def __init__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"config_path": "examples/pytorch/question_generation/config/squad_split2/qg.yaml",
"model_args.graph_construction_args.graph_construction_share.topology_subdir": "DependencyGraphForRGCN",
"model_args.graph_construction_args.graph_construction_private.edge_strategy": "heterogeneous",
"model_args.graph_construction_args.graph_construction_private.merge_strategy": "tailhead",
"model_args.graph_construction_args.graph_construction_private.sequential_link": true,
"model_args.graph_construction_args.graph_construction_private.as_node": false,
"model_args.graph_embedding_name": "rgcn",
"model_args.graph_embedding_args.graph_embedding_private.num_rels": 80,
"model_args.graph_embedding_args.graph_embedding_private.num_bases": 4,
"checkpoint_args.out_dir": "out/squad_split2/rgcn_dependency_ckpt"
}
7 changes: 6 additions & 1 deletion examples/pytorch/question_generation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from graph4nlp.pytorch.modules.utils.generic_utils import EarlyStopping, to_cuda
from graph4nlp.pytorch.modules.utils.logger import Logger

from examples.pytorch.semantic_parsing.graph2seq.rgcn_lib.graph2seq import RGCNGraph2Seq

from .fused_embedding_construction import FusedEmbeddingConstruction


Expand All @@ -39,7 +41,10 @@ def __init__(self, vocab, config):
]

# build Graph2Seq model
self.g2s = Graph2Seq.from_args(config, self.vocab)
if config["model_args"]["graph_embedding_name"] == "rgcn":
self.g2s = RGCNGraph2Seq.from_args(config, self.vocab)
else:
self.g2s = Graph2Seq.from_args(config, self.vocab)

if "w2v" in self.g2s.graph_initializer.embedding_layer.word_emb_layers:
self.word_emb = self.g2s.graph_initializer.embedding_layer.word_emb_layers[
Expand Down
14 changes: 4 additions & 10 deletions examples/pytorch/rgcn/rgcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ def __init__(
num_bases=None,
use_self_loop=True,
dropout=0.0,
device="cuda",
):
super(RGCN, self).__init__()
self.num_layers = num_layers
self.num_rels = num_rels
self.num_bases = num_bases
self.use_self_loop = use_self_loop
self.dropout = dropout
self.device = device

self.RGCN_layers = nn.ModuleList()

Expand Down Expand Up @@ -185,35 +183,31 @@ def __init__(
self_loop=False,
dropout=0.0,
layer_norm=False,
device="cuda",
):
super(RGCNLayer, self).__init__()
self.linear_dict = {
i: nn.Linear(input_size, output_size, bias=bias, device=device) for i in range(num_rels)
i: nn.Linear(input_size, output_size, bias=bias) for i in range(num_rels)
}
# self.linear_r = TypedLinear(input_size, output_size, num_rels, regularizer, num_bases)
self.bias = bias
self.activation = activation
self.self_loop = self_loop
self.layer_norm = layer_norm
self.device = device

# bias
if self.bias:
self.h_bias = nn.Parameter(torch.Tensor(output_size)).to(device)
self.h_bias = nn.Parameter(torch.Tensor(output_size))
nn.init.zeros_(self.h_bias)

# TODO(minjie): consider remove those options in the future to make
# the module only about graph convolution.
# layer norm
if self.layer_norm:
self.layer_norm_weight = nn.LayerNorm(
output_size, elementwise_affine=True, device=device
)
self.layer_norm_weight = nn.LayerNorm(output_size, elementwise_affine=True)

# weight for self loop
if self.self_loop:
self.loop_weight = nn.Parameter(torch.Tensor(input_size, output_size)).to(device)
self.loop_weight = nn.Parameter(torch.Tensor(input_size, output_size))
nn.init.xavier_uniform_(self.loop_weight, gain=nn.init.calculate_gain("relu"))

self.dropout = nn.Dropout(dropout)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"config_path": "examples/pytorch/semantic_parsing/graph2seq/config/dependency_rgcn_undirected.yaml",
"model_args.graph_embedding_args.graph_embedding_share.direction_option": "bi_sep",
"training_args.log_file": "examples/pytorch/semantic_parsing/graph2seq/log/dependency_rgcn_bi_sep.txt"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"config_path": "examples/pytorch/semantic_parsing/graph2seq/config/dependency_rgcn_undirected.yaml",
"model_args.graph_embedding_args.graph_embedding_share.direction_option": "undirected",
"training_args.log_file": "examples/pytorch/semantic_parsing/graph2seq/log/dependency_rgcn_undirected.txt"
}
2 changes: 1 addition & 1 deletion examples/pytorch/semantic_parsing/graph2seq/main_rgcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _build_logger(self, log_file):
import os

log_folder = os.path.split(log_file)[0]
if not os.path.exists(log_file):
if not os.path.exists(log_folder):
os.makedirs(log_folder)
self.logger = get_log(log_file)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from graph4nlp.pytorch.models.graph2seq import Graph2Seq

from examples.pytorch.rgcn.rgcn import RGCN
# from examples.pytorch.rgcn.rgcn import RGCN
from graph4nlp.pytorch.modules.graph_embedding_learning.rgcn import RGCN


class RGCNGraph2Seq(Graph2Seq):
Expand Down Expand Up @@ -74,10 +74,12 @@ def __init__(

def _build_gnn_encoder(
self,
gnn,
num_layers,
input_size,
hidden_size,
output_size,
direction_option,
feats_dropout,
gnn_num_rels=80,
gnn_num_bases=4,
Expand All @@ -89,6 +91,8 @@ def _build_gnn_encoder(
hidden_size,
output_size,
num_rels=gnn_num_rels,
num_bases=gnn_num_bases,
dropout=feats_dropout,
direction_option=direction_option,
# num_bases=gnn_num_bases,
# dropout=feats_dropout,
feat_drop=feats_dropout,
)
2 changes: 1 addition & 1 deletion graph4nlp/pytorch/models/graph2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Graph2Seq(Graph2XBase):
>>> "It is just a how-to-use example."
>>> from graph4nlp.pytorch.modules.config import get_basic_args
>>> opt = get_basic_args(graph_construction_name="node_emb", graph_embedding_name="gat", decoder_name="stdrnn")
>>> graph2seq = Graph2Seq.from_args(opt=opt, vocab_model=vocab_model, device=torch.device("cuda:0"))
>>> graph2seq = Graph2Seq.from_args(opt=opt, vocab_model=vocab_model)
>>> batch_graph = [GraphData() for _ in range(2)]
>>> tgt_seq = torch.Tensor([[1, 2, 3], [4, 5, 6]])
>>> seq_out, _, _ = graph2seq(batch_graph=batch_graph, tgt_seq=tgt_seq)
Expand Down
Loading