UserWarning: The given NumPy array is not writeable?

I get the following notification when I load my dataset into the DataLoader:

/pytorch/torch/csrc/utils/tensor_numpy.cpp:141: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program.

I get this error when I want to convert my labels from a numpy array to a tensor. I tried changing the flags but it doesn’t work. Also I tried to copy the array as proposed, but I still get the error. I also can’t change the writeable flag at all. I tried the following in code:

        print(y.flags)
        y.setflags(write=1)
        print(y.flags)
        y = y.copy()
        print(y.flags)
        y.setflags(write=1)
        print(y.flags)
        y = torch.as_tensor(y, dtype=torch.long)

I check the flags three times after each operation and it always returns:

  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : False
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

Can someone help?

Have you tried just using from_numpy? I faced this a similar issue and tried a bunch of copies but the simple solution worked the best.