I don’t know what your trainable is or what its length is, but creating many tensors each time you want a sample of data will inherently be slow. If you need to create a bunch of one-hot tensors, consider using scatter_.