How can I get labels from data generator?
I try to generate data for my conditional VAE and I need labels, but after generating data when I want to get labels I got this error:
def gen_batch(BATCH_SIZE):
    labels = torch.randint(0, 8, (BATCH_SIZE,)).long().to(device)
    theta = (np.pi/4) * labels.float().to(device)
    centers = torch.stack((torch.cos(theta), torch.sin(theta)), dim = -1)
    noise = torch.randn_like(centers) * 0.1

    return centers + noise, labels

def data_gen(BATCH_SIZE):
    #8 gaussians
    while 1:
        yield gen_batch(BATCH_SIZE)
train_loader,train_labels = data_gen(args.batch_size)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-17-3bc469737639&gt; in &lt;module&gt;
----&gt; 1 train_loader,train_labels = data_gen(args.batch_size)

ValueError: too many values to unpack (expected 2)

how can I fix that?

The exception was raised, because the data_gen() is a generator function, and such a function returns only a single object called a generator object, which cannot be unpacked like you tried to do. A generator object is an iterator that generates and returns data on demand and for that iterators support a special method called __next__, that returns only a single element at a time. However, you do not usually call this method directly. You either pass an iterator to the next built-in function, or you use it, for example, in a for loop that will call next() behind the scenes. Also, the important thing to remember is that after you have exhausted an iterator, you cannot use it again, and if you want to do that, you simply need to create a new one. In your case, though, you cannot exhaust your iterator, because you have an infinite while loop in the data_gen function. Here's an example:
def data_gen(BATCH_SIZE):
    #8 gaussians
    while 1:
        yield gen_batch(BATCH_SIZE)

gen_obj = data_gen(args.batch_size)
train_loader, train_labels = next(gen_obj)
# or:
gen_obj = data_gen(args.batch_size)
for train_loader, train_labels in gen_obj:
    # this is only an example and the for loop
    # will never end, because in 
    # your case the generator is infinite
    pass

