Hi,
I have been trying to understand how to build a custom Block in Gluon to feed in multiple input data.
In keras for example I found what I was looking for: https://keras.io/getting-started/functional-api-guide/#multi-input-and-multi-output-models, where different layers are concatenated.
How would I be able to have this functionality in Gluon?
What I currently have is a very simple custom block that stores two dense layers and concatenates their activations on the forward pass:
class ConcatLayer(gluon.nn.HybridBlock):
def __init__(self, **kwargs):
gluon.nn.HybridBlock.__init__(self)
with self.name_scope():
self.a = gluon.nn.Dense(3)
self.b = gluon.nn.Dense(5)
def hybrid_forward(self, F, first_input, *args, **kwargs):
return F.concat(self.a(first_input), self.b(first_input), dim=1)
This is working, but is currently feeding the same input to the two dense layers instead of having two, or an abitrary number of inputs. If net is a HybridSequential model, how would I call this with two, or generally more, parameters?
With a model defined as:
net = gluon.nn.HybridSequential()
net.add(ConcatLayer())
net.hybridize()
net.collect_params().initialize(mx.init.Normal(sigma=1.), ctx=self.model_ctx)
just net(a, b)
? I tried that and got an error in _get_graph, block.py:742 (TypeError: hybrid_forward() takes 3 positional arguments but 4 were given). I am using MXNet 1.3.
Any ideas of how to have a multiple input block in Gluon? Ideally, the block should be able to take other blocks as inputs in the constructor and make use of them in the forward pass.
Thanks,
Philipp