You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by jx...@apache.org on 2017/11/08 22:59:40 UTC
[incubator-mxnet] branch master updated: reimplement cross-entropy
operator in recommender example with NDArray (#8588)
This is an automated email from the ASF dual-hosted git repository.
jxie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git
The following commit(s) were added to refs/heads/master by this push:
new 6da0472 reimplement cross-entropy operator in recommender example with NDArray (#8588)
6da0472 is described below
commit 6da0472357da252502cd5dace9e712800a29e216
Author: solin319 <li...@126.com>
AuthorDate: Thu Nov 9 06:59:37 2017 +0800
reimplement cross-entropy operator in recommender example with NDArray (#8588)
* use ndarray in crossentropy
* add performance test in crossentropy
---
example/recommenders/crossentropy.py | 33 ++++++++++++++++++++++-----------
1 file changed, 22 insertions(+), 11 deletions(-)
diff --git a/example/recommenders/crossentropy.py b/example/recommenders/crossentropy.py
index 51648b0..d8577ed 100644
--- a/example/recommenders/crossentropy.py
+++ b/example/recommenders/crossentropy.py
@@ -20,6 +20,7 @@
"""Cross-entropy loss layer for MXNet.
"""
import os
+import time
import numpy as np
import mxnet as mx
@@ -51,10 +52,10 @@ class CrossEntropyLoss(mx.operator.CustomOp):
# d = number of dimensions
actually_calculate_loss = False
if actually_calculate_loss:
- p = in_data[0].asnumpy() # shape=(b,d)
- y = in_data[1].asnumpy()
- out = y * np.log(p+self.eps) + (1.-y) * np.log((self.eps1) - p)
- self.assign(out_data[0], req[0], mx.nd.array(out))
+ p = in_data[0] # shape=(b,d)
+ y = in_data[1]
+ out = y * mx.nd.log(p+self.eps) + (1.-y) * mx.nd.log((self.eps1) - p)
+ self.assign(out_data[0], req[0], out)
else:
# Just copy the predictions forward
self.assign(out_data[0], req[0], in_data[0])
@@ -70,19 +71,19 @@ class CrossEntropyLoss(mx.operator.CustomOp):
grad = 1/(p-1+y)
which is more numerically stable
"""
- p = in_data[0].asnumpy() # shape=(b,d)
- y = in_data[1].asnumpy()
+ p = in_data[0] # shape=(b,d)
+ y = in_data[1]
grad = -1. / (p - self.eps_1 + y)
- self.assign(in_grad[0], req[0], mx.nd.array(grad))
+ self.assign(in_grad[0], req[0], grad)
def exact_backward(self, req, out_grad, in_data, out_data, in_grad, aux):
"""grad = (y-p)/(p-p^2)
"""
- p = in_data[0].asnumpy() # shape=(b,d)
- y = in_data[1].asnumpy() # seems right
+ p = in_data[0] # shape=(b,d)
+ y = in_data[1] # seems right
grad = (p - y) / ((p+self.eps) * (self.eps1 - p))
- self.assign(in_grad[0], req[0], mx.nd.array(grad))
+ self.assign(in_grad[0], req[0], grad)
@mx.operator.register("CrossEntropyLoss")
@@ -126,5 +127,15 @@ if __name__ == "__main__":
out = e.outputs[0].asnumpy()
if np.abs(out).max() > 1e20:
raise ValueError("output too high!")
- print("Done with test")
+ print("performance test")
+ sz = (6,4)
+ d = mx.nd.array(rand.uniform(0.01,0.99,sz))
+ l = mx.nd.array(rand.randint(0,2,sz))
+ e = net.bind(ctx=mx.cpu(), args={'data':d, 'labs':l})
+ tic = time.time()
+ for i in range(5000):
+ e.forward()
+ e.outputs[0].wait_to_read()
+ print("5000 tests costs time: %f s" % (time.time()-tic))
+ print("Done with test")
--
To stop receiving notification emails like this one, please contact
['"commits@mxnet.apache.org" <co...@mxnet.apache.org>'].