Example of using a Gluon HybridBlock in a Symbol Program


Hi, I’ve been looking at Gluon’s HybridBlocks, and I thought I might ask this question as a way to document (make googleable) - the response:

Where can I find an example of using a Gluon HybridBlock in an existing symbolic program? Im thinking of something like:

fc = mx.sym.FullyConnected(x)
fc = MyGluonHybrid(fc)
fc = mx.sym.relu(fc)



This works. But then you will have a model defined as a Symbol and won’t be able to use it with gluon.

You might also want to checkout SymbolBlock


So @piiswrong, if I have a class like

class MyGluonHybrid(HybridBlock):
   def __init__(self, in_units=0, **kwargs):
       super(MyGluonHybrid, self).__init__(**kwargs)
       with self.name_scope():
           self.units = in_units
           self._in_units = in_units
           self.weight = self.params.get(
               'weight', init=mx.init.Xavier(magnitude=2.24),
               shape=(1, in_units))

   def hybrid_forward(self, F, x, weight):
       return F.broadcast_mul(x, weight)

Can I do something like:

l1_reg = mx.sym.sum(mx.sym.abs(MyGluonHybrid().weight).var())

Strange issue when using HybridBlock in a symbolic program