CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

How to calculate the average activation of a unit within a minibatch?

Open e-thereal opened this issue 10 years ago • 3 comments

I've found two operations that come very close to what I want. The ReduceElements operation can be used to reduce along different axis of a single tensor except for the axis that spans different samples of a minibatch. In comparison, the SumElements operation calculates a sum over all elements of a minbatch, thereby also summing over all samples within the minibatch. I would like to be able to calculate the sum of the same element of a tensor for all samples of a mini-batch. This would allow me to calculate the average activation of a hidden unit and in turn would allow to specify a sparsity term for the hidden units. Is there any way to accomplish this in CNTK? And if so, how would I do it? Thanks!

e-thereal avatar Nov 02 '16 10:11 e-thereal

Have you looked into how layerNormalization works?

n17s avatar Nov 03 '16 05:11 n17s

@n17s Thank you for your reply. Yes I have looked at layerNormalization. The LayerNormalization() function calculates the mean over all elements of a sample but I need it the other way round (the mean over all samples of a minibatch for each element). The closest thing to what I want is BatchNormalization. However, BatchNormalization only calculates the mean over samples internally and does not expose the calculated mean in order to use it independendly of the normalized output. Since BatchNormalization is implemented as a dedicated ComputationNodeNonLooping class and not using BrainScript (as is the case for LayerNormalization()), I don't see a way to just have the mean calculation without creating a new node type in CNTK based on the implementation on BatchNormalization. However, it seems that the ReduceElementsNode would be a much better starting point, because it is already very close to what I want to achieve. Maybe it just needs to be implemented as a ComputationNodeNonLooping, similar to the SumElementsNode, but I just don't quite understand the implementation of both nodes and I'm afraid I'm not able to make the necessary changes myself.

e-thereal avatar Nov 03 '16 08:11 e-thereal

Sorry for the extremely late response. It is impossible to get to the mean activation from the batchNormalizationNode. It is a private value. One option would be to use the V2 API where reductions can be used with the batch axis. In Python:

avg = C.reduce_mean(x, axis=C.Axis.default_batch_axis())

and in C++

avg = ReduceMean(x, Axis::DefaultBatchAxis());

Note that we do not plan to add this functionality in V1/Brainscript.

Moreover, I'm going to close this thread. Feel free to reopen if you have further questions.

n17s avatar Aug 23 '17 00:08 n17s