1. 程式人生 > >pytorch從python2遷移到python3時遇到torch.FloatTensor constructor received an invalid combination of argumen

pytorch從python2遷移到python3時遇到torch.FloatTensor constructor received an invalid combination of argumen

在執行 AI_Challenger_2018閱讀理解程式基準程式碼時出現以下錯誤:

raceback (most recent call last):   File "train.py", line 39, in <module>     model = MwAN(vocab_size=vocab_size, embedding_size=args.emsize, encoder_size=args.nhid, drop_out=args.dropout)   File "/root/qa_lwh/AI_Challenger_2018/Baselines/opinion_questions_machine_reading_comprehension2018_baseline/model.py", line 17, in __init__     bidirectional=True)   File "/root/anaconda3/envs/python36/lib/python3.6/site-packages/torch/nn/modules/rnn.py", line 482, in __init__     super(GRU, self).__init__('GRU', *args, **kwargs)   File "/root/anaconda3/envs/python36/lib/python3.6/site-packages/torch/nn/modules/rnn.py", line 39, in __init__     w_ih = Parameter(torch.Tensor(gate_size, layer_input_size)) TypeError: torch.FloatTensor constructor received an invalid combination of arguments - got (float, int), but expected one of:  * no arguments  * (int ...)       didn't match because some of the arguments have invalid types: (float, int)  * (torch.FloatTensor viewed_tensor)  * (torch.Size size)  * (torch.FloatStorage data)  * (Sequence data)

經過除錯發現model.py 語句出錯

self.a_encoder = nn.GRU(input_size=embedding_size, hidden_size=embedding_size / 2, batch_first=True,                                 bidirectional=True)  

python3中遇到除操作時都將結果轉換為float型別,強制轉換為int後,錯誤消失,訓練程式可以正確執行。

  self.a_encoder = nn.GRU(input_size=embedding_size, hidden_size=int(embedding_size / 2), batch_first=True,                                 bidirectional=True)