Calling function in another function with lots of arguments

username: KanZa
cooked: <p>I want to call a function in another function but arguments have to be defined in another function which are producing new errors. is there some way out for this in PyTorch</p>
<p>I have a function that has multiple arguments, and I want that function to be another function using PyTorch</p>
<p>def function1(a,b,c,d,e,z):<br>
…<br>
…<br>
def function2(f,g):<br>
z=x+y<br>
…<br>
model = function2(a,b,c,d,e,z)</p>
<p>how to define all these variables a, b,c,d,e for function2</p>
<p>def <strong>query_samples</strong>(method, data_unlabeled, subset, labeled_set, cycle, args,  param):<br>
if method == ‘VAAL’:<br>
# Create unlabeled dataloader for the unlabeled subset<br>
unlabeled_loader = DataLoader(data_unlabeled, batch_size=BATCH,<br>
sampler=SubsetSequentialSampler(subset),<br>
pin_memory=True)</p>
<pre><code>        labeled_loader = DataLoader(data_unlabeled, batch_size=BATCH, 
                                    sampler=SubsetSequentialSampler(labeled_set), 
                                    pin_memory=True)
        if args.dataset == 'fashionmnist':
            vae = VAE(28,1,3)
            discriminator = Discriminator(28)
        else:
            vae = VAE()
            discriminator = Discriminator(32)
        models      = {'vae': vae, 'discriminator': discriminator}
        
        
        #OPTUNA for VAAL method 
        #optim_vae = optim.Adam(vae.parameters(), lr=5e-4)
        optim_vae = getattr(optim, param['optimizer'])(vae.parameters(), lr= param['learning_rate'])
        
        #optim_discriminator = optim.Adam(discriminator.parameters(), lr=5e-4)
        optim_discriminator = getattr(optim, param['optimizer'])(discriminator.parameters(), lr= param['learning_rate'])
        
        optimizers = {'vae': optim_vae, 'discriminator': optim_discriminator}

        train_vaal(models, optimizers, labeled_loader, unlabeled_loader, cycle+1)
        
        all_preds, all_indices = [], []

        for images, _, indices in unlabeled_loader:                       
            images = images.cuda()
            with torch.no_grad():
                _, _, mu, _ = vae(images)
                preds = discriminator(mu)

            preds = preds.cpu().data
            all_preds.extend(preds)
            all_indices.extend(indices)

        all_preds = torch.stack(all_preds)
        all_preds = all_preds.view(-1)
        # need to multiply by -1 to be able to use torch.topk 
        all_preds *= -1
        # select the points which the discriminator things are the most likely to be unlabeled
        _, arg = torch.sort(all_preds) 
    return arg
</code></pre>
<p>I want to call this in the objective function defined below</p>
<p>def <strong>objective</strong>(trial, args, method):<br>
params = {<br>
‘optimizer’: trial.suggest_categorical(“optimizer”, [“Adam”, “RMSprop”, “SGD”]),<br>
‘learning_rate’: trial.suggest_loguniform(‘learning_rate’, 1e-5, 1e-1)<br>
}<br>
cycle = CYCLES<br>
subset = SUBSET</p>
<pre><code>        indices = list(range(NUM_TRAIN))
        random.shuffle(indices)

        if args.total:
            labeled_set= indices
        else: 
            labeled_set = indices[:ADDENDUM]
            unlabeled_set = [x for x in indices if x not in labeled_set]
        
        data_unlabeled = load_dataset(args.dataset)

        model = **query_samples**(method, data_unlabeled, subset, labeled_set, cycle, args, params)
        accuracy = test(params, model)

        return accuracy
</code></pre>
<p>I will be very thankful if someone gives me an idea about avoiding defining<br>
all there arguments in the <strong>objective</strong> function for (method, data_unlabeled, subset, labeled_set, cycle, args, params) in  <strong>query_samples()</strong></p>