numpy ValueError:在使用Vision Transformer进行训练期间,数组拆分不会导致等分

ca1c2owp  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(80)

代码是这样的

import torch.utils.data as data
from torch.autograd import Variable
import numpy as np

print("Number of train samples: ", len(train_ds))
print("Number of test samples: ", len(test_ds))
print("Detected Classes are: ", train_ds.class_to_idx) 

train_loader = data.DataLoader(train_ds, batch_size=BATCH_SIZE, shuffle=True,  num_workers=4)
test_loader  = data.DataLoader(test_ds, batch_size=BATCH_SIZE, shuffle=True, num_workers=4) 

# Train the model
for epoch in range(EPOCHS):        
  for step, (x, y) in enumerate(train_loader):
    # Change input array into list with each batch being one element
    x = np.split(np.squeeze(np.array(x)), BATCH_SIZE)
    # Remove unecessary dimension
    for index, array in enumerate(x):
      x[index] = np.squeeze(array)
    # Apply feature extractor, stack back into 1 tensor and then convert to tensor
    x = torch.tensor(np.stack(feature_extractor(x)['pixel_values'], axis=0))
    # Send to GPU if available
    x, y  = x.to(device), y.to(device)
    b_x = Variable(x)   # batch x (image)
    b_y = Variable(y)   # batch y (target)
    # Feed through model
    output, loss = model(b_x, None)
    # Calculate loss
    if loss is None: 
      loss = loss_func(output, b_y)   
      optimizer.zero_grad()           
      loss.backward()                 
      optimizer.step()

    if step % 50 == 0:
      # Get the next batch for testing purposes
      test = next(iter(test_loader))
      test_x = test[0]
      # Reshape and get feature matrices as needed
      test_x = np.split(np.squeeze(np.array(test_x)), BATCH_SIZE)
      for index, array in enumerate(test_x):
        test_x[index] = np.squeeze(array)
      test_x = torch.tensor(np.stack(feature_extractor(test_x)['pixel_values'], axis=0))
      # Send to appropirate computing device
      test_x = test_x.to(device)
      test_y = test[1].to(device)
      # Get output (+ respective class) and compare to target
      test_output, loss = model(test_x, test_y)
      test_output = test_output.argmax(1)
      # Calculate Accuracy
      accuracy = (test_output == test_y).sum().item() / BATCH_SIZE
      print('Epoch: ', epoch, '| train loss: %.4f' % loss, '| test accuracy: %.2f' % accuracy)

字符串
我已经尝试获取批量大小,但仍然存在错误:

import math

# Determine batch size
batch_size_candidates = [32, 64, 128, 256]
for candidate in batch_size_candidates:
    if len(train_ds) % candidate == 0:
        BATCH_SIZE = candidate
        break
if BATCH_SIZE is None:
    raise ValueError("Cannot find a batch size that evenly divides the number of training samples.")
print("Batch size: ", BATCH_SIZE)


训练以某种方式开始,但在此过程中,它出错并给出ValueError:数组拆分不会导致训练期间的等分,导致错误的突出显示的代码行如下:第一个月

2jcobegt

2jcobegt1#

我正在尝试类似的代码来训练我自己的代码,我遇到了同样的问题。我可以通过添加
drop_last = True
在训练和测试数据加载器。
例如,我的代码是
train_loader = data.DataLoader(train_ds,batch_size=BATCH_SIZE,shuffle=True,num_workers=4,drop_last=True)
test_loader = data.DataLoader(test_ds,batch_size=BATCH_SIZE,shuffle=True,num_workers=4,drop_last=True)
此外,始终建议选择16、32或64等批量大小,以防止批量大小拆分问题。但是,我相信,在这种情况下,它是不下降的原因,没有能够在一批投入的项目。

相关问题