-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathnn_ops.py
More file actions
29 lines (24 loc) · 811 Bytes
/
nn_ops.py
File metadata and controls
29 lines (24 loc) · 811 Bytes
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
from torch import nn
def conv3x3(in_planes, out_planes, stride=1, bias=False):
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=bias)
def variable_init(m, neg_slope=0.0):
if isinstance(m, (nn.Linear, nn.Conv2d)):
nn.init.kaiming_uniform_(m.weight.data, neg_slope)
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
if m.weight is not None:
m.weight.data.fill_(1)
if m.bias is not None:
m.bias.data.zero_()
m.running_mean.zero_()
m.running_var.zero_()
class PlusMinusOne(object):
""" Scales values that are between [0, 1] into [-1, 1]. """
def __init__(self):
pass
def __call__(self, x):
x = 2.0 * x - 1.0
return x