Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize Tensor variable as identity matrix in TensorFlow

How do I initialize a Tensor T as the identity matrix?

The following initializes T as a 784 by 784 matrix of zeros.

T = tf.Variable(tf.zeros([784, 784]))

But I can't find a tf.fn that behaves as required. How can this be done?

like image 930
Daniel Que Avatar asked Jul 13 '16 18:07

Daniel Que


People also ask

How do you initialize a TensorFlow variable in a matrix?

First, remember that you can use the TensorFlow eye functionality to easily create a square identity matrix. We create a 5x5 identity matrix with a data type of float32 and assign it to the Python variable identity matrix. So we used tf. eye, give it a size of 5, and the data type is float32.

How do you transpose a tensor in TensorFlow?

transpose(x, perm=[1, 0]) . As above, simply calling tf. transpose will default to perm=[2,1,0] . To take the transpose of the matrices in dimension-0 (such as when you are transposing matrices where 0 is the batch dimension), you would set perm=[0,2,1] .

What is identity in TensorFlow?

identity on a variable will make a Tensor that represents the value of that variable at the time it is called.


3 Answers

You can actually pass numpy arrays as an argument for initial_value, so tf.Variable(initial_value = np.identity(784)) should do what you intend to do.

like image 173
eager2learn Avatar answered Nov 14 '22 21:11

eager2learn


The tf.fn you are looking for is called tf.eye. Thus, the most succinct answer is

T = tf.Variable(tf.eye(size))

Note: putting this in tf.Variable initializes learnable weights to the identity, but allows that it may be changed. If you actually just want the constant of an identity matrix, then simply use

T = tf.eye(size)
like image 43
Multihunter Avatar answered Nov 14 '22 21:11

Multihunter


Don't want to install numpy just for np.identity ?
Here is a tensorflow-only variant:

T = tf.Variable(tf.diag(tf.ones(size)))
like image 35
NiziL Avatar answered Nov 14 '22 22:11

NiziL