0

I am trying to use a custom function with an mxnet neural network model. This custom function is supposed to create a fuzzy representation of the final layer activation vector.

I am confused how to make this work as regular python functions are working in an imperative manner, while mxnet is working in a declarative manner (i.e. Symbols). When I try to use my function with the defined model it raises an exception as the parameter is a Symbol not a real array during model declaration.

Any ideas regarding how to make my custom function works in a declarative manner (i.e. like mxnet.sym.concat for example)?

Here is my custom function definition:

def getFuzzyRep(arr):
    fuzzRep = ""
    x_qual = np.arange(0, 11, 0.1)
    qual_lo = fuzz.trimf(x_qual, [0, 0, 0.5])
    qual_md = fuzz.trimf(x_qual, [0, 0.5, 1.0])
    qual_hi = fuzz.trimf(x_qual, [0.5, 1.0, 1.0])
    FuzzVals=["Low","Medium","High"]
    i =0
    for val in arr:
        if i == 0:
            fuzzRep = FuzzVals[np.argmax([fuzz.interp_membership(x_qual, qual_lo, val),fuzz.interp_membership(x_qual, qual_md, val),fuzz.interp_membership(x_qual, qual_hi, val)])]
        else:
            fuzzRep = fuzzRep +","+FuzzVals[np.argmax([fuzz.interp_membership(x_qual, qual_lo, val),fuzz.interp_membership(x_qual, qual_md, val),fuzz.interp_membership(x_qual, qual_hi, val)])]
        i+=1
    return fuzzRep 
DigitalFox
  • 1,486
  • 1
  • 13
  • 17

1 Answers1

0

You would need to implement your custom function as a custom HybridBlock and use only methods from the backend (F parameter of hybrid_forward method, which you need to override). If you do so, MXNet will take care of imperative/declarative difference for you.

Take a look here (more high-level description) and here (more detailed description).

Sergei
  • 1,617
  • 15
  • 31