Ejemplo n.º 1
0
    def __init__(self, config, gpu_list, *args, **params):
        super(DEC, self).__init__()

        self.sentence_encoder = LSTMEncoder(config, gpu_list, *args, **params)
        self.document_encoder = LSTMEncoder(config, gpu_list, *args, **params)
        self.sentence_attention = Attention(config, gpu_list, *args, **params)
        self.document_attention = Attention(config, gpu_list, *args, **params)
Ejemplo n.º 2
0
    def __init__(self, config, gpu_list, *args, **params):
        super(RESModel, self).__init__()

        self.hidden_size = config.getint("model", "hidden_size")
        self.word_num = 0
        f = open(config.get("data", "word2id"), "r", encoding="utf8")
        for line in f:
            self.word_num += 1

        self.embedding = nn.Embedding(self.word_num, self.hidden_size)
        self.context_encoder = GRUEncoder(config, gpu_list, *args, **params)
        self.question_encoder = GRUEncoder(config, gpu_list, *args, **params)
        self.attention = Attention(config, gpu_list, *args, **params)

        hidden_size = config.getint(
            "model", "output_channel")  # config.num_fc_hidden_size
        self.resnet = ResNet(block=BasicBlock,
                             layers=[0, 0, 0, 0],
                             num_classes=hidden_size)
        self.rank_module = nn.Linear(hidden_size, 1)

        self.criterion = nn.CrossEntropyLoss()
        self.bce = nn.BCEWithLogitsLoss(reduction='mean')

        self.multi_module = nn.Linear(4, 16)
        self.accuracy_function = single_label_top1_accuracy
Ejemplo n.º 3
0
    def __init__(self, config, gpu_list, *args, **params):
        super(ModelL, self).__init__()

        self.hidden_size = config.getint("model", "hidden_size")

        self.context_len = config.getint("data", "max_option_len")
        self.question_len = config.getint("data", "max_question_len")

        # self.embedding = nn.Embedding(self.word_num, self.hidden_size)
        self.context_encoder = XLNetEncoder(config, gpu_list, *args, **params)
        # for param in self.context_encoder.parameters():
        #     param.requires_grad = True

        # self.question_encoder = XLNetEncoder(config, gpu_list, *args, **params)
        # for param in self.question_encoder.parameters():
        #     param.requires_grad = True
        self.seresnet = seresnet50(pretrained=True)

        self.attention = Attention(config, gpu_list, *args, **params)
        self.dropout = nn.Dropout(config.getfloat("model", "dropout"))

        self.bce = nn.CrossEntropyLoss(reduction='sum')
        # self.gelu = nn.GELU()
        # self.softmax = nn.Softmax(dim=1)
        self.rank_module = nn.Linear(1000, 4)
        self.accuracy_function = single_label_top1_accuracy
Ejemplo n.º 4
0
    def __init__(self, config, gpu_list, *args, **params):
        super(ModelXS, self).__init__()

        self.batch_size = config.getint("model", "hidden_size")
        self.hidden_size = config.getint("model", "hidden_size")
        self.word_num = 0
        f = open(config.get("data", "word2id"), "r", encoding="utf8")
        for line in f:
            self.word_num += 1

        self.context_len = config.getint("data", "max_option_len") * 4
        self.question_len = config.getint("data", "max_question_len")

        self.embedding = nn.Embedding(self.word_num, self.hidden_size)
        self.context_encoder = GRUEncoder(config, gpu_list, *args, **params)
        self.question_encoder = GRUEncoder(config, gpu_list, *args, **params)
        self.attention = Attention(config, gpu_list, *args, **params)
        self.dropout = nn.Dropout(config.getfloat("model", "dropout"))

        self.dim_capsule = config.getint("model", "dim_capsule")
        self.num_compressed_capsule = config.getint("model",
                                                    "num_compressed_capsule")
        self.ngram_size = [2, 4, 8]
        self.convs_doc = nn.ModuleList([
            nn.Conv1d(self.hidden_size * 2,
                      config.getint("model", "capsule_size"),
                      K,
                      stride=2) for K in self.ngram_size
        ])
        torch.nn.init.xavier_uniform_(self.convs_doc[0].weight)
        torch.nn.init.xavier_uniform_(self.convs_doc[1].weight)
        torch.nn.init.xavier_uniform_(self.convs_doc[2].weight)

        self.primary_capsules_doc = PrimaryCaps(num_capsules=self.dim_capsule,
                                                in_channels=config.getint(
                                                    "model", "capsule_size"),
                                                out_channels=16,
                                                kernel_size=1,
                                                stride=1)

        self.flatten_capsules = FlattenCaps()

        self.W_doc = nn.Parameter(
            torch.FloatTensor(12224, self.num_compressed_capsule))
        torch.nn.init.xavier_uniform_(self.W_doc)

        self.fc_size = config.getint("model", "fc_size")
        self.fc_capsules_doc_child = FCCaps(
            config,
            output_capsule_num=self.fc_size,
            input_capsule_num=self.num_compressed_capsule,
            in_channels=self.dim_capsule,
            out_channels=self.dim_capsule)

        self.bce = nn.MultiLabelSoftMarginLoss(reduction='sum')
        self.gelu = nn.GELU()
        # self.fc_module_q = nn.Linear(self.question_len, 1)
        self.fc_module = nn.Linear(self.fc_size, 4)
        self.accuracy_function = multi_label_top1_accuracy
Ejemplo n.º 5
0
    def __init__(self, config, gpu_list, *args, **params):
        super(BertCAPSModel, self).__init__()

        self.hidden_size = config.getint("model", "hidden_size")
        self.word_num = 0
        f = open(config.get("data", "word2id"), "r", encoding="utf8")
        for line in f:
            self.word_num += 1

        self.embedding = nn.Embedding(self.word_num, self.hidden_size)
        self.context_encoder = BertEncoder(config, gpu_list, *args, **params)
        self.question_encoder = LSTMEncoder(config, gpu_list, *args, **params)
        self.attention = Attention(config, gpu_list, *args, **params)

        self.num_classes = 4
        # self.conv_channel = config.getint("data", "max_question_len") + config.getint("data", "max_option_len")
        self.question_len = config.getint("data", "max_question_len")
        self.context_len = config.getint("data", "max_option_len") * 4
        self.dim_capsule = config.getint("model", "dim_capsule")
        self.num_compressed_capsule = config.getint("model",
                                                    "num_compressed_capsule")
        self.ngram_size = [2, 4, 8]
        self.convs_doc = nn.ModuleList([
            nn.Conv1d(self.hidden_size, 32, K, stride=2)
            for K in self.ngram_size
        ])
        torch.nn.init.xavier_uniform_(self.convs_doc[0].weight)
        torch.nn.init.xavier_uniform_(self.convs_doc[1].weight)
        torch.nn.init.xavier_uniform_(self.convs_doc[2].weight)

        self.primary_capsules_doc = PrimaryCaps(num_capsules=self.dim_capsule,
                                                in_channels=32,
                                                out_channels=32,
                                                kernel_size=1,
                                                stride=1)

        self.flatten_capsules = FlattenCaps()

        self.W_doc = nn.Parameter(
            torch.FloatTensor(49024, self.num_compressed_capsule))
        torch.nn.init.xavier_uniform_(self.W_doc)

        self.fc_capsules_doc_child = FCCaps(
            config,
            output_capsule_num=self.num_classes,
            input_capsule_num=self.num_compressed_capsule,
            in_channels=self.dim_capsule,
            out_channels=self.dim_capsule)

        # self.rank_module = nn.Linear(hidden_size, 1)

        self.criterion = nn.CrossEntropyLoss()
        self.bce = nn.BCEWithLogitsLoss(reduction='mean')

        self.fc_module = nn.Linear(self.dim_capsule, self.num_classes)
        self.accuracy_function = multi_label_top1_accuracy
Ejemplo n.º 6
0
    def __init__(self, config, gpu_list, *args, **params):
        super(BiDAF, self).__init__()

        self.hidden_size = config.getint("model", "hidden_size")

        self.embedding = nn.Embedding(
            len(json.load(open(config.get("data", "word2id")))),
            config.getint("model", "hidden_size"))
        self.encoder = LSTMEncoder(config, gpu_list, *args, **params)
        self.attention = Attention(config, gpu_list, *args, **params)

        self.fc = nn.Linear(self.hidden_size * 2, 1)

        self.criterion = cross_entropy_loss
        self.accuracy_function = single_label_top1_accuracy
Ejemplo n.º 7
0
Archivo: qa.py Proyecto: yueyedeai/CAIL
    def __init__(self, config, gpu_list, *args, **params):
        super(ModelX, self).__init__()

        self.hidden_size = config.getint("model", "hidden_size")
        # self.word_num = 0
        # f = open(config.get("data", "word2id"), "r", encoding="utf8")
        # for line in f:
        #     self.word_num += 1

        self.context_len = config.getint("data", "max_option_len")
        self.question_len = config.getint("data", "max_question_len")

        # self.embedding = nn.Embedding(self.word_num, self.hidden_size)
        self.context_encoder = BertEncoder(config, gpu_list, *args, **params)
        # for param in self.context_encoder.parameters():
        #     param.requires_grad = True

        self.question_encoder = BertEncoder(config, gpu_list, *args, **params)
        # for param in self.question_encoder.parameters():
        #     param.requires_grad = True

        self.attention = Attention(config, gpu_list, *args, **params)
        self.dropout = nn.Dropout(config.getfloat("model", "dropout"))

        # self.att_weight_c = Linear(self.hidden_size, 1)
        # self.att_weight_q = Linear(self.hidden_size, 1)
        # self.att_weight_cq = Linear(self.hidden_size, 1)
        # self.conv1 = nn.Conv2d(1, 1, kernel_size=13, stride=7,padding=0, bias=False)
        # self.conv2 = nn.Conv2d(1, 1, kernel_size=11, stride=5, padding=0, bias=False)
        # self.conv3 = nn.Conv2d(1, 1, kernel_size=7, stride=3, padding=0, bias=False)
        # self.conv4 = nn.Conv2d(1, 1, kernel_size=5, stride=3, padding=0, bias=False)
        # self.conv5 = nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=0, bias=False)
        # self.maxpool = nn.MaxPool2d(kernel_size=5, stride=3, padding=0)

        # self.resnet = ResNet(block=Bottleneck, groups=1, layers=[1,1,1,1], num_classes=64)
        # self.fpn = FPN(in_channels=[1, 1, 1, self.context_len * 4], out_channels=self.question_len)

        self.bce = nn.CrossEntropyLoss(reduction='mean')
        # self.kl = nn.KLDivLoss(reduction='mean')
        self.gelu = nn.GELU()
        self.softmax = nn.Softmax(dim=1)
        # self.fc_module_inner = nn.Linear(self.question_len * self.context_len *  4, self.hidden_size)
        self.fc_module = nn.Linear(self.context_len * self.question_len * 4,
                                   15)
        self.accuracy_function = single_label_top1_accuracy
Ejemplo n.º 8
0
    def __init__(self, config, gpu_list, *args, **params):
        super(Model, self).__init__()

        self.hidden_size = config.getint("model", "hidden_size")
        self.word_num = 0
        f = open(config.get("data", "word2id"), "r", encoding="utf8")
        for line in f:
            self.word_num += 1

        self.embedding = nn.Embedding(self.word_num, self.hidden_size)
        self.context_encoder = LSTMEncoder(config, gpu_list, *args, **params)
        self.question_encoder = LSTMEncoder(config, gpu_list, *args, **params)
        self.attention = Attention(config, gpu_list, *args, **params)

        self.rank_module = nn.Linear(self.hidden_size * 2, 1)

        self.criterion = nn.CrossEntropyLoss()

        self.multi_module = nn.Linear(4, 16)
        self.accuracy_function = single_label_top1_accuracy
Ejemplo n.º 9
0
    def __init__(self, config, gpu_list, *args, **params):
        super(ModelS, self).__init__()

        self.hidden_size = config.getint("model", "hidden_size")
        self.word_num = 0
        f = open(config.get("data", "word2id"), "r", encoding="utf8")
        for line in f:
            self.word_num += 1

        self.context_len = config.getint("data", "max_option_len") * 4
        self.question_len = config.getint("data", "max_question_len")

        self.embedding = nn.Embedding(self.word_num, self.hidden_size)
        self.context_encoder = LSTMEncoder(config, gpu_list, *args, **params)
        self.question_encoder = LSTMEncoder(config, gpu_list, *args, **params)
        self.attention = Attention(config, gpu_list, *args, **params)
        self.dropout = nn.Dropout(config.getfloat("model", "dropout"))

        self.bce = nn.MultiLabelSoftMarginLoss(reduction='sum')
        self.gelu = nn.GELU()
        # self.fc_module_q = nn.Linear(self.question_len, 1)
        self.fc_module = nn.Linear(self.hidden_size * 2, 4)
        self.accuracy_function = multi_label_top1_accuracy