python “GRU”对象没有属性“_flat_weights_names”

wfypjpf4  于 2023-03-28  发布在  Python
关注(0)|答案(1)|浏览(662)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
D:\Temp\ipykernel_6580\3729707838.py in <module>
      9                                   num_layers, dropout)
     10 net = d2l.EncoderDecoder(encoder, decoder)
---> 11 d2l.train_seq2seq(net, train_iter, lr, num_epochs, tgt_vocab, device)
     12 
     13 

~\AppData\Roaming\Python\Python37\site-packages\d2l\torch.py in train_seq2seq(net, data_iter, lr, num_epochs, tgt_vocab, device)
   1032                 if "weight" in param:
   1033                     nn.init.xavier_uniform_(m._parameters[param])
-> 1034     net.apply(xavier_init_weights)
   1035     net.to(device)
   1036     optimizer = torch.optim.Adam(net.parameters(), lr=lr)

~\AppData\Roaming\Python\Python37\site-packages\torch\nn\modules\module.py in apply(self, fn)

~\AppData\Roaming\Python\Python37\site-packages\torch\nn\modules\module.py in apply(self, fn)

~\AppData\Roaming\Python\Python37\site-packages\torch\nn\modules\module.py in apply(self, fn)

~\AppData\Roaming\Python\Python37\site-packages\d2l\torch.py in xavier_init_weights(m)
   1029             nn.init.xavier_uniform_(m.weight)
   1030         if type(m) == nn.GRU:
-> 1031             for param in m._flat_weights_names:
   1032                 if "weight" in param:
   1033                     nn.init.xavier_uniform_(m._parameters[param])

~\AppData\Roaming\Python\Python37\site-packages\torch\nn\modules\module.py in __getattr__(self, name)

属性错误:“GRU”对象没有属性“_flat_weights_names”
属性错误:'GRU'对象没有属性'_flat_weights_names'如何修复?

bpzcxfmw

bpzcxfmw1#

代替
type(m) == nn.GRU
尝试
isinstance(m, nn.GRU)

相关问题