I have a model based on HybridSequential that I need also to include a fixed matrix mutiply. It looks like something like this:
class MyModel_MX(gluon.nn.HybridSequential):
def __init__(...):
super(DeformNetModel_MX, self).__init__()
with self.name_scope():
self.inputlayer = nn.Dense(units)
self.batchnorm0 = nn.BatchNorm()
self.dropout0 = nn.Dropout(dropout)
# add some more layers...
self.outputlayer = nn.Dense(outputdim)
# there is a parameter called abc which has the matrix under its 'components_' attribute
self.thematrix = gluon.Constant('abc_components', mx.nd.array(abc.components_, dtype=np.float32))
def hybrid_forward(self, F, input_tensor, abc_components):
x = self.inputlayer(input_tensor)
x = self.batchnorm0(x)
x = self.dropout0(x)
# process the other layers in the same way
x = self.outputlayer(x)
# neither of these work:
# x = F.linalg.gemm2(x, abc_components, transpose_b=True)
x = F.linalg.gemm2(x, self.thematrix, transpose_b=True)
return x
I’ve tried a LOT of variations of how to specify the matrix in the init (everything from don’t specify it at all, to making it a constant as above, to making it a parameter, to initializing the data directly or in the hybrid_forward, and so on). But it fails in the forward either as:
type <class ‘mxnet.gluon.parameter.Parameter’> not supported
Or in a variety of other ways.
Does anyone have a working example of, say, a 1-layer MLP with a matrix multiply by a const matrix at the end? Huge thanks if so!!