Example #1
0
    def __init__(self, vertex_embedding_dims, message_dims, h_dims):
        AbstractTorchModule.__init__(self)

        gates = []
        baselines = []
        for v_dim, m_dim, h_dim in zip(vertex_embedding_dims, message_dims,
                                       h_dims):
            gate_input_shape = [v_dim, m_dim, v_dim]
            gate = torch.nn.Sequential(
                MultipleInputsLayernormLinear(gate_input_shape, h_dim), ReLU(),
                Linear(h_dim, 1), Squeezer(), HardConcrete())

            gates.append(gate)

            baseline = torch.FloatTensor(m_dim)
            stdv = 1. / math.sqrt(m_dim)
            baseline.uniform_(-stdv, stdv)
            baseline = torch.nn.Parameter(baseline, requires_grad=True)

            baselines.append(baseline)

        gates = torch.nn.ModuleList(gates)
        self.gates = gates

        baselines = torch.nn.ParameterList(baselines)
        self.baselines = baselines

        # Initially we cannot update any parameters. They should be enabled layerwise
        for parameter in self.parameters():
            parameter.requires_grad = False
Example #2
0
    def __init__(self, vertex_embedding_dims, message_dims, n_relations, h_dims):
        AbstractTorchModule.__init__(self)

        self.n_relations = n_relations

        self.hard_gates = HardConcrete()

        transforms = []
        baselines = []
        for v_dim, m_dim, h_dim in zip(vertex_embedding_dims, message_dims, h_dims):
            transform_src = torch.nn.Sequential(
                Linear(v_dim, h_dim),
                ReLU(),
                Linear(h_dim, m_dim * n_relations),
            )

            transforms.append(transform_src)

            baseline = torch.FloatTensor(m_dim)
            stdv = 1. / math.sqrt(m_dim)
            baseline.uniform_(-stdv, stdv)
            baseline = torch.nn.Parameter(baseline, requires_grad=True)

            baselines.append(baseline)

        transforms = torch.nn.ModuleList(transforms)
        self.transforms = transforms

        baselines = torch.nn.ParameterList(baselines)
        self.baselines = baselines

        # Initially we cannot update any parameters. They should be enabled layerwise
        for parameter in self.parameters():
            parameter.requires_grad = False
Example #3
0
    def __init__(self, num_edges, num_layers, m_dim):
        AbstractTorchModule.__init__(self)

        self.hard_concrete = HardConcrete()

        gates = []
        baselines = []
        for layer in range(num_layers):
            gate_input = torch.FloatTensor(num_edges)
            gate_input.normal_(-1, 1)
            gate_input = torch.nn.Parameter(gate_input, requires_grad=True)

            gates.append(gate_input)

            baseline = torch.FloatTensor(m_dim)
            stdv = 1. / math.sqrt(m_dim)
            baseline.uniform_(-stdv, stdv)
            baseline = torch.nn.Parameter(baseline, requires_grad=True)

            baselines.append(baseline)

        gates = torch.nn.ParameterList(gates)
        self.gates = gates

        baselines = torch.nn.ParameterList(baselines)
        self.baselines = baselines

        # Initially we cannot update any parameters. They should be enabled layerwise
        for parameter in self.parameters():
            parameter.requires_grad = False
Example #4
0
    def __init__(self, configuration):
        AbstractTorchModule.__init__(self)
        self.n_classes = configuration["task"]["n_colours"]
        self.gcn_dim = configuration["task"]["gcn_dim"]

        self.initial_transform = torch.nn.Sequential(
            Linear(2 * self.n_classes, self.gcn_dim),
            LayerNorm(self.gcn_dim),
            torch.nn.ReLU(),
            Linear(self.gcn_dim, self.gcn_dim),
            LayerNorm(self.gcn_dim),
            torch.nn.ReLU(),
        )

        self.final_transform = torch.nn.Sequential(
            Linear(self.gcn_dim, self.gcn_dim * 2),
            LayerNorm(self.gcn_dim * 2), torch.nn.ReLU(),
            Linear(self.gcn_dim * 2, self.gcn_dim), LayerNorm(self.gcn_dim),
            torch.nn.ReLU(), Linear(self.gcn_dim, 1))

        self.loss = torch.nn.BCEWithLogitsLoss(reduction="none")

        self.gnn = RGCN(
            self.gcn_dim,
            self.gcn_dim,
            n_relations=self.n_classes,
            n_layers=configuration["model_parameters"]["gnn_layers"],
            inverse_edges=False)
Example #5
0
    def __init__(self, configuration):
        AbstractTorchModule.__init__(self)

        self.configuration = configuration

        pos_index = create_voc(
            'file', configuration["task"]["voc_folder"] + "/pos.voc.conll2009")
        word_index = create_voc(
            'file',
            configuration["task"]["voc_folder"] + "/words.voc_unk.conll2009")
        word_index.add_unks()
        frame_index = create_voc(
            'file',
            configuration["task"]["voc_folder"] + "/frames.voc.conll2009")
        role_index = create_voc(
            'file',
            configuration["task"]["voc_folder"] + "/labels.voc.conll2009")

        self.pretrained_word_embedding_tensor = parse_word_embeddings(
            configuration["task"]["voc_folder"] +
            "/word_embeddings_proper.sskip.conll2009.txt")
        print("Warning: Rolling pretrained word embeddings to fix indexes")
        self.pretrained_word_embedding_tensor = np.roll(
            self.pretrained_word_embedding_tensor, 1, axis=0)

        if configuration["task"]["use_lstm"]:
            self.lstm = torch.nn.LSTM(input_size=317,
                                      hidden_size=512,
                                      num_layers=4,
                                      batch_first=True,
                                      bidirectional=True,
                                      dropout=0)
        else:
            self.input_transform = torch.nn.Sequential(
                torch.nn.Linear(317, 2 * 512), torch.nn.LayerNorm(2 * 512),
                torch.nn.ReLU())

        self.gnn = SrlGcn(
            dim=512 * 2,
            n_layers=configuration["model_parameters"]["gnn_layers"],
            n_relations=(len(srl_utils._DEP_LABELS) - 1))

        self.role_mlp = torch.nn.Sequential(
            torch.nn.Linear(128 + 128, 2 * 2 * 512), torch.nn.ReLU())

        self.loss = CrossEntropyLoss(reduction="none")

        self.word_embedding = Embedding(word_index.size(), 100)
        self.pos_embedding = Embedding(pos_index.size(), 16)
        self.predicate_lemma_embedding = Embedding(frame_index.size(), 100)

        self.pretrained_word_embedding = Embedding(
            self.pretrained_word_embedding_tensor.shape[0],
            self.pretrained_word_embedding_tensor.shape[1])
        self.pretrained_word_embedding.weight = torch.nn.Parameter(
            torch.from_numpy(self.pretrained_word_embedding_tensor))

        self.frame_embedding = Embedding(frame_index.size(), 128)
        self.role_embedding = Embedding(role_index.size(), 128)
Example #6
0
    def __init__(self, configuration):
        AbstractTorchModule.__init__(self)

        self.layers = configuration["model_parameters"]["gnn_layers"]
        self.configuration = configuration
        self.max_nodes = configuration["task"]["max_nodes"]
        self.max_query_size = configuration["task"]["max_query_size"]
        self.max_candidates = configuration["task"]["max_candidates"]

        embedding_input_dim = 300

        self.gcn = QaGNN(dim=512,
                         n_layers=self.layers,
                         n_relations=self.n_edge_types,
                         share_parameters=True)

        self.node_compress_mlp = torch.nn.Sequential(
            XavierLinear(embedding_input_dim, 256), torch.nn.Tanh(),
            torch.nn.Dropout(p=0.2))

        self.node_mlp = torch.nn.Sequential(XavierLinear(512, 1024),
                                            torch.nn.Tanh(),
                                            torch.nn.Dropout(p=0.2),
                                            XavierLinear(1024, 512),
                                            torch.nn.Tanh(),
                                            torch.nn.Dropout(p=0.2))

        # self.lstm = LSTM(3072, 256, 2, batch_first=True, bidirectional=True)

        self.lstm1 = torch.nn.LSTM(embedding_input_dim,
                                   256,
                                   num_layers=1,
                                   batch_first=True,
                                   bidirectional=True,
                                   dropout=0)
        self.lstm2 = torch.nn.LSTM(512,
                                   128,
                                   num_layers=1,
                                   batch_first=True,
                                   bidirectional=True,
                                   dropout=0)
        self.query_dropout = Dropout(p=0.2)

        self.second_mlp = torch.nn.Sequential(XavierLinear(768, 128),
                                              torch.nn.Tanh(),
                                              XavierLinear(128, 1),
                                              torch.nn.Dropout(p=0.2))

        self.loss = CrossEntropyLoss(reduction="none")
    def __init__(self, vertex_embedding_dims, message_dims, h_dims, mean_and_var):
        AbstractTorchModule.__init__(self)

        gates = []
        for v_dim, m_dim, h_dim in zip(vertex_embedding_dims, message_dims, h_dims):
            gate_input_shape = [v_dim, m_dim, v_dim]
            gate = torch.nn.Sequential(
                MultipleInputsLayernormLinear(gate_input_shape, h_dim),
                ReLU(),
                Linear(h_dim, 1),
                Squeezer(),
                Sigmoid()
            )

            gates.append(gate)

        gates = torch.nn.ModuleList(gates)
        self.gates = gates

        self.q_z_loc = torch.FloatTensor(mean_and_var[0])
        self.q_z_scale = torch.FloatTensor(mean_and_var[1])
    def __init__(self, num_edges, num_layers):
        AbstractTorchModule.__init__(self)

        pseudo_gates = torch.ones((num_layers, num_edges))
        self.pseudo_gates = torch.nn.Parameter(pseudo_gates,
                                               requires_grad=True)