gpt4 book ai didi

python - 在 colab 上使用 TPU 上的估计器进行 BERT 微调 ​​TypeError : unsupported operand type(s) for *=: 'NoneType' and 'int'

转载 作者:行者123 更新时间:2023-12-02 01:56:31 26 4
gpt4 key购买 nike

我在谷歌的 colab 上写了一个 jupyter-notebook 来微调(用于文本分类)我已经仅在阿拉伯语上进行过预训练的 BERT 版本。当训练开始时我无法解决这个错误。

我按照google在github上提供的笔记本进行操作

模型构建代码:

model_fn = model_fn_builder(
bert_config=modeling.BertConfig.from_json_file(CONFIG_FILE),
num_labels=len(label_list),
init_checkpoint=INIT_CHECKPOINT,
learning_rate=LEARNING_RATE,
num_train_steps=num_train_steps,
num_warmup_steps=num_warmup_steps,
use_tpu=True,
use_one_hot_embeddings=True
)


tpu_cluster_resolver = tf.contrib.cluster_resolver.TPUClusterResolver(TPU_ADDRESS)

run_config = tf.contrib.tpu.RunConfig(
cluster=tpu_cluster_resolver,
model_dir=OUTPUT_DIR,
save_checkpoints_steps=SAVE_CHECKPOINTS_STEPS,
tpu_config=tf.contrib.tpu.TPUConfig(
iterations_per_loop=ITERATIONS_PER_LOOP,
num_shards=NUM_TPU_CORES,
per_host_input_for_training=tf.contrib.tpu.InputPipelineConfig.PER_HOST_V2))

estimator = tf.contrib.tpu.TPUEstimator(
use_tpu=USE_TPU,
model_fn=model_fn,
config=run_config,
train_batch_size=TRAIN_BATCH_SIZE,
eval_batch_size=EVAL_BATCH_SIZE,
predict_batch_size=PREDICT_BATCH_SIZE,)

train_input_fn = input_fn_builder(
features=train_features,
seq_length=MAX_SEQ_LENGTH,
is_training=True,
drop_remainder=False)

#tf.reset_default_graph()
print(f'Beginning Training!')
current_time = datetime.now()
estimator.train(input_fn=train_input_fn, max_steps=TRAIN_STEPS)
print("Training took time ", datetime.now() - current_time)

错误代码:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/tpu/tpu_sharding.py in _unshard_shape(self, shape)
214 (shape.as_list(), self._shard_dimension))
215 dims = shape.as_list()
--> 216 dims[self._shard_dimension] *= self._number_of_shards
217 return tensor_shape.as_shape(dims)
218

TypeError: unsupported operand type(s) for *=: 'NoneType' and 'int'

参数和其余代码位于 Colab 笔记本的共享副本中:colab_link

最佳答案

为了社区的利益,在本节中提及答案(即使在评论部分中对此进行了解答)。

在函数 input_fn_builder 中将参数 drop_remainder 设置为 True 已解决了该问题。

相应的代码片段如下所示:

train_input_fn = input_fn_builder(
features=train_features,
seq_length=MAX_SEQ_LENGTH,
is_training=True,
drop_remainder=False)

关于python - 在 colab 上使用 TPU 上的估计器进行 BERT 微调 ​​TypeError : unsupported operand type(s) for *=: 'NoneType' and 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58029896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com