Model(*input_tensors) vs model(input_tensors.permute(0,3,1,2))

As the title says, are these two ways of permuting the dimensions to be (batch,channel,height,width)? Or does the first syntax have nothing to do with this?
Thank you

First syntax is python stuff.
In general you can pass variable inputs in a python function. This is enabled by setting a starred variable.

def dummy(x):

This function will accept a single input

def dummy(*x)

This function will accept any amount of inputs.

This start is used in that case to indicate variable amount of inputs, in the rest of cases it is used to unroll an iterable.

def dummy_star(*args):
    print(args)


def dummy(args):
    print(args)

dummy(7)

dummy_star(7,8)
# dummy(7,8) # Throws error as only a single input is allowed

list_of_inputs = [7,8,9]

dummy_star(*list_of_inputs,*list_of_inputs)
print('here you are unrolling list of inputs twice,thats why args is a tuple of ints')
dummy_star(list_of_inputs,list_of_inputs)
print('here you arent unrolling list of inputs twice,thats why args is a tuple of lists')

import torch
list_tensors = torch.rand(2,1)
dummy_star(*list_tensors,*list_tensors)
dummy_star(list_tensors,list_tensors)

Simply if you star a tensor you will iterate over dim 0 and will send each tensor in dim 0 as an input tensor
Btw: https://www.geeksforgeeks.org/args-kwargs-python/

1 Like

Thank you for your answer. I understand. So basically the asterix syntax won’t permute the dimensions but will give the model the input images one by one. What I don’t understand though is why model(*inputs) gives me an error but some coders use it. If you could tell me in what instance it works, that would help. Thanks

Because it depends on how the forward were coded. I still recommend you to read about *args and **kwargs but let’s illustrate it with an example.

if you define your forward function like:

def forward(*inputs):
   # Here inputs is a list.
   x = convolution(inputs[0])
   y = convolution(inputs[1])
   c = x+y 
   return c

output = model(a,b) # here you pass directly a and b

without the starring you would have to pass a list

def forward(inputs):
   # Here inputs is a list.
   x = convolution(inputs[0])
   y = convolution(inputs[1])
   c = x+y 
   return c

output = model([a,b]) # here you pass a list with a and b as forward only expects a single input

But still, google *args and **kwargs cos you are trying to understand a python feature a pytorch rule

1 Like

I googled it. Thank you for your answer.