I am training on GPU, so when I need to compute my loss I transfer the label data over to the GPU with .cuda. However, I thought that it would be faster to preallocate an array of max batchsize and just copy the data over to GPU without reallocating it and if the batch is smaller I just index the GPU labels matrix.
What is the recommended way of doing this? I tried a bit around but could not come up with a good solution.
labels = torch.zeros((maxbatch, 100)).float().cuda()
for e in range(nepochs):
    labels.zero_()  # Remove any old data
    # The following won't work as it won't track the assignment of the values as it's not a Variable yet
    labels[:batchlabels.shape[0]] = batchlabels
    loss(pred_labels, Variable(labels[:batchlabels.shape[0]]))
    # The following won't work unless I make batchlabels Variable(torch.cuda.FloatTensor), beats the point
    sublabels = Variable(labels[:batchlabels.shape[0]])
    sublabels[:batchlabels.shape[0]] = batchlabels 
    loss(pred_labels, sublabels)