I would like to re-create the Xavier initialization in NumPy (using basic functions) in the same way that TensorFlow2 does for CNN. Here is how I learned to do Xavier initialization in NumPy:
# weights.shape = (2,2)
np.random.seed(0)
nodes_in = 2*2
weights = np.random.rand(2,2) * np.sqrt(1/nodes_in)
>>>array([[0.27440675, 0.35759468],
[0.30138169, 0.27244159]])
This is the way I learned Xavier initialization for the logistic regression model. It seems that for Convolution Neural Network it should be different but I don't know how.
initializer = tf.initializers.GlorotUniform(seed=0)
tf.Variable(initializer(shape=[2,2],dtype=tf.float32))
>>><tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy=
array([[-0.7078647 , 0.50461936],
[ 0.73500216, 0.6633029 ]], dtype=float32)>
I'm confused by the TensorFlow documentation when they explain the "fan_in" and "fan_out". I'm guessing this is where the problem is. Can somebody dumb it down for me, please?
Much appreciate it!
[UPDATE]:
When I follow the tf.keras.initializers.GlorotUniform documentation I still don't come to the same results:
# weights.shape = (2,2)
np.random.seed(0)
fan_in = 2*2
fan_out = 2*2
limit = np.sqrt(6/(fan_in + fan_out))
np.random.uniform(-limit,limit,size=(2,2))
>>>array([[0.08454747, 0.37271892],
[0.17799139, 0.07773995]])