TPU上不支持占位符X类型的操作。如果在图形中使用此操作,则执行将失败

2024-04-25 13:33:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我在TPU上运行使用BERT的文本分类任务。 我使用了不同的教程来运行我的实验123,和{a4}。 我与第二个例子的唯一区别是,我的数据集不是Bert处理器中预定义的数据集之一,所以我必须自己加载并预处理它。另外,我想在create_model中进行一些更改,因此我必须将其写下:

 def create_model(is_training, input_ids, input_mask, segment_ids, labels,
             num_labels, bert_hub_module_handle):
 tags = set()
  if is_training:
    tags.add("train")
  bert_module = hub.Module(bert_hub_module_handle, tags=tags, trainable=True)
  bert_inputs = dict(
      input_ids=input_ids,
      input_mask=input_mask,
      segment_ids=segment_ids)
  bert_outputs = bert_module(
      inputs=bert_inputs,
      signature="tokens",
      as_dict=True)


  output_layer = bert_outputs["pooled_output"]

  hidden_size = output_layer.shape[-1].value

  output_weights = tf.get_variable(
      "output_weights", [num_labels, hidden_size],
      initializer=tf.truncated_normal_initializer(stddev=0.02))

  output_bias = tf.get_variable(
      "output_bias", [num_labels], initializer=tf.zeros_initializer())

  with tf.variable_scope("loss"):
    if is_training:
      # I.e., 0.1 dropout
      output_layer = tf.nn.dropout(output_layer, keep_prob=0.9)

    logits = tf.matmul(output_layer, output_weights, transpose_b=True)
    logits = tf.nn.bias_add(logits, output_bias)
    probabilities = tf.nn.softmax(logits, axis=-1)
    log_probs = tf.nn.log_softmax(logits, axis=-1)

    one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32)

    per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1)
    tf_loss = tf.losses.softmax_cross_entropy(onehot_labels=one_hot_labels,
    logits=log_probs,
    weights=1.0)
    loss = tf.reduce_mean(per_example_loss)

    return (tf_loss, loss, per_example_loss, logits, probabilities)

当我用create_model运行代码时,我看到很多错误,虽然培训没有问题,一切都很顺利,但我不确定是否:1。由于以下错误,模型是否使用TPU。由于所有create_model函数存在以下错误,因此模型使用Bert并对其进行微调。有什么想法吗? 以下是错误(所有函数的错误次数为数百万次):

Operation of type Placeholder X is not supported on the TPU. Execution will fail if this op is used in the graph.


Tags: layeridsinputoutputlabelsmodelistf