Converting a List to a Tensor

I am using the Wave-U-Net neural network for audio source separation but there is a bug in the code I have. When performing separation, the outputs are stored in a list and each item in the list is a Tensor. However, the tensors are on a GPU and need to be converted to CPU. My issue is that I cannot convert the list to CPU because numpy lists don’t have a .cpu() attribute. How can I convert all the tensors to CPU?

Here is the code I’m working with. The offending list is called “listofpred0”, and the error occurs when it is passed to mu_law_decode.

def test(epoch): # testing data
model.eval()
start_time = time.time()
with torch.no_grad():
for iloader, xtrain in loadtest:
iloader=iloader.item()
print(iloader)
listofpred0 = []
for ind in range(0, xtrain.shape[-1] - sampleSize, sampleSize):
output = model(xtrain[:, :,ind:ind + sampleSize].to(device))
listofpred0.append(output.reshape(-1))
ans0 = mu_law_decode(np.concatenate(listofpred0))
if not os.path.exists(‘vsCorpus/’): os.makedirs(‘vsCorpus/’)
sf.write(savemusic.format(iloader), ans0, sample_rate)
print(‘test stored done’, np.round(time.time() - start_time))

Found a solution…

Prior to the line ans0 = mu_law_decode(np.concatenate(listofpred0)), I added a for loop that went through all elements in listofpred0 and assigned to CPU one by one. Here is the updated code:

def test(epoch):  # testing data
    model.eval()
    start_time = time.time()
    with torch.no_grad():
        for iloader, xtrain in loadtest:
            iloader=iloader.item()
            print(iloader)
            listofpred0 = []
            for ind in range(0, xtrain.shape[-1] - sampleSize, sampleSize):
                output = model(xtrain[:, :,ind:ind + sampleSize].to(device))
                listofpred0.append(output.reshape(-1))
            # New For Loop
            for i in range(0,len(listofpred0)):
                listofpred0[i] = listofpred0[i].cpu()
            ans0 = mu_law_decode(np.concatenate(listofpred0))
            if not os.path.exists('vsCorpus/'): os.makedirs('vsCorpus/')
            sf.write(savemusic.format(iloader), ans0, sample_rate)
            print('test stored done', np.round(time.time() - start_time))