Hi;
I am new to nlp and pytorch.
I understand (I think) how to build ‘forward’ method, but if I want to iterate over each item in a batch inside forward, how do I incorporated that?
Thanks!
Hi;
I am new to nlp and pytorch.
I understand (I think) how to build ‘forward’ method, but if I want to iterate over each item in a batch inside forward, how do I incorporated that?
Thanks!
You could just iterate the input x
:
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = nn.Linear(20, 10)
self.fc2 = nn.Linear(10, 2)
def forward(self, x):
for x_ in x:
print(x_.shape)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
model = MyModel()
x = torch.randn(2, 20)
output = model(x)
What is your use case? Since the batch size is usually varying, you shouldn’t assume to get a specific batch size.
The most common approach is to write code that works with different batch sizes.