Error in training even though I passed all tests

Hi,

I keep on getting an error even though I passed all of my tests: RuntimeError: Expected object of backend CUDA but got backend CPU for sequence element 1 in sequence argument at position #1 ‘tensors’

It is erroring on this function code

def combine_vectors(x, y):
    '''
    Function for combining two vectors with shapes (n_samples, ?) and (n_samples, ?).
    Parameters:
      x: (n_samples, ?) the first vector. 
        In this assignment, this will be the noise vector of shape (n_samples, z_dim), 
        but you shouldn't need to know the second dimension's size.
      y: (n_samples, ?) the second vector.
        Once again, in this assignment this will be the one-hot class vector 
        with the shape (n_samples, n_classes), but you shouldn't assume this in your code.
    '''
    # Note: Make sure this function outputs a float no matter what inputs it receives
    #### START CODE HERE ####
    combined = torch.cat((x.long(), y.long()), 1)
    #### END CODE HERE ####
    return combined.to(float) 

Hey @Sarthak_Jain1,
Welcome to the community. The error is there because the device config of the tensor has been modified. In the entire notebook, the tensors on GPU (CUDA) are used, but your code has changed the device of the tensors from GPU to CPU, hence the error.

If you take a look at the docs of the long function, which can you find here, you will find that the use of this function may change the device type.

Try out the below code, it worked for me:

combined = torch.cat((x, y), 1).to(torch.float32)

I hope this helps :innocent:

Regards,
Elemento

@Elemento I think, this will not pass a test case wherein both x and y are initially of type “long”. Then, conversion to “float” will cause problem. So, it should be -

combined = torch.cat((x.float(),y.float()),1)

2 Likes