How to Specify a diagonal matrix using tf.get_variable

隐身守侯 提交于 2019-12-11 17:43:33

问题


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, ...)


来源:https://stackoverflow.com/questions/47425424/how-to-specify-a-diagonal-matrix-using-tf-get-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!