first of all this forum helped me so much in the past few days… Thank you very much for all the good posts and answers!
Now I have a problem I couldn’t find in a post…
I’m currently working on a RNN network for classification of images. I tried CNN but the images are signals not “normal” Images. I need to track them over time. (but that problem comes later)
the Images/Signals are 128x64
# init num_classes = 2 num_epochs = 10 batch_size = 4 learning_rate = 0.001 # input size and sequence length are the Image dimension input_size = 128 # tried both ways same error sequence_length = 64 hidden_size = 128 num_layers = 2
the data is loaded over
trainset = torchvision.datasets.ImageFolder(root="/PATH", transform=transforms.ToTensor()) validation = torchvision.datasets.ImageFolder(root="/PATH", transform=transforms.ToTensor()) trainloader = torch.utils.data.DataLoader(dataset = trainset, batch_size = batch_size, shuffle=False) validationloader = torch.utils.data.DataLoader(dataset = validation, batch_size = batch_size, shuffle=False)
class RNN(nn.Module): def __init__(self, input_size, hidden_size, num_layers, num_classes): super(RNN, self).__init__() self.num_layers = num_layers self.hidden_size = hidden_size self.rnn = nn.RNN(input_size, hidden_size, num_layers, batch_first=True) # -> x : (batch_size, seq, input_size) self.fc = nn.Linear(hidden_size, num_classes) def forward(self, x): h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device) print(x.size(0)) # outputs: 12 # Forward propagate RNN out, _ = self.rnn(x, h0) # out: tensor of shape (batch_size, seq_length, hidden_size) print(x.shape) # outputs: torch.Size([12, 64, 128]) out = out[:, -1, :] out = self.fc(out) return out
the Error i’m getting is:
ValueError: Expected input batch_size (12) to match target batch_size (4).
I know the problem is that the size from x is 12 and the batch_size is 4… but if i change batch_size to 12 the Error still remains just with 36… Somehow my batch_size is 3 times to big for the input (if I see this right?)
net = RNN(input_size, hidden_size, num_layers, num_classes).to(device) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(net.parameters(), lr=learning_rate) # train the model for epoch in range(10): # loop over the dataset multiple times print("epoche: ", epoch) running_loss = 0.0 for i, (images, labels) in enumerate(trainloader): # origin shape: [N, 1, 64, 128] # resized: [N, 64, 128] images = images.reshape(-1, sequence_length, input_size).to(device) labels = labels.to(device) # zero the parameter gradients optimizer.zero_grad() # forward + backward + optimize outputs = net(images) loss = criterion(outputs, labels) loss.backward() optimizer.step() # print statistics running_loss += loss.item() if i % 100 == 99: # print every 200 mini-batches print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 2000)) running_loss = 0.0 print('Finished Training')
EDIT: If I try the whole RNN with a MNIST (input_size: 28 / sequence_length: 28)dataset it works just fine (28x28)