Download lib/python3.9/site-packages/caffe2/python/ideep/leaky_relu_op_test.py from camenduru/slahmr-test: direct link, hf CLI and curl.
- Browser
- Download file 2.85 kB
-
https://huggingface.co/camenduru/slahmr-test/resolve/main/lib/python3.9/site-packages/caffe2/python/ideep/leaky_relu_op_test.py
- Command line
-
hf download hf://camenduru/slahmr-test/lib/python3.9/site-packages/caffe2/python/ideep/leaky_relu_op_test.py
-
curl -L -o leaky_relu_op_test.py https://huggingface.co/camenduru/slahmr-test/resolve/main/lib/python3.9/site-packages/caffe2/python/ideep/leaky_relu_op_test.py
2.85 kB
| import unittest | |
| import hypothesis.strategies as st | |
| from hypothesis import given, settings | |
| import numpy as np | |
| from caffe2.python import core, workspace, model_helper | |
| import caffe2.python.hypothesis_test_util as hu | |
| import caffe2.python.ideep_test_util as mu | |
| class LeakyReluTest(hu.HypothesisTestCase): | |
| def _get_inputs(self, N, C, H, W, order): | |
| input_data = np.random.rand(N, C, H, W).astype(np.float32) - 0.5 | |
| # default step size is 0.05 | |
| input_data[np.logical_and( | |
| input_data >= 0, input_data <= 0.051)] = 0.051 | |
| input_data[np.logical_and( | |
| input_data <= 0, input_data >= -0.051)] = -0.051 | |
| return input_data, | |
| def _get_op(self, device_option, alpha, order, inplace=False): | |
| outputs = ['output' if not inplace else "input"] | |
| op = core.CreateOperator( | |
| 'LeakyRelu', | |
| ['input'], | |
| outputs, | |
| alpha=alpha, | |
| device_option=device_option) | |
| return op | |
| def _feed_inputs(self, input_blobs, device_option): | |
| names = ['input', 'scale', 'bias'] | |
| for name, blob in zip(names, input_blobs): | |
| self.ws.create_blob(name).feed(blob, device_option=device_option) | |
| def test_leaky_relu_gradients(self, gc, dc, N, C, H, W, alpha, seed): | |
| np.random.seed(seed) | |
| op = self._get_op( | |
| device_option=gc, | |
| alpha=alpha, | |
| order='NCHW') | |
| input_blobs = self._get_inputs(N, C, H, W, "NCHW") | |
| self.assertDeviceChecks(dc, op, input_blobs, [0]) | |
| self.assertGradientChecks(gc, op, input_blobs, 0, [0]) | |
| def test_leaky_relu_model_helper_helper(self, N, C, H, W, alpha, seed): | |
| np.random.seed(seed) | |
| order = 'NCHW' | |
| arg_scope = {'order': order} | |
| model = model_helper.ModelHelper(name="test_model", arg_scope=arg_scope) | |
| model.LeakyRelu( | |
| 'input', | |
| 'output', | |
| alpha=alpha) | |
| input_blob = np.random.rand(N, C, H, W).astype(np.float32) | |
| self.ws.create_blob('input').feed(input_blob) | |
| self.ws.create_net(model.param_init_net).run() | |
| self.ws.create_net(model.net).run() | |
| output_blob = self.ws.blobs['output'].fetch() | |
| assert output_blob.shape == (N, C, H, W) | |
| if __name__ == "__main__": | |
| unittest.main() | |