Hello, i’m new to Pytorch and nlp.
I’m doing a Reinforcement Learning Project and I’m using GRU as the network. But when I changing the “num_layers” to 2, it comes a ValueError:
“ValueError: too many values to unpack (expected 2)”
Here is my code:
class ActorNetwork(nn.Module):
def __init__(self, input_size, alpha, hidden_size=64, num_layers=2):
super(ActorNetwork, self).__init__()
self.actor_rnn = nn.GRU(
input_size=input_size,
hidden_size=hidden_size, # rnn hidden unit
num_layers=num_layers, # number of rnn layer
bidirectional=True,
batch_first=True,
dropout=0.2
)
self.actor = nn.Linear(hidden_size * 2, 1)
self.optimizer = optim.Adam(self.parameters(), lr=alpha)
self.device = T.device('cuda:0' if T.cuda.is_available() else 'cpu')
self.to(self.device)
def init_weight(self):
for name, param in self.actor_rnn.named_parameters():
if 'bias' in name:
nn.init.constant_(param, 1.0)
elif 'weight' in name:
nn.init.orthogonal_(param, gain=1)
def forward(self, state):
r_out, (h_n, h_c) = self.actor_rnn(state, None)
act_pri = self.actor(r_out)
dist = T.squeeze(act_pri) # from 3 dim to 1
# if there is only 1 dim, it will be squeezed to a scale
if dist.dim() == 0:
dist = T.unsqueeze(dist, 0)
dist = Categorical(logits=dist)
return dist
But when the parameter num_layers is 1, the code can run normally.