Mxnet how to concat the input data and output features to the train_iter


#1

i need to create a network using mxnet, which i need the train_iter to includ the input original data and output feature of middle layer . the input original data is MNIST, the middle layer is RELU.
what i mean is that i let the output feature also to be part of the “input” trian data.


#2

Hi @mxnetwqs,

You have complete control over your network architecture, so you can take the array (or symbol) representing the input data and the array (or symbol) representing the feature map of interest and pass these both to another layer for processing. I don’t know how you are going to be using these two inputs, but one method (if they are of the same spatial dimensions) is to use the concatenate operation to stack the array (or symbols) depthwise.

An example in Gluon would look something like:

import mxnet as mx
from mxnet import gluon, nd
from mxnet.gluon import nn

class Net(gluon.Block):
    def __init__(self, **kwargs):
        super(Net, self).__init__(**kwargs)
        with self.name_scope():
            self.conv1 = nn.Conv2D(8, kernel_size=3, padding=1)
            self.conv2 = nn.Conv2D(16, kernel_size=3, padding=1)
            self.fc1 = nn.Dense(10)

    def forward(self, data):
        conv1_out = nd.relu(self.conv1(data))
        conv2_out = nd.relu(self.conv2(conv1_out))
        # work with input data AND feature map from here onwards
        concat = nd.concat(data, conv2_out)
        output = self.fc1(concat)
        return output
    
net = Net()
net.initialize()
data = nd.random.normal(shape=(1,3,32,32))
out = net(data)

I hope that helps, Cheers, Thom