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
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
def __init__(self, config, gpu_list, *args, **params): super(ModelLC, 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.context_encoder = XLNetEncoder(config, gpu_list, *args, **params) #self.question_encoder = BertEncoder(config, gpu_list, *args, **params) # self.attention = Attention(config, gpu_list, *args, **params) self.dropout = nn.Dropout(config.getfloat("model", "dropout")) self.dim_capsule = 128 #config.dim_capsule self.num_compressed_capsule = 16 #config.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(config.getint("model", "magic_param"), self.num_compressed_capsule)) torch.nn.init.xavier_uniform_(self.W_doc) self.fc_capsules_doc_child = FCCaps( config, output_capsule_num=4, input_capsule_num=self.num_compressed_capsule, in_channels=self.dim_capsule, out_channels=self.dim_capsule) self.bce = nn.CrossEntropyLoss(reduction='mean') # self.gelu = nn.GELU() # self.softmax = nn.Softmax(dim=1) self.rank_module = nn.Linear(self.hidden_size * 2, 4) self.accuracy_function = single_label_top1_accuracy