forked from dannysdeng/selu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selu.py
56 lines (47 loc) · 1.76 KB
/
selu.py
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
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 11 14:46:31 2017
@author: danny
"""
import torch
import numpy as np
import torch.nn as nn
from torch.autograd import Variable
from torch.nn import functional as F
class selu(nn.Module):
def __init__(self):
super(selu, self).__init__()
self.alpha = 1.6732632423543772848170429916717
self.scale = 1.0507009873554804934193349852946
def forward(self, x):
temp1 = self.scale * F.relu(x)
temp2 = self.scale * self.alpha * (F.elu(-1*F.relu(-1*x)))
return temp1 + temp2
class alpha_drop(nn.Module):
def __init__(self, p = 0.05, alpha=-1.7580993408473766, fixedPointMean=0, fixedPointVar=1):
super(alpha_drop, self).__init__()
keep_prob = 1 - p
self.a = np.sqrt(fixedPointVar / (keep_prob *((1-keep_prob) * pow(alpha-fixedPointMean,2) + fixedPointVar)))
self.b = fixedPointMean - self.a * (keep_prob * fixedPointMean + (1 - keep_prob) * alpha)
self.alpha = alpha
self.keep_prob = 1 - p
self.drop_prob = p
def forward(self, x):
if self.keep_prob == 1 or not self.training:
# print("testing mode, direct return")
return x
else:
random_tensor = self.keep_prob + torch.rand(x.size())
binary_tensor = Variable(torch.floor(random_tensor))
if torch.cuda.is_available():
binary_tensor = binary_tensor.cuda()
x = x.mul(binary_tensor)
ret = x + self.alpha * (1-binary_tensor)
ret.mul_(self.a).add_(self.b)
return ret
#Selu = selu()
#dropout_selu = alpha_drop(0.05)
#x = torch.normal(torch.rand(1000, 3, 3, 224), 1)
#x = Variable(x)
#w = Selu(Variable(x))
#y = dropout_selu(w)