【问题标题】:mxnet custom activation function / op in numpymxnet 自定义激活函数 / numpy 中的操作
【发布时间】:2017-04-02 21:55:12
【问题描述】:

我对在 mxnet 中创建自定义激活函数/操作时使用的语法有疑问。我在看这个例子: https://github.com/dmlc/mxnet/blob/master/example/numpy-ops/custom_softmax.py

具体来说,这部分:

class Softmax(mx.operator.CustomOp):
    def forward(self, is_train, req, in_data, out_data, aux):
        x = in_data[0].asnumpy()
        y = np.exp(x - x.max(axis=1).reshape((x.shape[0], 1)))
        y /= y.sum(axis=1).reshape((x.shape[0], 1))
        self.assign(out_data[0], req[0], mx.nd.array(y))

    def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
        l = in_data[1].asnumpy().ravel().astype(np.int)
        y = out_data[0].asnumpy()
        y[np.arange(l.shape[0]), l] -= 1.0
        self.assign(in_grad[0], req[0], mx.nd.array(y))

in_data[0] vs in_data[1] 和 out_data[0] vs out_data[1] 是怎么回事?索引对应什么?

谢谢!

【问题讨论】:

    标签: python mxnet


    【解决方案1】:

    in_data=[输入,标签],out_data=[输出]

    看看softmax输出API:https://github.com/dmlc/mxnet/blob/master/src/operator/softmax_output-inl.h

    CHECK_EQ(in_data.size(), 2U) << "SoftmaxOutput Input: [data, label]";
    CHECK_EQ(out_data.size(), 1U) << "SoftmaxOutput Output: [output]";
    

    【讨论】:

      猜你喜欢
      • 2018-04-15
      • 2018-09-30
      • 2020-12-07
      • 1970-01-01
      • 2020-05-29
      • 2018-08-30
      • 2019-01-16
      • 1970-01-01
      • 2019-06-12
      相关资源
      最近更新 更多