How to Specify a diagonal matrix using tf.get_vari

2019-08-25 12:59发布

问题:

I am trying to create a diagonal matrix using tf.get_variable But I do not know how! Like I can make a variable which is a diagonal matrix like:

dia_size = tf.zeros((num_filters, img_size))
b = tf.Variable(tf.matrix_diag(dia_size), name=name)
b = tf.reshape(b, [-1, img_size, img_size, num_filters])

but I can not do it with tf.get_variable.

Thanks for your help in advance!

回答1:

If you set the initializer parameter of tf.get_variable to a tensor, the variable will be initialized to the tensor's value. Therefore, you can use the following code:

dia_size = tf.zeros((num_filters, img_size))
b = tf.matrix_diag(dia_size)
var = tf.get_variable(..., initializer=b, ...)