File: nonlinearity.py

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (43 lines) | stat: -rw-r--r-- 1,145 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
## @package nonlinearity
# Module caffe2.python.helpers.nonlinearity





from caffe2.python import core


def prelu(model, blob_in, blob_out, num_channels=1, slope_init=None,
          **kwargs):
    """PRelu"""
    slope_init = (
        slope_init if slope_init else ('ConstantFill', {'value': 0.25}))
    if model.init_params:
        slope = model.param_init_net.__getattr__(slope_init[0])(
            [],
            blob_out + '_slope',
            shape=[num_channels],
            **slope_init[1]
        )
    else:
        slope = core.ScopedBlobReference(
            blob_out + '_slope', model.param_init_net)

    model.AddParameter(slope)

    return model.net.PRelu([blob_in, slope], [blob_out])


def relu(model, blob_in, blob_out, use_cudnn=False, order="NCHW", **kwargs):
    """Relu."""
    if use_cudnn:
        kwargs['engine'] = 'CUDNN'
    return model.net.Relu(blob_in, blob_out, order=order, **kwargs)


def tanh(model, blob_in, blob_out, use_cudnn=False, order="NCHW", **kwargs):
    """Tanh."""
    if use_cudnn:
        kwargs['engine'] = 'CUDNN'
    return model.net.Tanh(blob_in, blob_out, order=order, **kwargs)