You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/04/24 01:10:18 UTC

[GitHub] nickbent opened a new issue #10665: Variable amount of blocks with multiple outputs

nickbent opened a new issue #10665: Variable amount of blocks with multiple outputs
URL: https://github.com/apache/incubator-mxnet/issues/10665
 
 
   I am tryin to create a net that has a variable amount of blocks, but that also gives multiple outputs in the forward pass. Normally with a variable amount of blocks you would run a loop and add the layers in the loop, then for the forward pass you would input the x into the model. Since I want multiple outputs, just inputting x into the model for the forward pass does not work. Below is the code that I tried to implement, but I get : 
    "DiscriminatorCNN.model" is a container with Blocks. Note that Blocks inside the list, tuple or dict will not be registered automatically. Make sure to register them using register_child() or switching to nn.Sequential/nn.HybridSequential instead. 
   This seems to be caused because none of the blocks have unique names, but because I want variable amount of blocks, I can't give them unique names. Is there a way around this ?
   
   `def conv(channels, k_size = 3, stride=2, pad=1, bn=True, drop_out = True , p = 0.2 , ReLU = True, sequential = True):
       
       layers = []
       
       if ReLU :
           layers += [nn.Conv2D(channels=channels, strides = stride  , kernel_size=k_size, padding=pad, activation='relu')]
       else: 
           layers += [nn.Conv2D(channels=channels, strides = stride  , kernel_size=k_size, padding=pad)]
       if bn:
           layers += [nn.BatchNorm()]
       if drop_out :
           layers += [nn.Dropout(p)]
       if sequential :
           out = nn.HybridSequential()
           for layer in layers :
               out.add(layer)
           return out
               
       else:
           return layers
   
   class DiscriminatorCNN(nn.HybridBlock):
       def __init__(self, dim = 64, num_layers = 5):
           super().__init__()
           
           
           with self.name_scope() : 
               self.model = []
               self.model += conv( dim, bn= False)
   
           
               for block in range(num_layers-2) :
                   dim *= 2
                   self.model += conv( dim )
           
               self.model += conv(1, stride=1, pad=0, bn= False, ReLU = False)
       
       def hybrid_forward(self, F, x):
           
           out = []
           
           for block in self.model :
               x = block(x)
               out += x
           
           return F.sigmoid(x), out[:-1]`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services