1

我已经成功地使用我的训练数据训练模型,但现在在预测我的测试数据时遇到了一些麻烦。所以我通过测试数据加载

id1 = ttd.LabelField(use_vocab =False)
test_dataset= ttd.TabularDataset(
    path= 'test_1.csv',
    format='csv',
    skip_header=True,
    fields=[('id', id1),('text', TEXT)]
)
test_iter= ttd.BucketIterator(
    test_dataset_kaggle,
    batch_size=64,
    sort_key=lambda x: len(x.text),
    device=device)

并使用以下函数进行预测

def get_predictions_test(test_iter, model):
  model.eval()
  with torch.no_grad():
    predictions= np.array([])
    id2= np.array([])

    for batch in test_iter:
      output=model(batch.data)
      ##print(np.shape(output))
      ##print(output)
      y_pred_tag = np.squeeze(torch.round((output)))
      #_,indices = torch.max(output,dim=1)
      ##print(np.shape(predictions))
      ##print(np.shape(y_pred_tag))
      predictions=np.concatenate((predictions,y_pred_tag.cpu().numpy()))
      ##print(predictions)
      ##y_test = np.concatenate((y_test,batch.label.cpu().numpy()))
      id2 = np.concatenate((id2,batch.id.cpu().numpy()))
      
  return id2, predictions

但是,当我尝试运行

id4, predictions = get_predictions_test(test_iter,rnn_model)

我收到错误“字段”对象没有属性“词汇”。我已经为 TEXT 建立了一个词汇,所以这不应该是问题。我应该为ID建立一个词汇吗?还是我做错了什么

4

0 回答 0