You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2017/11/08 22:59:41 UTC

[GitHub] piiswrong closed pull request #8588: reimplement cross-entropy operator in recommender example with NDArray

piiswrong closed pull request #8588: reimplement cross-entropy operator in recommender example with NDArray
URL: https://github.com/apache/incubator-mxnet/pull/8588
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/example/recommenders/crossentropy.py b/example/recommenders/crossentropy.py
index 51648b0eb1..d8577ed898 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 @@ def forward(self, is_train, req, in_data, out_data, aux):
         #  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 @@ def approx_backward(self, req, out_grad, in_data, out_data, in_grad, aux):
         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 @@ def infer_shape(self, in_shape):
         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")


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services