nonlinearity.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. ## @package nonlinearity
  2. # Module caffe2.python.helpers.nonlinearity
  3. from caffe2.python import core
  4. def prelu(model, blob_in, blob_out, num_channels=1, slope_init=None,
  5. **kwargs):
  6. """PRelu"""
  7. slope_init = (
  8. slope_init if slope_init else ('ConstantFill', {'value': 0.25}))
  9. if model.init_params:
  10. slope = model.param_init_net.__getattr__(slope_init[0])(
  11. [],
  12. blob_out + '_slope',
  13. shape=[num_channels],
  14. **slope_init[1]
  15. )
  16. else:
  17. slope = core.ScopedBlobReference(
  18. blob_out + '_slope', model.param_init_net)
  19. model.AddParameter(slope)
  20. return model.net.PRelu([blob_in, slope], [blob_out])
  21. def relu(model, blob_in, blob_out, use_cudnn=False, order="NCHW", **kwargs):
  22. """Relu."""
  23. if use_cudnn:
  24. kwargs['engine'] = 'CUDNN'
  25. return model.net.Relu(blob_in, blob_out, order=order, **kwargs)
  26. def tanh(model, blob_in, blob_out, use_cudnn=False, order="NCHW", **kwargs):
  27. """Tanh."""
  28. if use_cudnn:
  29. kwargs['engine'] = 'CUDNN'
  30. return model.net.Tanh(blob_in, blob_out, order=order, **kwargs)