forked from google-research/albert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fine_tuning_utils.py
85 lines (74 loc) · 3.14 KB
/
fine_tuning_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# coding=utf-8
# Copyright 2018 The Google AI Team Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python3
"""Helper library for ALBERT fine-tuning.
This library can be used to construct ALBERT models for fine-tuning, either from
json config files or from TF-Hub modules.
"""
import modeling
import tokenization
import tensorflow.compat.v1 as tf
import tensorflow_hub as hub
def _create_model_from_hub(hub_module, is_training, input_ids, input_mask,
segment_ids):
"""Creates an ALBERT model from TF-Hub."""
tags = set()
if is_training:
tags.add("train")
albert_module = hub.Module(hub_module, tags=tags, trainable=True)
albert_inputs = dict(
input_ids=input_ids,
input_mask=input_mask,
segment_ids=segment_ids)
albert_outputs = albert_module(
inputs=albert_inputs,
signature="tokens",
as_dict=True)
return (albert_outputs["pooled_output"], albert_outputs["sequence_output"])
def _create_model_from_scratch(albert_config, is_training, input_ids,
input_mask, segment_ids, use_one_hot_embeddings,
use_einsum):
"""Creates an ALBERT model from scratch/config."""
model = modeling.AlbertModel(
config=albert_config,
is_training=is_training,
input_ids=input_ids,
input_mask=input_mask,
token_type_ids=segment_ids,
use_one_hot_embeddings=use_one_hot_embeddings,
use_einsum=use_einsum)
return (model.get_pooled_output(), model.get_sequence_output())
def create_albert(albert_config, is_training, input_ids, input_mask,
segment_ids, use_one_hot_embeddings, use_einsum, hub_module):
"""Creates an ALBERT, either from TF-Hub or from scratch."""
if hub_module:
tf.logging.info("creating model from hub_module: %s", hub_module)
return _create_model_from_hub(hub_module, is_training, input_ids,
input_mask, segment_ids)
else:
tf.logging.info("creating model from albert_config")
return _create_model_from_scratch(albert_config, is_training, input_ids,
input_mask, segment_ids,
use_one_hot_embeddings, use_einsum)
def create_vocab(vocab_file, do_lower_case, spm_model_file, hub_module):
"""Creates a vocab, either from vocab file or from a TF-Hub module."""
if hub_module:
return tokenization.FullTokenizer.from_hub_module(
hub_module=hub_module,
spm_model_file=spm_model_file)
else:
return tokenization.FullTokenizer.from_scratch(
vocab_file=vocab_file, do_lower_case=do_lower_case,
spm_model_file=spm_model_file)