Search code examples
keraskeras-layerkeras-2

Keras initializers outside Keras


I want to initialize a 4*11 matrix using glorot uniform in Keras, using following code:

import keras
keras.initializers.glorot_uniform((4,11))

I get this output :

<keras.initializers.VarianceScaling at 0x7f9666fc48d0>

How can I visualize the output? I have tried c[1] and got output 'VarianceScaling' object does not support indexing.


Solution

  • The glorot_uniform() creates a function, and later this function will be called with a shape. So you need:

    # from keras.initializers import * #(tf 1.x)
    
    from tensorflow.keras.initializers import *
    
    unif = glorot_uniform() #this returns a 'function(shape)'
    mat_as_tensor = unif((4,11)) #this returns a tensor - use this in keras models if needed   
    mat_as_numpy = K.eval(mat) #this returns a numpy array (don't use in models)
    print(mat_as_numpy)