Модель BERT не работает с TPU на Kaggle - PullRequest
0 голосов
/ 22 марта 2020

Я использую bert-for-tf2 для kaggle и пытаюсь использовать встроенный TPU, но безуспешно.

tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)

import tensorflow.keras
from keras import optimizers
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model

bert_layer = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/1",trainable=True)

max_len = 128

input_word_ids = tensorflow.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
input_mask = tensorflow.keras.Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
segment_ids = tensorflow.keras.Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")

_ , sequence_output = bert_layer([input_word_ids, input_mask, segment_ids]) #[batch_size, 768]
clf_output = sequence_output[:, 0, :]
out = tensorflow.keras.layers.Dense(768, activation='relu')(clf_output)
final = tensorflow.keras.layers.Dense(1, activation='sigmoid')(out)


with strategy.scope():
    model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=final)
    model.compile(Adam(lr=2e-6), loss='binary_crossentropy', metrics=['accuracy'])

model.summary()

Ошибка:

Variable (<tf.Variable 'bert_model/word_embeddings/embeddings:0' shape=(30522, 768) dtype=float32>) was not created in the distribution strategy scope

Что я могу сделать?

Заранее спасибо

...