I use pytorch
to create GRU model, and I notice that there is a parameter called num_layers
.
My question is:
What is the difference between setting num_layers
to 3 and using 3 nn.GRU
layers?
For example:
the case of assign 3 to
num_layers
import torch import torch.nn as nn input_size = 10 hidden_size = 20 num_layers = 3 # Create the GRU model gru1 = nn.GRU(input_size, hidden_size, num_layers, batch_first=True) # Generate some dummy input data batch_size = 32 seq_length = 50 input_data = torch.randn(batch_size, seq_length, input_size) # Forward propagate through the GRU output, hn = gru1(input_data) print(output.shape)
the case of using 3
nn.GRU
import torch import torch.nn as nn # Create the GRU model gru1 = nn.GRU(input_size, hidden_size, batch_first=True) gru2 = nn.GRU(hidden_size, hidden_size, batch_first=True) gru3 = nn.GRU(hidden_size, hidden_size, batch_first=True) # Generate some dummy input data batch_size = 32 seq_length = 50 input_data = torch.randn(batch_size, seq_length, input_size) # Forward propagate through the GRU output1, hn1 = gru1(input_data) output2, hn2 = gru2(output1, hn1) output, hn = gru3(output2, hn2) print(output.shape)