示例#1
0
    def __init__(self, config, gpu_list, *args, **params):
        super(Attribute, self).__init__()

        self.word_num = 0
        f = open(os.path.join(config.get("model", "bert_path"), "vocab.txt"), "r")
        for line in f:
            self.word_num += 1

        self.embedding = nn.Embedding(self.word_num, 256)
        self.encoder = nn.LSTM(256, 256, batch_first=True, num_layers=2, bidirectional=True)
        self.ys_emb = nn.Embedding(27, 256)
        self.ys_token = []
        for a in range(0, 27):
            self.ys_token.append(a)
        self.ys_token = Variable(torch.LongTensor(self.ys_token)).cuda()
        self.attention = Attention()

        self.ys_fc = nn.Linear(256 * 27 * 2, 54)
        self.ft_fc = nn.Linear(1024, 23)
        self.zm_fc = nn.Linear(1024, 20)

        self.criterion = {
            "ys": MultiLabelSoftmaxLoss(config),
            "ft": cross_entropy_loss,
            "zm": cross_entropy_loss
        }
        self.accuracy_function = {
            "ys": multi_label_accuracy,
            "ft": single_label_top1_accuracy,
            "zm": single_label_top1_accuracy
        }
示例#2
0
    def __init__(self, config, gpu_list, *args, **params):
        super(BaselineYSBert, self).__init__()

        self.output_dim = config.getint("model", "output_dim")
        self.bert = BertModel.from_pretrained(config.get("model", "bert_path"))
        self.fc = nn.Linear(768, self.output_dim * 2)

        self.criterion = MultiLabelSoftmaxLoss(config)
        self.accuracy_function = init_accuracy_function(config, *args, **params)
示例#3
0
文件: YSDPCNN.py 项目: thunlp/QAJudge
    def __init__(self, config, gpu_list, *args, **params):
        super(BaselineYSDPCNN, self).__init__()
        self.model_name = "DPCNN"
        self.emb_dim = 300
        self.mem_dim = 150
        self.output_dim = config.getint("model", "output_dim") * 2
        self.word_num = 0
        f = open(os.path.join(config.get("model", "bert_path"), "vocab.txt"), "r")
        for line in f:
            self.word_num += 1

        self.embedding = nn.Embedding(self.word_num, self.emb_dim)

        # region embedding
        self.region_embedding = nn.Sequential(
            nn.Conv1d(self.emb_dim, self.mem_dim,
                      kernel_size=3, padding=1),
            nn.BatchNorm1d(num_features=self.mem_dim),
            nn.ReLU(),
        )
        self.conv_block = nn.Sequential(
            nn.BatchNorm1d(num_features=self.mem_dim),
            nn.ReLU(),
            nn.Conv1d(self.mem_dim, self.mem_dim,
                      kernel_size=3, padding=1),
            nn.BatchNorm1d(num_features=self.mem_dim),
            nn.ReLU(),
            nn.Conv1d(self.mem_dim, self.mem_dim,
                      kernel_size=3, padding=1),
        )

        self.num_seq = config.getint("data", "max_seq_length")
        resnet_block_list = []
        while (self.num_seq > 2):
            resnet_block_list.append(ResnetBlock(self.mem_dim))
            self.num_seq = self.num_seq // 2
        self.resnet_layer = nn.Sequential(*resnet_block_list)
        self.fc = nn.Sequential(
            nn.Linear(self.mem_dim * self.num_seq, self.output_dim),
            nn.BatchNorm1d(self.output_dim),
            nn.ReLU(inplace=True),
            nn.Linear(self.output_dim, self.output_dim)
        )

        self.criterion = MultiLabelSoftmaxLoss(config)
        self.accuracy_function = init_accuracy_function(config, *args, **params)
示例#4
0
文件: YSGRU.py 项目: thunlp/QAJudge
    def __init__(self, config, gpu_list, *args, **params):
        super(BaselineYSGRU, self).__init__()
        self.emb_dim = 256
        self.hidden_size = 256
        self.output_dim = config.getint("model", "output_dim") * 2
        self.word_num = 0
        f = open(os.path.join(config.get("model", "bert_path"), "vocab.txt"), "r")
        for line in f:
            self.word_num += 1

        self.embedding = nn.Embedding(self.word_num, self.emb_dim)

        self.gru = nn.GRU(self.emb_dim, self.hidden_size, 4, batch_first=True, bidirectional=True)
        self.fc = nn.Linear(2 * self.hidden_size, self.output_dim)

        self.criterion = MultiLabelSoftmaxLoss(config)
        self.accuracy_function = init_accuracy_function(config, *args, **params)
示例#5
0
    def __init__(self, config, gpu_list, *args, **params):
        super(TextCNN, self).__init__()

        self.encoder = CNNEncoder(config, gpu_list, *args, **params)

        self.embedding = nn.Embedding(len(json.load(open(config.get("data", "word2id")))),
                                      config.getint("model", "hidden_size"))
        self.class_num = len(json.load(open(config.get("data", "mapping_file"), "r", encoding="utf8"))["name2id"])

        self.multi = config.getboolean("data", "multi")
        if self.multi:
            self.criterion = MultiLabelSoftmaxLoss(config, self.class_num)
            #self.criterion = nn.BCEWithLogitsLoss()
            self.accuracy_function = multi_label_accuracy
            self.fc = nn.Linear(config.getint("model", "hidden_size"), self.class_num * 2)
        else:
            self.criterion = cross_entropy_loss
            self.accuracy_function = single_label_top1_accuracy
            self.fc = nn.Linear(config.getint("model", "hidden_size"), self.class_num)
示例#6
0
文件: dqn.py 项目: thunlp/QAJudge
    def __init__(self, config, gpu_list, *args, **params):
        super(ZMDqn, self).__init__()

        try:
            self.save_details = config.get("eval",
                                           "print_details").find("yes") != -1
        except:
            self.save_details = False

        self.accuracy_function = init_accuracy_function(
            config, *args, **params)
        self.learning_rate = config.getfloat("train", "learning_rate")
        self.batch_size = config.getint("rl", "batch_size")
        self.epsilone = config.getfloat("rl", "epsilone")
        self.gamma = config.getfloat("rl", "gamma")
        self.target_update = config.getint("rl", "target_update")
        self.memory_capacity = config.getint("rl", "memory_capacity")
        self.bert = BertModel.from_pretrained(config.get("model", "bert_path"))

        self.n_actions = config.getint("rl", "n_actions")
        self.n_question = config.getint("rl", "n_questions")
        self.output_dim = config.getint("model", "output_dim")
        self.n_states = self.n_actions
        self.user = UserSimulator()

        self.agent_initialized = False

        self.memory = ReplayMemory(self.memory_capacity)
        self.policy_net = nn.Linear(self.n_states + 768, self.n_actions)
        self.target_net = nn.Linear(self.n_states + 768, self.n_actions)
        self.i_episode = 0
        self.optimizer = torch.optim.Adam(self.policy_net.parameters(),
                                          lr=self.learning_rate)
        self.loss_function = nn.MSELoss()
        self.criterion_out = MultiLabelSoftmaxLoss(config)

        lgb_path = config.get("ml", "lgb_path")
        self.lgb = pickle.load(
            open(os.path.join(lgb_path, "predict_net.pkl"), "rb"))
示例#7
0
    def __init__(self, config, gpu_list, *args, **params):
        super(TopJudge, self).__init__()

        self.encoder = CNNEncoder()
        self.decoder = LSTMDecoder()
        self.trans_linear = nn.Linear(self.encoder.feature_len, self.decoder.feature_len)

        self.word_num = 0
        f = open(os.path.join(config.get("model", "bert_path"), "vocab.txt"), "r")
        for line in f:
            self.word_num += 1

        self.embedding = nn.Embedding(self.word_num, 256)

        self.criterion = {
            "ys": MultiLabelSoftmaxLoss(config),
            "ft": cross_entropy_loss,
            "zm": cross_entropy_loss
        }
        self.accuracy_function = {
            "ys": multi_label_accuracy,
            "ft": single_label_top1_accuracy,
            "zm": single_label_top1_accuracy
        }