简体   繁体   中英

TypeError: test() missing 1 required positional argument

I want to predict a score for each sentence in a text. I have written this test method:

def test(sent):
    # Predict for a given sentence
    if sent != "":
        input, seq_lengths, target = make_variables([sent], [])
        output = classifier(input, seq_lengths)
        pred = output.data.max(1, keepdim=True)[1]
        score = pred.cpu().numpy()[0][0]
        print("The sentence is:",sent, "The score is:", score)
        return


    print("evaluating trained model ...")
    total_mse=0

    for sents, scores in test_loader:
        input, seq_lengths, target = make_variables(sents, scores)
        output = classifier(input, seq_lengths)
        pred = output.data.max(1, keepdim=True)[1]
        error=mean_squared_error(pred,target.data.view_as(pred.float()))
        total_mse +=error
    print(" **********  Total MSE is   **********",total_mse)
    return

In a part of main method, I have:

# Testing
test("")
# Testing for a given sample _a sentence_
test("For instance, wolves prey on moose, which are too big for coyotes.")

But I received this error:

Error Traceback (most recent call last): File "/home/mahsa/anaconda3/envs/pytorch_env/lib/python3.5/unittest/case.py", line 59, in testPartExecutor yield File "/home/mahsa/anaconda3/envs/pytorch_env/lib/python3.5/unittest/case.py", line 601, in run testMethod() File "/home/mahsa/anaconda3/envs/pytorch_env/lib/python3.5/site-packages/nose/case.py", line 198, in runTest self.test(*self.arg) Exception: test() missing 1 required positional argument: 'sent' -------------------- >> begin captured logging << -------------------- gensim.models.doc2vec: DEBUG: Fast version of gensim.models.doc2vec is being used summa.preprocessing.cleaner: INFO: 'pattern' package not found; tag filters are not available for English gensim.utils: INFO: loading KeyedVectors object from /home/mahsa/PycharmProjects/PyTorch_env_project/Thesis/proj2/glove_saved gensim.utils: INFO: loading syn0 from /home/mahsa/PycharmProjects/PyTorch_env_project/Thesis/proj2/glove_saved.syn0.npy with mmap=None gensim.utils: INFO: setting ignored attribute syn0norm to None gensim.utils: INFO: loaded /home/mahsa/PycharmProjects/PyTorch_env_project/Thesis/proj2/glove_saved --------------------- >> end captured logging << ---------------------

E ====================================================================== ERROR: mahsa_rnn_sent_classification.test ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/mahsa/anaconda3/envs/pytorch_env/lib/python3.5/site-packages/nose/case.py", line 198, in runTest self.test(*self.arg) TypeError: test() missing 1 required positional argument: 'sent' -------------------- >> begin captured logging << -------------------- gensim.models.doc2vec: DEBUG: Fast version of gensim.models.doc2vec is being used summa.preprocessing.cleaner: INFO: 'pattern' package not found; tag filters are not available for English gensim.utils: INFO: loading KeyedVectors object from /home/mahsa/PycharmProjects/PyTorch_env_project/Thesis/proj2/glove_saved gensim.utils: INFO: loading syn0 from /home/mahsa/PycharmProjects/PyTorch_env_project/Thesis/proj2/glove_saved.syn0.npy with mmap=None gensim.utils: INFO: setting ignored attribute syn0norm to None gensim.utils: INFO: loaded /home/mahsa/PycharmProjects/PyTorch_env_project/Thesis/proj2/glove_saved --------------------- >> end captured logging << ---------------------

---------------------------------------------------------------------- Ran 1 test in 0.004s

FAILED (errors=1)

I think that test() has its argument 'sent'. How can I correct this error?

使用nose或单元测试运行测试并unittest ,如果您是新手,我认为您可以更轻松地开始使用pytest类的pytest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM