Abrar
Abrar

Reputation: 631

initialize matrices in tensorflow

I have 6 matrices, which my model will learn, I defined them as follow:

self.R= tf.get_variable('R_',dtype=tf.float32, shape=[6,300 ,300],
           initializer=tf.random_uniform_initializer(maxval=0.1, minval=-0.1))

what I need to do is to change the initialization. I want to initialize each one of them as an identity matrix. can someone help me with that?

Upvotes: 2

Views: 2908

Answers (2)

Michail N
Michail N

Reputation: 3855

If you want to create a 6x300x300 matrix where each 300x300 array is an identity matrix you can simply:

import numpy as np;

dimension = 300 
singleIdentityMatrix = np.identity(dimension, dtype= np.float32) 
stackedMatrix = np.dstack( [singleIdentityMatrix] * 6)

and pass this matrix with

self.R = tf.Variable(initial_value = stackedMatrix)

Upvotes: 2

Tianjin Gu
Tianjin Gu

Reputation: 784

identity initializer should help, it's available in TensorFlow 1.3. this interface only support 2D array.

Change your code into

self.R= tf.get_variable('R_',dtype=tf.float32, shape=[6,300 ,300], initializer=tf.initializers.identity())

Another way is you generate a identity matrix with numpy and as initial value of Variable, but the identity matrix size can not too large, which will cause 'tf.Graph' larger than 2GB

Upvotes: 1

Related Questions