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?
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.
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] .
identity on a variable will make a Tensor that represents the value of that variable at the time it is called.
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.
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)
Don't want to install numpy just for np.identity
?
Here is a tensorflow-only variant:
T = tf.Variable(tf.diag(tf.ones(size)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With