这段代码哪里错了 class my_BiGRU(torch.nn.Module): def __init__(self, input_size, hidden_size, num_layers, output_size, gru_dropout): super(my_BiGRU, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.num_layers = num_layers self.output_size = output_size self.gru_dropout = gru_dropout self.gru = torch.nn.GRU(input_size, hidden_size, num_layers=num_layers, batch_first=True, bidirectional=True) self.fc = torch.nn.Linear(hidden_size * 2, output_size) def _setup_BiGRU_layers(self): self.BiGRU_layers = my_BiGRU(input_size=self.args.capsule_out_dim, output_size=self.args.apsule_out_features, hidden_size=self.args.BiGRU_hiddensize, num_layers=self.args.BiGRU_layers, gru_dropout=self.args.BiGRU_dropout, )
时间: 2024-02-14 07:11:04 浏览: 126
Pytorch 实现focal_loss 多类别和二分类示例
4星 · 用户满意度95%
这段代码存在两个问题:
1.函数 `_setup_BiGRU_layers` 中的代码应该是在类的构造函数 `__init__` 中进行初始化,而不是在另外一个函数中进行初始化。
2.在调用 `my_BiGRU` 类时,参数名称有误,应该将 `apsule_out_features` 改为 `capsule_out_features`。
改正后的代码如下所示:
```
class my_BiGRU(torch.nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size, gru_dropout, capsule_out_features):
super(my_BiGRU, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.output_size = output_size
self.gru_dropout = gru_dropout
self.gru = torch.nn.GRU(input_size, hidden_size, num_layers=num_layers, batch_first=True, bidirectional=True)
self.fc = torch.nn.Linear(hidden_size * 2, output_size)
self.BiGRU_layers = my_BiGRU(input_size=self.input_size,
output_size=capsule_out_features,
hidden_size=self.hidden_size,
num_layers=self.num_layers,
gru_dropout=self.gru_dropout,
)
```
注意:这里假设 `capsule_out_dim` 和 `args` 都已经在代码中被定义好了。
阅读全文