Esempio n. 1
0
    def __init__(self, keep_tokens, load_path=None):
        keras.backend.clear_session()
        gpu_config = tf.ConfigProto()
        gpu_config.gpu_options.allow_growth = True
        keras.backend.set_session(tf.Session(config=gpu_config))
        self.session = keras.backend.get_session()

        need_load = False
        if load_path and os.path.exists(load_path):
            need_load = True

        bert = build_transformer_model(
            join(BERT_PATH, 'bert_config.json'),
            None if need_load else join(BERT_PATH, 'bert_model.ckpt'),
            keep_tokens=keep_tokens,  # 只保留keep_tokens中的字,精简原字表
            return_keras_model=False,
        )

        layers_out_lambda = Lambda(lambda x: x[:, 0])
        layers_out_dense = Dense(units=2,
                                 activation='softmax',
                                 kernel_initializer=bert.initializer)

        output = layers_out_lambda(bert.model.output)
        output = layers_out_dense(output)

        self.model = keras.models.Model(bert.model.input,
                                        output,
                                        name='Final-Model')
        if need_load:
            logger.info('=' * 15 +
                        'Load from checkpoint: {}'.format(load_path))
            self.model.load_weights(load_path)
        self.data_deal = None
Esempio n. 2
0
    def __init__(self, keep_tokens, num_classes, load_path=None):
        keras.backend.clear_session()
        gpu_config = tf.ConfigProto()
        gpu_config.gpu_options.allow_growth = True
        keras.backend.set_session(tf.Session(config=gpu_config))
        self.session = keras.backend.get_session()

        need_load = False
        if load_path and os.path.exists(load_path):
            need_load = True
        bert = build_transformer_model(
            config_path=join(BERT_PATH, 'bert_config.json'),
            checkpoint_path=None if need_load else join(BERT_PATH, 'bert_model.ckpt'),
            return_keras_model=False,
            keep_tokens=keep_tokens,
        )
        output = Lambda(lambda x: x[:, 0])(bert.model.output)
        output = Dense(
            units=num_classes,
            activation='softmax',
            kernel_initializer=bert.initializer
        )(output)

        self.model =  keras.models.Model(bert.model.input, output)
        # self.model.summary()

        if need_load:
            logger.info('=' * 15 + 'Load from checkpoint: {}'.format(load_path))
            self.model.load_weights(load_path)
        self.data_deal = None
Esempio n. 3
0
    def __init__(self):
        keras.backend.clear_session()
        gpu_config = tf.ConfigProto()
        gpu_config.gpu_options.allow_growth = True
        keras.backend.set_session(tf.Session(config=gpu_config))
        self.session = keras.backend.get_session()

        self.tokenizer = Tokenizer(join(BERT_PATH, 'vocab.txt'),
                                   do_lower_case=True)  # 建立分词器
        self.model = build_transformer_model(
            join(BERT_PATH, 'bert_config.json'),
            join(BERT_PATH, 'bert_model.ckpt'),
        )
Esempio n. 4
0
    def __init__(self, keep_tokens, load_path=None):
        keras.backend.clear_session()
        gpu_config = tf.ConfigProto()
        gpu_config.gpu_options.allow_growth = True
        keras.backend.set_session(tf.Session(config=gpu_config))
        self.session = keras.backend.get_session()

        need_load = False
        if load_path and os.path.exists(load_path):
            need_load = True

        self.model = build_transformer_model(
            join(BERT_PATH, 'bert_config.json'),
            None if need_load else join(BERT_PATH, 'bert_model.ckpt'),
            application='lm',
            keep_tokens=keep_tokens,  # 只保留keep_tokens中的字,精简原字表
        )
        self.model.summary()

        if need_load:
            logger.info('=' * 15 +
                        'Load from checkpoint: {}'.format(load_path))
            self.model.load_weights(load_path)
Esempio n. 5
0
    def __init__(self, tag='d', is_predict=False, load_path=None):
        self.save_path = join(MODEL_PATH, 'rc_' + tag, 'trained.h5')

        keras.backend.clear_session()
        gpu_config = tf.ConfigProto()
        gpu_config.gpu_options.allow_growth = True
        self.session = tf.Session(config=gpu_config)
        keras.backend.set_session(self.session)

        self.data_deal = RCInput()
        self.config_path = join(BERT_PATH, 'bert_config.json')
        self.checkpoint_path = join(BERT_PATH, 'bert_model.ckpt')

        self.tokenizer = self.data_deal.tokenizer
        self.max_p_len = self.data_deal.max_p_len
        self.max_q_len = self.data_deal.max_q_len
        self.max_a_len = self.data_deal.max_a_len
        self.batch_size = self.data_deal.batch_size

        model = build_transformer_model(
            self.config_path,
            None if is_predict else self.checkpoint_path,
            model='bert',
            with_mlm=True,
            keep_tokens=self.data_deal.keep_tokens,  # 只保留keep_tokens中的字,精简原字表
        )
        output = Lambda(lambda x: x[:, 1:self.max_a_len + 1])(model.output)
        self.model = Model(model.input, output)
        # self.model.summary()
        if load_path:
            logger.info('Load from init checkpoint {} .'.format(load_path))
            self.model.load_weights(load_path)
        elif os.path.exists(self.save_path):
            logger.info('Load from init checkpoint {} .'.format(
                self.save_path))
            self.model.load_weights(self.save_path)