-
Notifications
You must be signed in to change notification settings - Fork 0
/
q2_initialization.py
52 lines (41 loc) · 1.74 KB
/
q2_initialization.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
import numpy as np
import tensorflow as tf
def xavier_weight_init():
"""Returns function that creates random tensor.
The specified function will take in a shape (tuple or 1-d array) and
returns a random tensor of the specified shape drawn from the
Xavier initialization distribution.
Hint: You might find tf.random_uniform useful.
"""
def _xavier_initializer(shape, **kwargs):
"""Defines an initializer for the Xavier distribution.
Specifically, the output should be sampled uniformly from [-epsilon, epsilon] where
epsilon = sqrt(6) / <sum of the sizes of shape's dimensions>
e.g., if shape = (2, 3), epsilon = sqrt(6 / (2 + 3))
This function will be used as a variable initializer.
Args:
shape: Tuple or 1-d array that species the dimensions of the requested tensor.
Returns:
out: tf.Tensor of specified shape sampled from the Xavier distribution.
"""
### YOUR CODE HERE
epsilon = tf.sqrt(6 / tf.reduce_sum(shape))
out = tf.random_uniform(shape,minval=-epsilon,maxval=epsilon,dtype=tf.float64)
### END YOUR CODE
return out
# Returns defined initializer function.
return _xavier_initializer
def test_initialization_basic():
"""Some simple tests for the initialization.
"""
print("Running basic tests...")
xavier_initializer = xavier_weight_init()
shape = (1,)
xavier_mat = xavier_initializer(shape)
assert xavier_mat.get_shape() == shape
shape = (1, 2, 3)
xavier_mat = xavier_initializer(shape)
assert xavier_mat.get_shape() == shape
print("Basic (non-exhaustive) Xavier initialization tests pass")
if __name__ == "__main__":
test_initialization_basic()