-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.py
78 lines (55 loc) · 3 KB
/
init.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import math
import needle as ndl
def rand(*shape, low=0.0, high=1.0, device=None, dtype="float32", requires_grad=False):
""" Generate random numbers uniform between low and high """
device = ndl.cpu() if device is None else device
array = device.rand(*shape) * (high - low) + low
return ndl.Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad)
def randn(*shape, mean=0.0, std=1.0, device=None, dtype="float32", requires_grad=False):
""" Generate random normal with specified mean and std deviation """
device = ndl.cpu() if device is None else device
array = device.randn(*shape) * std + mean
return ndl.Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad)
def constant(*shape, c=1.0, device=None, dtype="float32", requires_grad=False):
""" Generate constant Tensor """
device = ndl.cpu() if device is None else device
array = device.ones(*shape, dtype=dtype) * c # note: can change dtype
return ndl.Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad)
def ones(*shape, device=None, dtype="float32", requires_grad=False):
""" Generate all-ones Tensor """
return constant(*shape, c=1.0, device=device, dtype=dtype, requires_grad=requires_grad)
def zeros(*shape, device=None, dtype="float32", requires_grad=False):
""" Generate all-zeros Tensor """
return constant(*shape, c=0.0, device=device, dtype=dtype, requires_grad=requires_grad)
def randb(*shape, p=0.5, device=None, dtype="bool", requires_grad=False):
""" Generate binary random Tensor """
device = ndl.cpu() if device is None else device
array = device.rand(*shape) <= p
return ndl.Tensor(array, device=device, dtype=dtype, requires_grad=requires_grad)
def one_hot(n, i, device=None, dtype="float32", requires_grad=False):
""" Generate one-hot encoding Tensor """
device = ndl.cpu() if device is None else device
return ndl.Tensor(device.one_hot(n,i.numpy(), dtype=dtype), device=device, requires_grad=requires_grad)
def xavier_uniform(fan_in, fan_out, gain=1.0, **kwargs):
### BEGIN YOUR SOLUTION
a = gain * math.sqrt(6 / (fan_in + fan_out))
return a * (2 * rand(fan_in, fan_out, **kwargs) - 1)
### END YOUR SOLUTION
def xavier_normal(fan_in, fan_out, gain=1.0, **kwargs):
### BEGIN YOUR SOLUTION
std = gain * math.sqrt(2 / (fan_in + fan_out))
return std * randn(fan_in, fan_out, **kwargs)
### END YOUR SOLUTION
def kaiming_uniform(fan_in, fan_out, nonlinearity="relu", **kwargs):
assert nonlinearity == "relu", "Only relu supported currently"
### BEGIN YOUR SOLUTION
bound = math.sqrt(2) * math.sqrt(3 / fan_in)
return bound * (2 * rand(fan_in, fan_out, **kwargs) - 1)
### END YOUR SOLUTION
def kaiming_normal(fan_in, fan_out, nonlinearity="relu", **kwargs):
assert nonlinearity == "relu", "Only relu supported currently"
### BEGIN YOUR SOLUTION
gain = math.sqrt(2)
std = gain / math.sqrt(fan_in)
return std * randn(fan_in, fan_out, **kwargs)
### END YOUR SOLUTION