0

我从 Huggingface 训练了一个 DistilBERT 模型,用于使用 3 个标签(Claim、Premise、Non-Arg)进行分类,并将模型保存为 .h5 文件。

当我尝试在服务器上部署保存的模型、标记文本并使用模型创建预测时,我收到一个错误代码。

我正在使用以下代码来预测结果:

    for sentence in sentence_list:
        predict_input = tokenizer.encode(sentence,
                                  truncation=True,
                                  padding=True,
                                  return_tensors="tf"
                                  )
        tf_output = model.predict(predict_input)[0][0]
        print("tf_output", tf_output)

        print("tf_output_new", tf_output)
        tf_prediction = tf.exp(tf_output) / tf.reduce_sum(tf.exp(tf_output), axis=0).numpy()
        tf_prediction = tf_prediction.numpy()
        print("tf_prediction", tf_prediction)
        index = int(tf.math.argmax(tf_prediction).numpy())

并得到以下错误代码:

Traceback (most recent call last): 
File "C:/Users/phili/OneDrive/Desktop/LearntoArgue/server (1).py", 
line 121, in <module> output = predict_sentences_from_list(list_, text, tokenizer, loaded_model, ['Claim', 'Premise', 'Non-Arg']) 
File "C:/Users/phili/OneDrive/Desktop/LearntoArgue/server (1).py", 
line 80, in predict_sentences_from_list index = int(tf.math.argmax(tf_prediction).numpy()) 
TypeError: only size-1 arrays can be converted to Python scalars

具有某种多数组输出,我无法处理。(见下面的例子)

tf_output [[ 0.21984343 -0.5467574   0.0040243  ... -0.19792344  0.75051785
   0.42536935]
 [ 0.40001354 -0.33571464  0.2520863  ... -0.169448    0.74237293
   0.10507141]
 [ 0.5038232  -0.5277173   0.40050912 ... -0.32889333  0.52978003
   0.29682326]
 ...
 [ 0.03361291 -0.15118203  0.5666938  ... -0.5183538   0.46048933
   0.26265204]
 [ 0.59308946 -0.13700745 -0.04438984 ... -0.04448561 -0.32723922
  -0.01682709]
 [ 0.58926195 -0.02571652  0.30869782 ...  0.0776237   0.13490912
   0.22816204]]

有没有人有关于如何处理它的提示/经验?或者另一种方式,这可能会有所帮助?

谢谢!

4

0 回答 0