I have set batch_size equals to 64, but when i print out the train_batch and val_batch, the size is not equal to 64.
The train data and val data are in the below format:

First, i define TEXT and LABEL field.
tokenize = lambda x: x.split()
TEXT = data.Field(sequential=True, tokenize=tokenize)
LABEL = data.Field(sequential=False)
And then i keep trying follow tutorials, and wrote things below:
train_data, valid_data = data.TabularDataset.splits(
path='.',
train='train_intent.csv', validation='val.csv',
format='csv',
fields= {'sentences': ('text', TEXT),
'labels': ('label',LABEL)}
)
test_data = data.TabularDataset(
path='test.csv',
format='csv',
fields={'sentences': ('text', TEXT)}
)
TEXT.build_vocab(train_data)
LABEL.build_vocab(train_data)
BATCH_SIZE = 64
train_iter, val_iter = data.BucketIterator.splits(
(train_data, valid_data),
batch_sizes=(BATCH_SIZE, BATCH_SIZE),
sort_key=lambda x: len(x.text),
sort_within_batch=False,
repeat=False,
device=device
)
But when i want to know the iter is fine or not, i just find the below strange things:
train_batch = next(iter(train_iter))
print(train_batch.text.shape)
print(train_batch.label.shape)
[output]
torch.Size([15, 64])
torch.Size([64])
And the train process output errorValueError: Expected input batch_size (15) to match target batch_size (64).:
def train(model, iterator, optimizer, criterion):
epoch_loss = 0
model.train()
for batch in iterator:
optimizer.zero_grad()
predictions = model(batch.text)
loss = criterion(predictions, batch.label)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
return epoch_loss / len(iterator)
Anyone could give me a hint would be highly appreciated. Thanks!
torchtextnor NLP, I see you're working with Chinese characters, so my guess is that this issue stems from UTF encoding having variable character lengths. Takingnbytes of a an UTF string does not guarantee getting any specific number of characters, and you may even end in a middle of a character. Does this sound reasonable as the cause of the issue?