Converting a numpy array to torch tensor

I’m trying to train a model on MNIST dataset in an unsupervised way to extract features. As part of the program, I have to convert a numpy array to a torch tensor. Here is the code and error:

            current_offset = batch_idx*train_batch_size
            assigned_indices = indices[current_offset : current_offset + train_batch_size]
            #assigned_indices = np.array(assigned_indices,dtype='int32')

            assigned_targets = targets[assigned_indices]


            if(current_offset > 0):
                print('current offset: %d, next offset: %d' % (current_offset,current_offset+train_batch_size))
                #print('assigned indices: ',assigned_indices.shape)
                print('assigned targets shape: ',assigned_targets.shape)


            #convert into Variable
            assigned_targets = assigned_targets.astype(float)
            assigned_targets_tens = torch.from_numpy(assigned_targets)

The error:

As it can be seen in the output, I’m printing out the shape of assigned_targets array and it doesn’t have zero dimensions. But the error shows up. I’m wondering if this is a bug in the from_numpy() method.

Hi,

You could try to use np.copy(assigned_targets) maybe?
If that does not help, could you provide a small script to reproduce the error please?