Segmentation fault when using the custom activation function
I am trying to implement a custom activation function (the codes attached below). Before using the custom activation function, everything works well. However, as long as it is used, the server would throw the error:

Segmentation fault

The error always appears at the first epoch.

I am using

Pytorch 1.1.0
Cuda compilation tools, release 9.2, V9.2.148
the codes

def mg(x):

    c = 1.33
    b = 0.4
    p = 6.88
    input_size = x.shape
    num = torch.numel(x) # the element number of the input tensor
    x = x.view(num)

    out = torch.zeros(len(x))

    for i in range(len(x)):
    if x[i] &lt; 0:
            out[i] = 0
        else:
            out[i] = (c * x[i]) / (1 + torch.mul(b * p, torch.pow(x[i], p)))

    out = out.view(input_size[0], input_size[1], input_size[2], input_size[3])
    return out


You are breaking the gradient with newly created out.

You should modify your code to act upon x inputs. Additionally, you shouldn't use any loops (almost always there is a way to do it without them). Given that, this function should be equivalent to yours but works:

def mg(x, c=1.33, b=0.4, p=6.88):
    input_size = x.shape
    x = x.flatten()

    x[x &lt; 0] = 0
    x[x != 0] *= c
    x[x != 0] /= 1 + b * p * x[x != 0] ** p

    return x.reshape(*input_size)


If you are still getting an error it's probably related to some other part of your program.
