代码之家  ›  专栏  ›  技术社区  ›  Mahsa

类型错误:test()缺少1个必需的位置参数

  •  0
  • Mahsa  · 技术社区  · 6 年前

    我想预测一篇文章中每个句子的得分。我写了这个测试方法:

    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
    

    在主要方法的一部分中,我有:

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

    但我收到了这个错误:

    错误跟踪(最近调用的最后一个):文件 “/home/mahsa/anaconda3/envs/pytorch_env/lib/python3.5/unittest/case.py”, 第59行,在testpartexecutor中 yield file“/home/mahsa/anaconda3/envs/pytorch_env/lib/python3.5/unittest/case.py”, 线路601,运行中 testmethod()文件“/home/mahsa/anaconda3/envs/pytorch_env/lib/python3.5/site packages/nose/case.py”, 行198,运行测试中 self.test(*self.arg)异常:test()缺少1个必需的位置参数:“sent” ------------开始捕获日志记录<<------------gensim.models.doc2vec:debug:gensim.models.doc2vec的快速版本是 正在使用summa.preprocessing.cleaner:信息:“pattern”包不是 找到;标记筛选器不适用于英文gensim.utils:info: 从加载keyedVectors对象 /home/mahsa/pycharmprojects/pytorch_env_project/thesis/proj2/glove_已保存 gensim.utils:信息:从加载syn0 /home/mahsa/pycharmprojects/pytorch_env_project/thesis/proj2/glove_saved.syn0.npy 使用mmap=none gensim.utils:info:设置忽略的属性syn0norm 无gensim.utils:info:loaded /home/mahsa/pycharmprojects/pytorch_env_project/thesis/proj2/glove_已保存 -----------------结束捕获日志记录--

    e ============================================================错误:Mahsa_rnn_sent_classification.test ———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— “/home/mahsa/anaconda3/envs/pytorch_env/lib/python3.5/site packages/nose/case.py”, 行198,运行测试中 self.test(*self.arg)类型错误:test()缺少1个必需的位置参数:“sent” ------------开始捕获日志记录<<------------gensim.models.doc2vec:debug:gensim.models.doc2vec的快速版本是 正在使用summa.preprocessing.cleaner:信息:“pattern”包不是 找到;标记筛选器不适用于英文gensim.utils:info: 从加载keyedVectors对象 /home/mahsa/pycharmprojects/pytorch_env_project/thesis/proj2/glove_已保存 gensim.utils:信息:从加载syn0 /home/mahsa/pycharmprojects/pytorch_env_project/thesis/proj2/glove_saved.syn0.npy 使用mmap=none gensim.utils:info:设置忽略的属性syn0norm 无gensim.utils:info:loaded /home/mahsa/pycharmprojects/pytorch_env_project/thesis/proj2/glove_已保存 -----------------结束捕获日志记录--

    ————————————————————————————————————————————————0.004s运行1次测试

    失败(错误=1)

    我认为test()的参数为“sent”。如何更正此错误?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Silmathoron    6 年前

    使用运行测试 nose unittest 不是小事,如果你是新来的,我想你会有一个更容易的时间开始做一些像 pytest