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
    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)

也可以hidden_size=embedding_size / /2

python3下可运行代码

https://github.com/dreamnotover/oqmrc2018

你可能感兴趣的:(机器学习,python)