Converting regular Block code to HybridBlock


#1

I have a network that I coded using regular Blocks. It works, but, the performance is extremly slow!

So, I am in the process of converting the code to use HybridBlocks in the hope that it would result in faster learning. But, I am stuck trying to figure out how to go about writing the equivalent HybridBlock code. Let me reproduce a tiny part.

def hybrid_forward(self, F, input):
    # former regular Block code that needs to be changed for HybridBlock
    numInputs = input.shape[0]
    blah = input.slice_axis(axis=1, begin=self.numInputs-1, end=self.numInputs).reshape((numInputs,))
    scaledBlah = blah * self.scale
    temp = nd.array(scaledBlah.floor() % self.scale)

But, after hybridize() call, the very first statement there itself triggers the error saying "AttributeError: ‘Symbol’ object has no attribute ‘shape’ ".

Can someone please tell me how can code lines such as the above be changed so that they are OK for HybridBlock?

Thanks!


#2

tensor in hybridblock can have any shape, so you cannot access shape.

You can replace this line
blah = input.slice_axis(axis=1, begin=self.numInputs-1, end=self.numInputs).reshape((numInputs,))

to blah = input[:, -1]


#3

I didn’t think that symbols currently support that type of indexing. Won’t that blow up when you call hybridize?


#4

You are right.

So you can use slice with begin=-1. Negative indexing is supported

https://mxnet.incubator.apache.org/api/python/symbol/symbol.html?highlight=slice#mxnet.symbol.slice