-
Notifications
You must be signed in to change notification settings - Fork 193
/
test_compare_activations.py
190 lines (137 loc) · 7.8 KB
/
test_compare_activations.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# coding=utf-8
#
# created by kpe on 23.May.2019 at 17:10
#
from __future__ import absolute_import, division, print_function
import os
import string
import unittest
import tempfile
import tensorflow as tf
import numpy as np
from tensorflow import keras
from bert import bert_tokenization
from .test_common import AbstractBertTest, MiniBertFactory
class CompareBertActivationsTest(AbstractBertTest):
def setUp(self):
tf.compat.v1.reset_default_graph()
keras.backend.clear_session()
tf.compat.v1.disable_eager_execution()
print("Eager Execution:", tf.executing_eagerly())
@staticmethod
def load_stock_model(model_dir, max_seq_len):
from tests.ext.modeling import BertModel, BertConfig, get_assignment_map_from_checkpoint
tf.compat.v1.reset_default_graph() # to scope naming for checkpoint loading (if executed more than once)
bert_config_file = os.path.join(model_dir, "bert_config.json")
bert_ckpt_file = os.path.join(model_dir, "bert_model.ckpt")
pl_input_ids = tf.compat.v1.placeholder(tf.int32, shape=(1, max_seq_len))
pl_mask = tf.compat.v1.placeholder(tf.int32, shape=(1, max_seq_len))
pl_token_type_ids = tf.compat.v1.placeholder(tf.int32, shape=(1, max_seq_len))
bert_config = BertConfig.from_json_file(bert_config_file)
s_model = BertModel(config=bert_config,
is_training=False,
input_ids=pl_input_ids,
input_mask=pl_mask,
token_type_ids=pl_token_type_ids,
use_one_hot_embeddings=False)
tvars = tf.compat.v1.trainable_variables()
(assignment_map, initialized_var_names) = get_assignment_map_from_checkpoint(tvars, bert_ckpt_file)
tf.compat.v1.train.init_from_checkpoint(bert_ckpt_file, assignment_map)
return s_model, pl_input_ids, pl_token_type_ids, pl_mask
@staticmethod
def predict_on_stock_model(model_dir, input_ids, input_mask, token_type_ids):
max_seq_len = input_ids.shape[-1]
(s_model,
pl_input_ids, pl_token_type_ids, pl_mask) = CompareBertActivationsTest.load_stock_model(model_dir, max_seq_len)
with tf.compat.v1.Session() as sess:
sess.run(tf.compat.v1.global_variables_initializer())
s_res = sess.run(
s_model.get_sequence_output(),
feed_dict={pl_input_ids: input_ids,
pl_token_type_ids: token_type_ids,
pl_mask: input_mask,
})
return s_res
@staticmethod
def load_keras_model(model_dir, max_seq_len):
from tensorflow.python import keras
from bert import BertModelLayer
from bert.loader import StockBertConfig, load_stock_weights, params_from_pretrained_ckpt
bert_config_file = os.path.join(model_dir, "bert_config.json")
bert_ckpt_file = os.path.join(model_dir, "bert_model.ckpt")
l_bert = BertModelLayer.from_params(params_from_pretrained_ckpt(model_dir))
l_input_ids = keras.layers.Input(shape=(max_seq_len,), dtype='int32', name="input_ids")
l_token_type_ids = keras.layers.Input(shape=(max_seq_len,), dtype='int32', name="token_type_ids")
output = l_bert([l_input_ids, l_token_type_ids])
model = keras.Model(inputs=[l_input_ids, l_token_type_ids], outputs=output)
model.build(input_shape=[(None, max_seq_len),
(None, max_seq_len)])
load_stock_weights(l_bert, bert_ckpt_file)
return model
@staticmethod
def predict_on_keras_model(model_dir, input_ids, input_mask, token_type_ids):
max_seq_len = input_ids.shape[-1]
model = CompareBertActivationsTest.load_keras_model(model_dir, max_seq_len)
k_res = model.predict([input_ids, token_type_ids])
return k_res
def test_compare(self):
model_dir = tempfile.TemporaryDirectory().name
os.makedirs(model_dir)
save_path = MiniBertFactory.create_mini_bert_weights(model_dir)
tokenizer = bert_tokenization.FullTokenizer(vocab_file=os.path.join(model_dir, "vocab.txt"), do_lower_case=True)
# prepare input
max_seq_len = 16
input_str = "hello, bert!"
input_tokens = tokenizer.tokenize(input_str)
input_tokens = ["[CLS]"] + input_tokens + ["[SEP]"]
input_ids = tokenizer.convert_tokens_to_ids(input_tokens)
input_ids = input_ids + [0]*(max_seq_len - len(input_tokens))
input_mask = [0]*len(input_tokens) + [0]*(max_seq_len - len(input_tokens)) # FIXME: input_mask broken - chane to [1]*
token_type_ids = [0]*len(input_tokens) + [0]*(max_seq_len - len(input_tokens))
input_ids = np.array([input_ids], dtype=np.int32)
input_mask = np.array([input_mask], dtype=np.int32)
token_type_ids = np.array([token_type_ids], dtype=np.int32)
print(" tokens:", input_tokens)
print("input_ids:{}/{}:{}".format(len(input_tokens), max_seq_len, input_ids), input_ids.shape, token_type_ids)
bert_1_seq_out = CompareBertActivationsTest.predict_on_stock_model(model_dir, input_ids, input_mask, token_type_ids)
bert_2_seq_out = CompareBertActivationsTest.predict_on_keras_model(model_dir, input_ids, input_mask, token_type_ids)
np.set_printoptions(precision=9, threshold=20, linewidth=200, sign="+", floatmode="fixed")
print("stock bert res", bert_1_seq_out.shape)
print("keras bert res", bert_2_seq_out.shape)
print("stock bert res:\n {}".format(bert_1_seq_out[0, :2, :10]), bert_1_seq_out.dtype)
print("keras bert_res:\n {}".format(bert_2_seq_out[0, :2, :10]), bert_2_seq_out.dtype)
abs_diff = np.abs(bert_1_seq_out - bert_2_seq_out).flatten()
print("abs diff:", np.max(abs_diff), np.argmax(abs_diff))
self.assertTrue(np.allclose(bert_1_seq_out, bert_2_seq_out, atol=1e-6))
def test_finetune(self):
model_dir = tempfile.TemporaryDirectory().name
os.makedirs(model_dir)
save_path = MiniBertFactory.create_mini_bert_weights(model_dir)
tokenizer = bert_tokenization.FullTokenizer(vocab_file=os.path.join(model_dir, "vocab.txt"), do_lower_case=True)
# prepare input
max_seq_len = 24
input_str_batch = ["hello, bert!", "how are you doing!"]
input_ids_batch = []
token_type_ids_batch = []
for input_str in input_str_batch:
input_tokens = tokenizer.tokenize(input_str)
input_tokens = ["[CLS]"] + input_tokens + ["[SEP]"]
print("input_tokens len:", len(input_tokens))
input_ids = tokenizer.convert_tokens_to_ids(input_tokens)
input_ids = input_ids + [0]*(max_seq_len - len(input_tokens))
token_type_ids = [0]*len(input_tokens) + [0]*(max_seq_len - len(input_tokens))
input_ids_batch.append(input_ids)
token_type_ids_batch.append(token_type_ids)
input_ids = np.array(input_ids_batch, dtype=np.int32)
token_type_ids = np.array(token_type_ids_batch, dtype=np.int32)
print(" tokens:", input_tokens)
print("input_ids:{}/{}:{}".format(len(input_tokens), max_seq_len, input_ids), input_ids.shape, token_type_ids)
model = CompareBertActivationsTest.load_keras_model(model_dir, max_seq_len)
model.compile(optimizer=keras.optimizers.Adam(),
loss=keras.losses.mean_squared_error)
pres = model.predict([input_ids, token_type_ids]) # just for fetching the shape of the output
print("pres:", pres.shape)
model.fit(x=(input_ids, token_type_ids),
y=np.zeros_like(pres),
batch_size=2,
epochs=2)