Symbol API binding - single/multiple function call


#1

In order to use rand_zipfian in NDArray API

>>> true_cls = mx.nd.array([3])
>>> samples, exp_count_true, exp_count_sample = mx.nd.contrib.rand_zipfian(true_cls, 4, 5)

However, samples, expected count of true classes and expected count for sampled candidates are all obtained for a particular rand_zipfian function call. Values can be obtained as follows

>>> samples
[1 3 3 3]
<NDArray 4 @cpu(0)>
>>> exp_count_true
[ 0.12453879]
<NDArray 1 @cpu(0)>
>>> exp_count_sample
[ 0.22629439  0.12453879  0.12453879  0.12453879]
<NDArray 4 @cpu(0)>

However, in case of Symbol API, the issue is

>>> true_cls = mx.sym.var('true_cls')
>>> samples, exp_count_true, exp_count_sample = mx.sym.contrib.rand_zipfian(true_cls, 4, 5)

But in order to obtain values of each, I have to use bind and forward.

e = samples.bind(mx.cpu(),{'true_cls':mx.nd.array([3])})
>>> y=e.forward()
>>> y
[
[1 3 3 3]
<NDArray 4 @cpu(0)>]

But the issue is, when I try to get one value twice, it doesn’t give me same value.

>>> e = exp_count_sample.bind(mx.cpu(),{'true_cls':mx.nd.array([3])})
>>> y = e.forward()
>>> y
[
[0.90517754 0.49815515 0.90517754 0.64223369]
<NDArray 4 @cpu(0)>]
>>> e = exp_count_sample.bind(mx.cpu(),{'true_cls':mx.nd.array([3])})
>>> y = e.forward()
>>> y
[
[0.64223369 0.90517754 0.90517754 0.64223369]
<NDArray 4 @cpu(0)>]

It led me to realize that everytime I use bind and forward it is calling the function. How to prevent that from happening?