Feeding HybridBlock with dict of inputs

Hello, I’m having a problem with managing multiple inputs of nested Gluon’s HybridBlocks. Currently HybridBlock can take as input only Symbols, NDArray and nested lists of them. Lists of inputs are not well manageable when there are multiple nested components. A dictionary passed as input would be better since we have keys instead of raw nd arrays. Do you know some way around this to mange inputs better?

The goal is to have something like this:

class MyBlock(gluon.HybridBlock):

    def __init__(self, child_blocks):
        self.child_blocks = child_blocks

    def hybrid_forward(self, F, input_dict, **kwargs):                                                                                                          
        for child_block in self.child_blocks:                                                                                                              
            cur_outputs = child_blocks(input_dict)  # child fetches input itself                                                                                                                
            outputs = self._merge_output(outputs, cur_outputs)                                                                                                                  
        return outputs

instead of this:

class MyBlock(gluon.HybridBlock):

    def __init__(self, child_blocks):
        self.child_blocks = child_blocks

    def hybrid_forward(self, F, *inputs, **kwargs):                                                                                                          
        for inpt, child_block in zip(inputs, self.child_blocks):                                                                                                              
            cur_outputs = child_blocks(inpt) # parent block must handle how children handles data                                                                                                                 
            outputs = self._merge_output(outputs, cur_outputs)                                                                                                                  
        return outputs

In essence - can MyBlock be unaware how the children handle own inputs?

Hi,

If I understand you correctly you want to be able to pass in a dictionary into the hybrid_forward of a child block so that the child block, knowing it’s key, can access it’s inputs from the dict. AFAIK, you can’t do this. You could try making the *inputs a single ndarray instead and have the child blocks access different slices of that ndarray, which ensures that your block can accept a single ndarray pass it to it’s children and be unaware of how they handle the inputs.