xidulu commented on a change in pull request #18403: URL: https://github.com/apache/incubator-mxnet/pull/18403#discussion_r437556895
########## File path: python/mxnet/gluon/probability/block/stochastic_block.py ########## @@ -0,0 +1,130 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# coding: utf-8 +# pylint: disable=wildcard-import +"""Stochastic block class.""" +__all__ = ['StochasticBlock', 'StochasticSequential'] + +from functools import wraps +from ...block import HybridBlock +from ...nn.basic_layers import HybridSequential +from ...utils import _indent + + +class StochasticBlock(HybridBlock): + """`StochasticBlock` extends `HybridBlock` to support accumulating loss + in the forward phase, which is extremely useful in building Bayesian Neural Network, + where the loss function is composed of a classification loss and a KL loss. + + """ + + def __init__(self, prefix=None, params=None): + super(StochasticBlock, self).__init__(prefix=prefix, params=params) + self._losses = [] + self._losscache = [] + self._count = 0 + + def add_loss(self, loss): + self._count += 1 + self._losscache.append(loss) + + @staticmethod + def collectLoss(func): + """To accumulate loss during the forward phase, one could first decorate + hybrid_forward with `StochasticBlock.collectLos`s`, + and then collect the loss tensor `x` by calling self.add_loss(x). + For example, in the following forward function, + we generate samples from a Gaussian parameterized by `loc` and `scale` and + accumulate the KL-divergence between it and its prior into the block's loss storage.: + @StochasticBlock.collectLoss + def hybrid_forward(self, F, loc, scale): + qz = mgp.Normal(loc, scale) + # prior + pz = mgp.Normal(F.np.zeros_like(loc), F.np.ones_like(scale)) + self.add_loss(mgp.kl_divergence(qz, pz)) + return qz.sample() + """ + @wraps(func) + def inner(self, *args, **kwargs): + # Loss from hybrid_forward + func_out = func(self, *args, **kwargs) + collected_loss = self._losscache + self._losscache = [] + return (func_out, collected_loss) + + return inner + + def __call__(self, *args): + """Calls forward. Only accepts positional arguments.""" + for hook in self._forward_pre_hooks.values(): + hook(self, args) + self._losses = [] + out = self.forward(*args) # out[0]: net output, out[1]: collected loss + self._losses.extend(out[1]) + for hook in self._forward_hooks.values(): + hook(self, args, out) + return out[0] + + @property + def losses(self): + return self._losses + + +class StochasticSequential(StochasticBlock): + """Stack StochasticBlock sequentially. + """ + + def __init__(self, prefix=None, params=None): + super(StochasticSequential, self).__init__( + prefix=prefix, params=params) + self._layers = [] + + def add(self, *blocks): + """Adds block on top of the stack.""" + for block in blocks: + self._layers.append(block) + self.register_child(block) + + @StochasticBlock.collectLoss + def hybrid_forward(self, F, x): + for block in self._layers: + x = block(x) Review comment: @leezu I reimplement the HybridSequential block. The hybrid_forward api is kept because the code is largely borrowed from the implementation of HybridSequential: https://github.com/apache/incubator-mxnet/blob/5b9aedd933d0fd506de93a1680e107d1f8aa8983/python/mxnet/gluon/nn/basic_layers.py#L104 which also overrides the `HybridSequential`. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
