Use value of symbol for indexing an array

gluon
how-to
python
#1

I have the following code snippet, which works fine when i am using the imperative mode of Gluon:

class MixedActivation(HybridBlock):
    def __init__(self, quantization_methods, *args, **kwargs):
        super(MixedActivation, self).__init__(*args, **kwargs)

        self.quantization_methods = quantization_methods

    def hybrid_forward(self, F, x, which_quantize_f=0):
        # apply one of the two quantization methods, given value of argument which_quantize_f
        x = self.quantization_methods[int(which_quantize_f[0].asnumpy())](F, x)
        return x

However, I cannot hybridize the network, as there is no .asnumpy function for symbols. Is there a way to achieve the same behaviour for symbolic and imperative mode? Basically, I want to use one or another activation function, given the output of the previous layer. I fear this is fundamentally impossible with symbolic/hybrid mode, as the computational graph is then not fixed, but I am not sure.

Thanks for any replies

Cheers

0 Likes

#2

HybridBlock only accepts Symbols and NDArrays and you can only use functions that are implemented for both (HybridBlock and Symbol).

You could define a condition with F.contrib.cond() which may help you to facilitate your use-case. For instance:

F.contrib.cond(pred, then_func, else_func, name)

then_func would pass your data through a layer with activation function1, else_func would pass the data through a layer with activation function2.

You can find more information about control flow operations here: https://mxnet.incubator.apache.org/versions/master/tutorials/control_flow/ControlFlowTutorial.html

0 Likes

#3

Thanks for the answer, looks like the best option for symbol. For my example with the activation this should solve my problem indeed.

0 Likes