Search code examples
tensorflowdeep-learningautoencoder

How to initialize weights in tensorflow.contrib.layers conv2d


I wrote a simple autoencoder using tensorflow library. This is the sample code I have written for the network of the autoencoder What I dont get is how to introduce weights into this model as it is working now without any specific initialization?


Solution

  • Weight initialization is being defined when creating a conv2d layer. In the API (https://www.tensorflow.org/api_docs/python/tf/contrib/layers/conv2d) there is a parameter in such function named weights_initializer that can be filled with a tf.initializer object and defines the way weights are initialized. By default, for TFv1.8, this is a xavier_initializer (https://www.tensorflow.org/api_docs/python/tf/contrib/layers/xavier_initializer), but you could set it to any of the initializers included here: https://www.tensorflow.org/api_docs/python/tf/initializers. For example, if you want weight initialization to be constant with value 12345, you would write:

    lays.conv2d(inputs, 32,[5,5], stride=2, padding="SAME", 
                       weights_initializer=tf.constant_initializer(12345))