Erro no notebook do curso, na parte do modelo Q&A

Home Fóruns Fórum Processamento de Linguagem Natural com BERT e Python Erro no notebook do curso, na parte do modelo Q&A

Visualizando 4 posts - 1 até 4 (de 4 do total)
  • Autor
    Posts
  • #42261

    Quando executo o código da célula de treinamento:

    for epoch in range(NB_EPOCHS):
    print(“Start of epoch {}”.format(epoch + 1))
    start = time.time()

    train_loss.reset_states()

    for (batch, (inputs, targets)) in enumerate(train_dataset_light):
    with tf.GradientTape() as tape:
    model_outputs = bert_squad(inputs)
    loss = squad_loss_fn(targets, model_outputs)

    gradients = tape.gradient(loss, bert_squad.trainable_variables)
    optimizer.apply_gradients(zip(gradients, bert_squad.trainable_variables))

    train_loss(loss)

    if batch % 50 == 0:
    print(“Epoch {} Batch {} Loss {:.4f}”.format(epoch+1, batch, train_loss.result()))

    if batch % 500 == 0:
    ckpt_save_path = ckpt_manager.save()
    print(“Saving checkpoint for epoch {} at {}”.format(epoch+1, ckpt_save_path))

    print(“Time taken for 1 epoch: {} secs\n”.format(time.time() – start))

    Obtenho esse erro:

    ————————————————————————— UnimplementedError Traceback (most recent call last) c:\Users\Null\Downloads\Squad\BERT_Perguntas_e_Respostas.ipynb Cell 28 line 1 10 loss = squad_loss_fn(targets, model_outputs) 12 gradients = tape.gradient(loss, bert_squad.trainable_variables) —> 13 optimizer.apply_gradients(zip(gradients, bert_squad.trainable_variables)) 15 train_loss(loss) 17 if batch % 50 == 0: File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\official\nlp\optimization.py:152, in AdamWeightDecay.apply_gradients(self, grads_and_vars, name, experimental_aggregate_gradients) 145 if experimental_aggregate_gradients: 146 # when experimental_aggregate_gradients = False, apply_gradients() no 147 # longer implicitly allreduce gradients, users manually allreduce gradient 148 # and passed the allreduced grads_and_vars. For now, the 149 # clip_by_global_norm will be moved to before the explicit allreduce to 150 # keep the math the same as TF 1 and pre TF 2.2 implementation. 151 (grads, _) = tf.clip_by_global_norm(grads, clip_norm=1.0) –> 152 return super(AdamWeightDecay, self).apply_gradients( 153 zip(grads, tvars), 154 name=name, 155 experimental_aggregate_gradients=experimental_aggregate_gradients) File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\optimizers\optimizer.py:1230, in Optimizer.apply_gradients(self, grads_and_vars, name, skip_gradients_aggregation, **kwargs) 1228 if not skip_gradients_aggregation and experimental_aggregate_gradients: 1229 grads_and_vars = self.aggregate_gradients(grads_and_vars) -> 1230 return super().apply_gradients(grads_and_vars, name=name) File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\optimizers\optimizer.py:650, in _BaseOptimizer.apply_gradients(self, grads_and_vars, name) 648 grads = self._clip_gradients(grads) 649 grads = self._deduplicate_sparse_grad(grads) –> 650 self._apply_weight_decay(trainable_variables) 651 grads_and_vars = list(zip(grads, trainable_variables)) 652 iteration = self._internal_apply_gradients(grads_and_vars) File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\optimizers\optimizer.py:1249, in Optimizer._apply_weight_decay(self, variables) 1244 for variable in variables: 1245 distribution.extended.update( 1246 variable, weight_decay_fn, group=False 1247 ) -> 1249 tf.__internal__.distribute.interim.maybe_merge_call( 1250 distributed_apply_weight_decay, 1251 self._distribution_strategy, 1252 variables, 1253 ) File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\python\distribute\merge_call_interim.py:51, in maybe_merge_call(fn, strategy, *args, **kwargs) 31 “””Maybe invoke fn via merge_call which may or may not be fulfilled. 32 33 The caller of this utility function requests to invoke fn via merge_call (…) 48 The return value of the fn call. 49 “”” 50 if strategy_supports_no_merge_call(): —> 51 return fn(strategy, *args, **kwargs) 52 else: 53 return distribute_lib.get_replica_context().merge_call( 54 fn, args=args, kwargs=kwargs) File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\optimizers\optimizer.py:1245, in Optimizer._apply_weight_decay.<locals>.distributed_apply_weight_decay(distribution, variables, **kwargs) 1242 variable.assign_sub(variable * wd * lr) 1244 for variable in variables: -> 1245 distribution.extended.update( 1246 variable, weight_decay_fn, group=False 1247 ) File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2992, in StrategyExtendedV2.update(self, var, fn, args, kwargs, group) 2989 fn = autograph.tf_convert( 2990 fn, autograph_ctx.control_status_ctx(), convert_by_default=False) 2991 with self._container_strategy().scope(): -> 2992 return self._update(var, fn, args, kwargs, group) 2993 else: 2994 return self._replica_ctx_update( 2995 var, fn, args=args, kwargs=kwargs, group=group) File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\python\distribute\distribute_lib.py:4062, in _DefaultDistributionExtended._update(self, var, fn, args, kwargs, group) 4059 def _update(self, var, fn, args, kwargs, group): 4060 # The implementations of _update() and _update_non_slot() are identical 4061 # except _update() passes var as the first argument to fn(). -> 4062 return self._update_non_slot(var, fn, (var,) + tuple(args), kwargs, group) File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\python\distribute\distribute_lib.py:4068, in _DefaultDistributionExtended._update_non_slot(self, colocate_with, fn, args, kwargs, should_group) 4064 def _update_non_slot(self, colocate_with, fn, args, kwargs, should_group): 4065 # TODO(josh11b): Figure out what we should be passing to UpdateContext() 4066 # once that value is used for something. 4067 with UpdateContext(colocate_with): -> 4068 result = fn(*args, **kwargs) 4069 if should_group: 4070 return result File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\python\autograph\impl\api.py:596, in call_with_unspecified_conversion_status.<locals>.wrapper(*args, **kwargs) 594 def wrapper(*args, **kwargs): 595 with ag_ctx.ControlStatusCtx(status=ag_ctx.Status.UNSPECIFIED): –> 596 return func(*args, **kwargs) File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\optimizers\optimizer.py:1241, in Optimizer._apply_weight_decay.<locals>.distributed_apply_weight_decay.<locals>.weight_decay_fn(variable) 1239 if self._use_weight_decay(variable): 1240 lr = tf.cast(self.learning_rate, variable.dtype) -> 1241 wd = tf.cast(self.weight_decay, variable.dtype) 1242 variable.assign_sub(variable * wd * lr) File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\python\util\traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs) 151 except Exception as e: 152 filtered_tb = _process_traceback_frames(e.__traceback__) –> 153 raise e.with_traceback(filtered_tb) from None 154 finally: 155 del filtered_tb File c:\Users\Null\AppData\Local\Programs\Python\Python311\Lib\site-packages\tensorflow\python\framework\ops.py:6656, in raise_from_not_ok_status(e, name) 6654 def raise_from_not_ok_status(e, name): 6655 e.message += (” name: ” + str(name if name is not None else “”)) -> 6656 raise core._status_to_exception(e) from None UnimplementedError: {{function_node __wrapped__Cast_device_/job:localhost/replica:0/task:0/device:CPU:0}} Cast string to float is not supported [Op:Cast] name:

    Ainda não consegui solucionar…

    #42262

    Indica o erro nessa linha:

    optimizer.apply_gradients(zip(gradients, bert_squad.trainable_variables))

    parece que algo que deveria ser inteiro para ser transformado em float, está em formato string, mas não consigo achar o que está em string…

     

    #42263

    Pelo que vejo é um erro na hora do cast

    #42376
    Denny Ceccon
    Moderador

      Olá Cesar,

      Isso geralmente é bug na versão que você tem instalada da biblioteca, nesse caso TensorFlow. Tenta fazer o downgrade. A versão 2.8 deve funcionar.

    Visualizando 4 posts - 1 até 4 (de 4 do total)
    • Você deve fazer login para responder a este tópico.