You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by wwwwcu via TVM Discuss <no...@discuss.tvm.ai> on 2020/06/02 01:54:39 UTC

[TVM Discuss] [Questions] Unsupported op 'LogisticRegressionOutput' when import NCF model from MXNet


Hi, 

I was trying to import NCF by MXNet relay. Check this: [MXNet NCF](https://github.com/apache/incubator-mxnet/tree/master/example/neural_collaborative_filtering)

There is an unsupported operator: LogisticRegressionOutput

According to the SigmoidBinaryCrossEntropyLoss code: https://github.com/apache/incubator-mxnet/blob/master/python/mxnet/gluon/loss.py#L229

I tried to implement it by myself.

```
def _mx_logistic_regression_output(inputs, attrs):
    label = inputs[1]
    pred = inputs[0]
    # We use the stable formula: 
    # max(pred, 0) - pred * label + log(1 + exp(-abs(pred)))
    one = _expr.const(1, dtype="float32")
    exp_neg_abs_x = _op.exp(_op.negative(_op.abs(pred)))  # exp(-abs(pred))
    soft_relu = _op.add(_op.log(_op.add(one, exp_neg_abs_x)), _op.nn.relu(pred))
    loss = _op.nn.relu(pred) - pred * label + soft_relu
    return loss
```

and test the function by the following code:
```
def test_forward_logistic_regression():
    m = mx.nd.ones((3, 4))
    n = mx.nd.zeros((3, 4))
    vm = mx.sym.Variable('m')
    vn = mx.sym.Variable('n')

    out = mx.sym.LogisticRegressionOutput(data=vm, label=vn)
    exec_ = out.bind(mx.cpu(), {'m': m, 'n': n})
    exec_.forward()
    exec_.outputs[0].asnumpy()

    shape_dict = {"m": m.shape, "n": n.shape}
    mod, params = relay.frontend.from_mxnet(out, shape_dict)
    intrp = relay.create_executor("graph", mod=mod, target='llvm')
    op_res = intrp.evaluate()(m.asnumpy(), n.asnumpy())

    print(op_res)
    print(exec_.outputs[0].asnumpy())
```

TVM result is different from MXNet result. I also tried other formulas. Still not work. I'm very confusing how to implement it. 
Does anyone know how to fix this? Thanks!!!





---
[Visit Topic](https://discuss.tvm.ai/t/unsupported-op-logisticregressionoutput-when-import-ncf-model-from-mxnet/6863/1) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/656eba5617af4702804aa297235e1deebc1f4b18fd988534832999d34110e3be).

[TVM Discuss] [Questions] Unsupported op 'LogisticRegressionOutput' when import NCF model from MXNet

Posted by wwwwcu via TVM Discuss <no...@discuss.tvm.ai>.

Oh, I got it.
Just using _op.sigmoid() will solve this. I misunderstand backward and forward functions.
```
def _mx_logistic_regression_output(inputs, attrs):
    loss = _op.sigmoid(inputs[0])
    return loss
```
Thank you again!~





---
[Visit Topic](https://discuss.tvm.ai/t/unsupported-op-logisticregressionoutput-when-import-ncf-model-from-mxnet/6863/4) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/110e696aa296bc6ef1854acc1b41389b9af11aa8529379ae15041b82b80ff31f).

[TVM Discuss] [Questions] Unsupported op 'LogisticRegressionOutput' when import NCF model from MXNet

Posted by wwwwcu via TVM Discuss <no...@discuss.tvm.ai>.

Thank you very much for your reply.
I also tried the way you suggested. But still got wrong result.
```
def _mx_logistic_regression_output(inputs, attrs):
    label = inputs[1]
    pred = _op.sigmoid(inputs[0])
    # We use the stable formula:  max(pred, 0) - pred * label + log(1 + exp(-abs(pred)))
    one = _expr.const(1, dtype="float32")
    exp_neg_abs_x = _op.exp(_op.negative(_op.abs(pred)))  # exp(-abs(pred))
    soft_relu = _op.add(_op.log(_op.add(one, exp_neg_abs_x)), _op.nn.relu(pred))
    loss = _op.nn.relu(pred) - pred * label + soft_relu
    return loss
```
```
TVM result:
[[1.8551042 1.8551042 1.8551042 1.8551042]
 [1.8551042 1.8551042 1.8551042 1.8551042]
 [1.8551042 1.8551042 1.8551042 1.8551042]]
MXNet result:
[[0.7310586 0.7310586 0.7310586 0.7310586]
 [0.7310586 0.7310586 0.7310586 0.7310586]
 [0.7310586 0.7310586 0.7310586 0.7310586]]
```





---
[Visit Topic](https://discuss.tvm.ai/t/unsupported-op-logisticregressionoutput-when-import-ncf-model-from-mxnet/6863/3) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/78415d10290dc36d87edfb48cd189db325c4fea9f359f676a7d09a34b72b3cf8).

[TVM Discuss] [Questions] Unsupported op 'LogisticRegressionOutput' when import NCF model from MXNet

Posted by Siju via TVM Discuss <no...@discuss.tvm.ai>.

i think in forward pass `LogisticRegressionOutput`  will  do the sigmoid of inputs[0]
you can use `_op.sigmoid(inputs[0])` and check your output.





---
[Visit Topic](https://discuss.tvm.ai/t/unsupported-op-logisticregressionoutput-when-import-ncf-model-from-mxnet/6863/2) to respond.

You are receiving this because you enabled mailing list mode.

To unsubscribe from these emails, [click here](https://discuss.tvm.ai/email/unsubscribe/d6e99a8cb5cfe42b17eaac405a94924f3f2469c91d032e8e0ee59afbfc9a09f1).